欢迎访问Spring Cloud中国社区

《重新定义Spring Cloud实战》由Spring Cloud中国社区倾力打造,基于Spring Cloud的Finchley.RELEASE版本,本书内容宽度足够广、深度足够深,而且立足于生产实践,直接从生产实践出发,包含大量生产实践的配置。欢迎加微信Software_King进群答疑,国内谁在使用Spring Cloud?欢迎登记

Spring Cloud Gateway的动态路由实现

xujin · 15天前 · 469 ·

摘要:本文主要介绍了Spring Cloud Gateway的动态路由的简单实现方式。

1.前言

网关中有两个重要的概念,那就是路由配置和路由规则,路由配置是指配置某请求路径路由到指定的目的地址。而路由规则是指匹配到路由配置之后,再根据路由规则进行转发处理。
Spring Cloud Gateway作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,尽量避免重启,需要实现Spring Cloud Gateway动态路由配置。前面章节介绍了Spring Cloud Gateway提供的两种方法去配置路由规则,但都是在Spring Cloud Gateway启动时候,就将路由配置和规则加载到内存里,无法做到不重启网关就可以动态的对应路由的配置和规则进行增加,修改和删除。本篇文章简单介绍如何实现Spring Cloud Gateway的动态路由。

2. Spring Cloud Gateway简单的动态路由实现

Spring Cloud Gateway的官方文档并没有讲如何动态配置,查看 Spring Cloud Gateway的源码,发现在org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint类中提供了动态配置的Rest接口,但是需要开启Gateway的端点,而且提供的功能不是很强大。通过参考和GatewayControllerEndpoint相关的代码,可以自己编码实际动态路由配置。
下面通过案例的方式去讲解怎么实现Gateway的动态路由配置。案例工程如ch18-7-gateway所示。

代码地址:https://github.com/SpringCloud/spring-cloud-code/blob/master/ch18-7/ch18-7-gateway

3. 简单动态路由的实现

3.1 新建Maven工程ch18-7-gateway

配置主要的核心依赖如代码清单18-33所示:
代码清单: ch18-7/ch18-7-gateway/pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-gateway</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-webflux</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-actuator</artifactId>
  13. </dependency>
  14. </dependencies>

3.2 根据Spring Cloud Gateway的路由模型定义数据传输模型

分别创建GatewayRouteDefinition.java, GatewayPredicateDefinition.java, GatewayFilterDefinition.java这三个类。
(1) 创建路由定义模型如下代码清单18-34所示:
代码清单 18-34: ch18-7/ch18-7-gateway/src/main/java/cn/springcloud/book/gateway/model/GatewayRouteDefinition.java

  1. public class GatewayRouteDefinition {
  2. //路由的Id
  3. private String id;
  4. //路由断言集合配置
  5. private List<GatewayPredicateDefinition> predicates = new ArrayList<>();
  6. //路由过滤器集合配置
  7. private List<GatewayFilterDefinition> filters = new ArrayList<>();
  8. //路由规则转发的目标uri
  9. private String uri;
  10. //路由执行的顺序
  11. private int order = 0;
  12. //此处省略get和set方法
  13. }

(2)创建过滤器定义模型,代码如代码清单18-35所示:
代码清单18-35: ch18-7/ch18-7-gateway/src/main/java/cn/springcloud/book/gateway/model/GatewayFilterDefinition.java

  1. public class GatewayFilterDefinition {
  2. //Filter Name
  3. private String name;
  4. //对应的路由规则
  5. private Map<String, String> args = new LinkedHashMap<>();
  6. //此处省略Get和Set方法
  7. }

(3)路由断言定义模型,代码如代码清单18-36所示:
代码清单18-36: ch18-7/ch18-7-gateway/src/main/java/cn/springcloud/book/gateway/model/GatewayPredicateDefinition.java

  1. public class GatewayPredicateDefinition {
  2. //断言对应的Name
  3. private String name;
  4. //配置的断言规则
  5. private Map<String, String> args = new LinkedHashMap<>();
  6. //此处省略Get和Set方法
  7. }

3.3 编写动态路由实现类

