zuul的使用

1、pom.xml
有两个注重点:
(1)springBoot2.1.0对于zuul有不兼容的问题会报错,报错见https://blog.csdn.net/weixin_42152604/article/details/86232199
(2)官方文档:

the Zuul starter does not include a discovery client, so for routes based on service IDs you need to provide one of those on the classpath as well (e.g. Eureka is one choice).
大意是zuul starter这个依赖中不包括服务发现部分,所以我们在用的时候要加上服务发现组件的依赖,例如eureka
4.0.0org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE com.strive zuul 0.0.1-SNAPSHOT zuul Demo project for Spring BootUTF-8UTF-8 1.8 Finchley.SR1 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-maven-plugin

2.启动类
@SpringBootApplication @EnableZuulProxy public class ZuulApplication {public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); }}

遇到了一个让我纠结了一下下并且没有想通的问题:
Finchley.SR1 版本下的@EnableZuulProxy注解中不包含@EnableDiscoveryClient注解,按照我的理解,应该是要在启动类上加上这个注解,可是在我没有加@EnableDiscoveryClient注解的情况下,启动成功了,并且在Eureka上注册成功,谁如果找到原因了,麻烦留言帮我解决下,谢谢!

@EnableCircuitBreaker @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Import({ZuulProxyMarkerConfiguration.class}) public @interface EnableZuulProxy { }

3、application.yml
不同的application.yml的详解
(1)最为简单的eureka-client的配置
这个时候zuul会自动代理你eureka上注册的所有服务,测试时我使用的是之前的eureka-client-provider项目,
http://localhost:8601/eureka-client-provider/getUUid/2这样访问就是使用的zuul对eureka-client-provider服务进行了代理进行的访问
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true

访问之后我们能够在zuul的项目日志中看到
Mapped URL path [/eureka-client-provider/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]

这条日志就可以说明,zuul完成了映射
(2)配置了简单的映射规则
ignoredServices: '*'代表的是zuul忽略代理所有的微服务 routes: eureka-client-provider: /provider/**代表的是eureka-client-provider这个服务的映射路径是在/provider/下面

也就是说我们访问http://172.20.101.73:8601/provider/getUUid/2这个路径就能访问到,如果我们不使用ignoredServices这个属性,那么默认的映射规则不会被忽略,那么http://172.20.101.73:8601/eureka-client-provider/getUUid/2这个路径也将能够成功访问
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true zuul: ignoredServices: '*' routes: eureka-client-provider: /provider/**

(3)path+serviceId指定
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true zuul: routes: xxx: path: /provider/** serviceId: eureka-client-provider

xxx保证唯一性即可:
serviceId代表的是我们要代理的服务的application name
path代表的是我们的映射地址,此处注意
/provider/*代表匹配一层,也就是只匹配/provider/x,不匹配/provider/x/x

/provider/**代表这些都匹配
当然,因为这部分我们也没有使用ignoredServices属性,所以http://172.20.101.73:8601/eureka-client-provider/getUUid/2这个地址依然能够访问,同时根据配置得http://172.20.101.73:8601/provider/getUUid/2这个地址也能访问
(4)path+url指定
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true zuul: routes: xxx: path: /provider/** serviceId: http://172.20.101.73:8600

同样,因为这部分我们也没有使用ignoredServices属性,所以http://172.20.101.73:8601/eureka-client-provider/getUUid/2这个地址依然能够访问,同时根据配置得http://172.20.101.73:8601/provider/getUUid/2这个地址也能访问
(5)负载均衡
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true zuul: routes: xxx: path: /provider/** serviceId: eureka-client-provider ribbon: eureka: enabled: false#这块是让ribbon脱离eureka去使用eureka-client-provider:#这里是serviceId ribbon: listOfServers: http://172.20.101.73:8602,http://172.20.101.73:8603

要负载均衡,那么:
a、我们不能使用path+url的形式,必须使用path+serviceId的形式
b、配置要让ribbon脱离eureka去使用
c、listOfServers配置的是对于对应的serviceId要去负载均衡的n个请求路径
(6)zuul.prefix和zuul.strip-prefix
spring: application: name: eureka-client-zuul server: port: 8601 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/ healthcheck: enabled: true zuul: prefix: /getUUid strip-prefix: false logging: level: com.netflix: debug

zuul.prefix是在我们请求zuul的时候加上前缀,zuul.strip-prefix是说我们是否在用zuul去请求别的service的时候去掉前缀,举例:
当我们只有zuul.prefix: /getUUid的时候,我们的请求就是http://172.20.108.103:8601/getUUid/eureka-client-provider/getUUid/2,strip-prefix默认是true,也就是去掉前缀;
当我们加上strip-prefix: false,也就是不去前缀,那么这时候我们的请求就是http://172.20.108.103:8601/getUUid/eureka-client-provider/2,这样才能访问到服务
(7)更加细粒度的忽略
zuul: ignoredPatterns: /**/admin/** routes: eureka-client-provider: /provider/**

代表的是忽略在/**/admin/**路径下匹配的所有服务,不去进行代理它们,然后将eureka -client-provider的代理路径变成/provider/**
(8)legacy其它的服务
zuul: routes: eureka-client-provider: path: /provider/** legacy: path: /**

表示代理eureka-client-provider服务的代理地址是/provider/**,其它的地址代理为/**,
注意此部分只能放在application.yml文件中,放在application.properties中的话,优先级会出现问题。
(9)敏感Header的控制
zuul: routes: eureka-client-provider: path: /provider/** sensitiveHeaders: Cookie,Set-Cookie,Authorization url: http://localhost:8601

sensitiveHeaders代表的是敏感信息列表,在列表中的信息不会被传递到downstream (下游),默认就是代码中的样子,如果需要传递这些参数,那么我们就需要将sensitiveHeaders设置成空。
(10)Strangulation Patterns and Local Forwards(扼杀模式和本地Forward)
zuul: routes: first: path: /first/** url: http://first.example.com second: path: /second/** url: forward:/second third: path: /third/** url: forward:/3rd legacy: path: /** url: http://legacy.example.com

这个是官网用例,和(4)中的path+url指定是一样的,forward:/second代表的是本地的转发
4、根据正则表达式匹配之间的服务名和请求路径之间的规则
@Bean public PatternServiceRouteMapper serviceRouteMapper() { return new PatternServiceRouteMapper( "(?^.+)-(?v.+$)", "${version}/${name}"); //参数1:服务名的规则;参数2:路径的规则 }

测试时,我们将eureka-client-provider这个application name和serviceId改为eureka-client-provider-v1,这样就匹配了正则表达式的规则,使用(1)中的application.yml然后我们访问时的路径也就变成了http://localhost:8601/v1/eureka-client-provider/getUUid/2 ;
当我们提供的serviceId不符合匹配的规则,name就会按照默认的情况进行请求,也就是说,如果我们现在的serviceId依然是原本的eureka-client-provider,name我们的这个配置将不起作用,请求路径不变http://localhost:8601/eureka-client-provider/getUUid/2 ;
5、@EnableZuulServer的使用
@EnableZuulServer就是@EnableZuulProxy的纯净版,@EnableZuulProxy中包含了太多的东西,如果不需要那么多,就可以选择@EnableZuulServer,然后自己去添加别的注解
6、禁用Filter
Zuul中有很多的Filter,如果我们不需要这些Filter,那么我们就可以选择去禁用它们,禁用方法例如:
zuul.SendResponseFilter.post.disable=true.
【zuul的使用】

    推荐阅读