Zuul
Zuul做为网关层,自身也是一个微服务,跟其它服务Service-1,Service-2, … Service-N一样,都注册在eureka server上,可以相互发现,zuul能感知到哪些服务在线,同时通过配置路由规则(后面会给出示例),可以将请求自动转发到指定的后端微服务上,对于一些公用的预处理(比如:权限认证,token合法性校验,灰度验证时部分流量引导之类),可以放在所谓的过滤器(ZuulFilter)里处理,这样后端服务以后新增了服务,zuul层几乎不用修改。
服务说明
Zuul官方说明:https://github.com/Netflix/zuul/wiki
服务搭建
新建module,命名为cloud-zuul
修改pom
引入netflix的zuul包,使用zuul时最好与eureka集成。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
修改启动类
在启动类上添加注释**@EnableZuulProxy**
修改配置文件
spring:
application:
name: cloud-zuul
server:
port: 8080
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
ignored-services: "*"
strip-prefix: true #请求转发时去掉转发路径(path中的值)
routes:
app-console-web: #所有以/console开头的请求都转发到app-console-web应用
path: /console/**
serviceId: app-console-web
应用示例
https://gitee.com/xiaoxming/cloud_project/tree/master/cloud-zuul