编写DynamicRouteServiceImpl并实现ApplicationEventPublisherAware接口,代码如代码清单18-37所示: ch18-37/ch18-7-gateway/src/main/java/cn/springcloud/book/gateway/route/DynamicRouteServiceImpl.java

  1. @Service
  2. public class DynamicRouteServiceImpl implements ApplicationEventPublisherAware {
  3. @Autowired
  4. private RouteDefinitionWriter routeDefinitionWriter;
  5. private ApplicationEventPublisher publisher;
  6. //增加路由
  7. public String add(RouteDefinition definition) {
  8. routeDefinitionWriter.save(Mono.just(definition)).subscribe();
  9. this.publisher.publishEvent(new RefreshRoutesEvent(this));
  10. return "success";
  11. }
  12. //更新路由
  13. public String update(RouteDefinition definition) {
  14. try {
  15. this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
  16. } catch (Exception e) {
  17. return "update fail,not find route routeId: "+definition.getId();
  18. }
  19. try {
  20. routeDefinitionWriter.save(Mono.just(definition)).subscribe();
  21. this.publisher.publishEvent(new RefreshRoutesEvent(this));
  22. return "success";
  23. } catch (Exception e) {
  24. return "update route fail";
  25. }
  26. }
  27. //删除路由
  28. public Mono<ResponseEntity<Object>> delete(String id) {
  29. return this.routeDefinitionWriter.delete(Mono.just(id))
  30. .then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build())))
  31. .onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
  32. }
  33. @Override
  34. public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  35. this.publisher = applicationEventPublisher;
  36. }
  37. }

3.4 编写Rest接口

编写RouteController类的提供Rest接口,用于动态路由配置。代码如代码清单18-38所示:
代码清单 18-38: ch18-7/ch18-7-gateway/src/main/java/cn/springcloud/book/gateway/controller/RouteController.java

  1. @RestController
  2. @RequestMapping("/route")
  3. public class RouteController {
  4. @Autowired
  5. private DynamicRouteServiceImpl dynamicRouteService;
  6. //增加路由
  7. @PostMapping("/add")
  8. public String add(@RequestBody GatewayRouteDefinition gwdefinition) {
  9. try {
  10. RouteDefinition definition = assembleRouteDefinition(gwdefinition);
  11. return this.dynamicRouteService.add(definition);
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. return "succss";
  16. }
  17. //删除路由
  18. @DeleteMapping("/routes/{id}")
  19. public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
  20. return this.dynamicRouteService.delete(id);
  21. }
  22. //更新路由
  23. @PostMapping("/update")
  24. public String update(@RequestBody GatewayRouteDefinition gwdefinition) {
  25. RouteDefinition definition = assembleRouteDefinition(gwdefinition);
  26. return this.dynamicRouteService.update(definition);
  27. }
  28. }

3.5 配置application.yml文件

在application.yml文件配置应用的配置信息,并开启Spring Cloud Gateway对外提供的端点Rest接口。代码如代码清单18-39所示:
代码清单 18-39: ch18-7/ch18-7-gateway/src/main/resources/application.yml
配置输出日志如下所示:

  1. # 配置输出日志
  2. logging:
  3. level:
  4. org.springframework.cloud.gateway: TRACE
  5. org.springframework.http.server.reactive: DEBUG
  6. org.springframework.web.reactive: DEBUG
  7. reactor.ipc.netty: DEBUG
  8. #开启端点
  9. management:
  10. endpoints:
  11. web:
  12. exposure:
  13. include: '*'
  14. security:
  15. enabled: false

3.6 启动ch18-7-gateway应用测试

(1) 启动ch18-7-gateway应用之后,由于开启了端点,首先打开浏览器访问端点URL:
http://localhost:8080/actuator/gateway/routes ,查看路由信息返回为空,如下图所示:

(2)打开PostMan,访问http://localhost:8080/route/add, 发起Post请求,如下图所示,返回success说明向Gateway增加路由配置成功。

然后再打开PostMan访问端点URL:http://localhost:8080/actuator/gateway/routes ,
查看路由信息返回如下图所示,可以看到已经添加的路由配置。

(3) 打开浏览器访问http://localhost:8080/jd, 可以正常转发https://www.jd.com/对应的京东商城首页。
(4) 通过访问http://localhost:8080/route/update, 对id为jd_route的路由更新配置,如下图所示:

然后再访问路由端点URL,发现路由配置已经被更新,如下图所示:

然后通过浏览器访问http://localhost:8080/taobao ,可以成功转发到淘宝网。
(5) 通过访问http: //localhost:8080/route/delete/jd_route,其中的id为路由对应的id,删除路由结果如下图所示:

4.Spring Cloud Gateway推荐文章

Spring Cloud Gateway中的权重路由
Spring Cloud Gateway中的GatewayFilter和GlobalFilter
Spring Cloud Gateway只有Pre和POST两种类型的Filter
Spring Cloud Gateway基于服务发现的默认路由规则
Spring Cloud Gateway的Before路由断言工厂
Spring Cloud Gateway的After路由断言工厂
Spring Cloud Gateway揭秘之处理请求流程
Spring Cloud Gateway入门案例

5.《重新定义Spring Cloud实战》中的Spring Cloud Gateway

