目录
(一)Nacos动态配置
(二)Nacos注册中心
(三)Sentinel之限流
(四)Sentinel之熔断
(五)Gateway之路由、限流
(六)Gateway之鉴权、日志
(七)Gateway搭配Nacos实现动态路由
(八)Dubbo + Nacos
正文
在 Spring Cloud Alibaba实战(五) - Gateway之路由、限流 中,路由信息定义在配置文件中,这种方式有一个缺点就是修改路由信息必须重启服务才能生效。网关作为全部流量的入口,可用时间当然越长越好,不重启服务而修改路由是一个更好的选择,结合Nacos可以做到这一点。
首先,参考前面章节启动Nacos、account-service和payment-service。并以gateway项目为基础增加本节功能。
Spring Cloud Gateway本身还不支持直接从Nacos动态加载路由配置表,需要自己编写监听器监听配置变化并刷新路由表。
NacosDynamicRouteService.java
@Component
public class NacosDynamicRouteService implements ApplicationEventPublisherAware {
private String dataId = "gateway-router";
private String group = "DEFAULT_GROUP";
@Value("${spring.cloud.nacos.config.server-addr}")
private String serverAddr;
@Autowired
private RouteDefinitionWriter routeDefinitionWriter;
private ApplicationEventPublisher applicationEventPublisher;
private static final List ROUTE_LIST = new ArrayList<>();
@PostConstruct
public void dynamicRouteByNacosListener() {
try {
ConfigService configService = NacosFactory.createConfigService(serverAddr);
configService.getConfig(dataId, group, 5000);
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
clearRoute();
try {
List gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class);
for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
addRoute(routeDefinition);
}
publish();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Executor getExecutor() {
return null;
}
});
} catch (NacosException e) {
e.printStackTrace();
}
}
private void clearRoute() {
for(String id : ROUTE_LIST) {
this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
}
ROUTE_LIST.clear();
}
private void addRoute(RouteDefinition definition) {
try {
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
ROUTE_LIST.add(definition.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
private void publish() {
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
代码中监听的配置ID为gateway-router,按此ID在Nacos中创建配置
从bootstrap.yml中删除路由配置,即删除以下内容
routes:
- id: payment-router
uri: lb://payment-service
predicates:
- Path=/pay/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 1
redis-rate-limiter.burstCapacity: 5
key-resolver: '#{@ipKeyResolver}'
动态路由功能修改完成,启动gateway测试,目前路由表中仅匹配了/acc/**的,分别测试一下/acc和/pay。
/acc/user请求成功转发到account-service,而/pay/balance没有找到匹配的路由信息,与期望行为一致。
下面来动态增加/pay的路由,修改Nacos中的gateway-router配置如下:
[{
"id": "account-router",
"order": 0,
"predicates": [{
"args": {
"pattern": "/acc/**"
},
"name": "Path"
}],
"uri": "lb://account-service"
},{
"id": "payment-router",
"order": 2,
"predicates": [{
"args": {
"pattern": "/pay/**"
},
"name": "Path"
}],
"uri": "lb://payment-service"
}]
不重启gateway再次测试/pay/balance请求
可以看到新增加的路由配置已生效。
本期代码
链接:https://pan.baidu.com/s/1ANe7slFiUw0nm1HBXdzzFg
提取码:2was