使用SpringCloudGateway的重要功能 – 权重路由
由于后端的服务器有时候性能不同,或者是由于需要灰度发布,需要上线一部分新版本的服务
注:灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度,而我们平常所说的金丝雀部署也就是灰度发布的一种方式。
spring-cloud-gateway自身提供一个predicate叫WeightRoutePredicateFactory,在启动的时候也可以看到
2019-05-29 23:06:47.321 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2019-05-29 23:06:47.321 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2019-05-29 23:06:47.322 INFO 4736 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]
但是不知道为什么在文档里面没有相关的描述,在官方ISSUE里面也有人提到了这个问题
https://github.com/spring-cloud/spring-cloud-gateway/issues/1072
具体的使用方式
routes:
- id: spring-cloud-client-demo
uri: lb://spring-cloud-client-demo
predicates:
- Path=/client/**
- Weight=group1, 2
filters:
- AddRequestHeader=X-Request-Foo, Bar
- StripPrefix=1
- SaveSession
- id: spring-cloud-client-demo1
uri: lb://spring-cloud-client-demo
predicates:
- Path=/client/**
- Weight=group1, 8
filters:
- StripPrefix=2
Weight有两个属性,group1是指定分组,后面的数字代表了权重
使用PostMan发起请求
GET http://localhost:10001/client/demo/demo/weight?param=mt
在对应的spring-cloud-client-demo工程里面有两个不同的controller
@GetMapping("/weight")
public String weight(@RequestParam String param) {
log.info("aaaa: {}", param);
return "aaa";
}
@GetMapping("/demo/weight")
public String demoWeight(@RequestParam String param) {
log.info("===============sb: {}", param);
return "bbb";
}
发起多次请求,可以观察到大概满足1:4这样一个比例
2019-05-29 23:19:15.462 INFO 1764 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:29.399 INFO 1764 --- [nio-8801-exec-2] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:33.512 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:36.425 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:19:37.404 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:19:39.425 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:42.396 INFO 1764 --- [nio-8801-exec-7] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:44.393 INFO 1764 --- [nio-8801-exec-8] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:45.422 INFO 1764 --- [nio-8801-exec-9] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:47.398 INFO 1764 --- [io-8801-exec-10] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:19:48.389 INFO 1764 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:50.499 INFO 1764 --- [nio-8801-exec-2] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:54.380 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:55.399 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:56.404 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:57.402 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:58.393 INFO 1764 --- [nio-8801-exec-7] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:19:59.395 INFO 1764 --- [nio-8801-exec-8] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:00.397 INFO 1764 --- [nio-8801-exec-9] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:02.408 INFO 1764 --- [io-8801-exec-10] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:03.390 INFO 1764 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:05.435 INFO 1764 --- [nio-8801-exec-2] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:06.430 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:13.410 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:20:14.435 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:20:17.481 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : aaaa: mt
例如,这里proxy为了方便两个使用了一个url,实际是使用不同的地址
- id: proxy
uri: http://localhost:8801
predicates:
- Path=/demo/**
- Weight=group2, 8
- id: proxy1
uri: http://localhost:8801
predicates:
- Path=/demo/**
- Weight=group2, 2
filters:
- StripPrefix=1
用PostMan发起请求
http://localhost:10001/demo/demo/weight?param=mt
可以看到,这两个反向代理是属于同一组代理,按照权重来进行路由,实现了不同url的负载均衡
2019-05-29 23:26:07.460 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:10.399 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:13.482 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:17.424 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:20.390 INFO 1764 --- [nio-8801-exec-7] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:23.405 INFO 1764 --- [nio-8801-exec-8] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:24.405 INFO 1764 --- [nio-8801-exec-9] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:26.415 INFO 1764 --- [io-8801-exec-10] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:27.423 INFO 1764 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:29.421 INFO 1764 --- [nio-8801-exec-2] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:32.407 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:34.392 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:37.418 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:38.412 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:40.482 INFO 1764 --- [nio-8801-exec-7] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:43.408 INFO 1764 --- [nio-8801-exec-8] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:44.459 INFO 1764 --- [nio-8801-exec-9] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:46.426 INFO 1764 --- [io-8801-exec-10] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:47.403 INFO 1764 --- [nio-8801-exec-1] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:48.408 INFO 1764 --- [nio-8801-exec-2] c.m.d.client.controller.DemoController : aaaa: mt
2019-05-29 23:26:49.410 INFO 1764 --- [nio-8801-exec-3] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:50.409 INFO 1764 --- [nio-8801-exec-5] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:51.429 INFO 1764 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:53.441 INFO 1764 --- [nio-8801-exec-4] c.m.d.client.controller.DemoController : ===============sb: mt
2019-05-29 23:26:55.435 INFO 1764 --- [nio-8801-exec-7] c.m.d.client.controller.DemoController : ===============sb: mt
GITHUB代码地址
欢迎关注微信交流