动态路由背景
无论你在使用zuul还是spring cloud gateway 的时候,官方文档提供的方案总是基于配置文件配置的方式
例如:
# zuul 的配置形式
routes:
pig-auth:
path: /auth/**
serviceid: pig-auth
stripprefix: true
# gateway 的配置形式
routes:
- id: pigx-auth
uri: lb://pigx-auth
predicates:
- path=/auth/**
filters:
- validatecodegatewayfilter
配置更改需要重启服务,不能满足实际生产过程中的动态刷新、实时变更的业务需求。
基于以上分析 pig已经提供了基于zuul版本的动态路由功能,附git 地址传送门,效果如下图可以实时配置修改刷新。
spring cloud gateway 路由加载源码
dispatcherhandler 接管用户请求
routepredicatehandlermapping 路由匹配
根据routelocator获取 routedefinitionlocator
返回多个routedefinitionlocator.getroutedefinitions()的路由定义信息
filteringwebhandler执行路由定义中的filter 最后路由到具体的业务服务中
spring cloud gateway 默认动态路由实现
gatewaycontrollerendpoint 基于actuate端点的默认实现,支持jvm 级别的动态路由,不能序列化存储
// 上图动态路由的信息保存的默认实现是基于内存的实现
public class inmemoryroutedefinitionrepository implements routedefinitionrepository {
private final map routes = synchronizedmap(new linkedhashmap());