SpringCloud 商城系统搭建之Zuul
Spring Cloud Zuul简介
Spring Cloud Zuul路由是微服务架构的不可或缺的一部分,提供动态路由,监控,弹性,安全等的边缘服务。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。
前提
本文是基于SpringCloud 商城系统搭建之eureka
创建supermarker 项目的子项目(supermarker_zuul),并在pom.xml 添加相关依赖.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zzg</groupId>
<artifactId>supermarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>supermarker_zuul</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--zuul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
Zuul网关配置
1、在EurekaZuulApplication启动类上加@EnableZuulProxy注解:开启Zuul网关代理的功能
package com.zzg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EurekaZuulApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(EurekaZuulApplication.class, args);
}
}
2、打开Spring Cloud Zuul的application.properties,添加如下配置
server.port=8086
spring.application.name=zuul
# 和eureka服务器通讯的URL
eureka.client.service-url.defaultZone=http://localhost:8081/eureka/
自此,一个简单的微服务网关就编写完毕。
3、微服务网关功能验证,第一步查看可被网关代理的生成者ServiceId,通过访问http://localhost:8081/
注意:默认情况下,Zuul会代理所有注册到Eureka Server的微服务(生产者),并且Zuul的路径规则如下:
http://zuul 网关所在服务器IP地址:zuul 网关端口/微服务在Eureka Server上的生成者/会被转发到生产者对应的微服务地址
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。