第17章Spring Cloud Gateway上篇399
17.1 Spring Cloud Gateway概述399
17.1.1 什么是Spring Cloud Gateway399
17.1.2 Spring Cloud Gateway的核心概念399
17.2 Spring Cloud Gateway的工作原理400
17.3 Spring Cloud Gateway入门案例401
17.4 Spring Cloud Gateway的路由断言404
17.4.1 After路由断言工厂404
17.4.2 Before路由断言工厂406
17.4.3 Between路由断言工厂406
17.4.4 Cookie路由断言工厂407
17.4.5 Header路由断言工厂408
17.4.6 Host路由断言工厂410
17.4.7 Method路由断言工厂411
17.4.8 Query路由断言工厂411
17.4.9 RemoteAddr路由断言工厂412
17.5 Spring Cloud Gateway的内置Filter413
17.5.1 AddRequestHeader过滤器工厂413
17.5.2 AddRequestParameter过滤器413
17.5.3 RewritePath过滤器414
17.5.4 AddResponseHeader过滤器415
17.5.5 StripPrefix过滤器416
17.5.6 Retry过滤器417
17.5.7 Hystrix过滤器418
17.6 本章小结420
第18章 Spring Cloud Gateway下篇421
18.1 Gateway基于服务发现的路由规则421
18.1.1 Gateway的服务发现路由概述421
18.1.2 服务发现的路由规则案例422
18.2 Gateway Filter和Global Filter425
18.2.1 Gateway Filter和Global Filter概述425
18.2.2 自定义Gateway Filter案例425
18.2.3 自定义Global Filter案例427
18.3 Spring Cloud Gateway实战428
18.3.1 Spring Cloud Gateway权重路由428
18.3.2 Spring Cloud Gateway中Https的使用技巧431
18.3.3 Spring Cloud Gateway集成Swagger436
18.3.4 Spring Cloud Gateway限流442
18.3.5 Spring Cloud Gateway的动态路由450
18.4 Spring Cloud Gateway源码篇458
18.4.1 Spring Cloud Gateway的处理流程458
18.4.2 Gateway中ServerWebExchange构建分析459
18.4.3 DispatcherHandler源码分析460
18.4.4 RoutePredicateHandlerMapping源码分析461
18.4.5 FilteringWebHandler源码分析462
18.4.6 执行Filter源码分析463
18.5 本章小结465

  1. ├── ch17-1
  2. ├── ch17-1-1-gateway
  3. ├── ch17-1-2-gateway
  4. ├── ch17-1.iml
  5. └── pom.xml
  6. ├── ch17-2
  7. ├── ch17-2-1-gateway
  8. ├── ch17-2-2-gateway
  9. ├── ch17-2-3-gateway
  10. ├── ch17-2-4-gateway
  11. ├── ch17-2-5-gateway
  12. ├── ch17-2-6-gateway
  13. ├── ch17-2-7-gateway
  14. ├── ch17-2-8-gateway
  15. ├── ch17-2-9-gateway
  16. ├── ch17-2-service
  17. ├── ch17-2.iml
  18. └── pom.xml
  19. ├── ch17-3
  20. ├── ch17-3-1-gateway
  21. ├── ch17-3-2-gateway
  22. ├── ch17-3-3-gateway
  23. ├── ch17-3-4-gateway
  24. ├── ch17-3-5-gateway
  25. ├── ch17-3-6-gateway
  26. ├── ch17-3-7-gateway
  27. ├── ch17-3-service
  28. ├── ch17-3.iml
  29. └── pom.xml
  30. ├── ch18-1
  31. ├── ch18-1-consumer
  32. ├── ch18-1-eureka
  33. ├── ch18-1-gateway
  34. ├── ch18-1-provider
  35. ├── ch18-1.iml
  36. └── pom.xml
  37. ├── ch18-2
  38. ├── ch18-2-gateway
  39. ├── ch18-2-provider
  40. ├── ch18-2.iml
  41. ├── pom.xml
  42. └── reademe.txt
  43. ├── ch18-3
  44. ├── ch18-3-gateway
  45. ├── ch18-3-provider
  46. ├── ch18-3.iml
  47. └── pom.xml
  48. ├── ch18-4
  49. ├── ch18-4-eureka
  50. ├── ch18-4-gateway-https
  51. ├── ch18-4-service-a
  52. ├── ch18-4-service-b
  53. ├── ch18-4.iml
  54. ├── pom.xml
  55. └── reademe.md
  56. ├── ch18-5
  57. ├── ch18-5-eureka
  58. ├── ch18-5-gateway
  59. ├── ch18-5-service
  60. ├── ch18-5.iml
  61. └── pom.xml
  62. ├── ch18-6
  63. ├── ch18-6-1-gateway
  64. ├── ch18-6-2-gateway
  65. ├── ch18-6-3-gateway
  66. ├── ch18-6-provider
  67. ├── ch18-6.iml
  68. └── pom.xml
  69. ├── ch18-7
  70. ├── ch18-7-gateway
  71. ├── ch18-7.iml
  72. ├── pom.xml
  73. └── readme.md

Spring Cloud Gateway所有示例代码地址:https://github.com/SpringCloud/spring-cloud-code