Spring Cloud Alibaba實戰(七) - Gateway搭配Nacos實現動態路由
目錄
(一)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<String> 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<RouteDefinition> 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
相關文章
- Spring Cloud Gateway實戰之三:動態路由SpringCloudGateway路由
- Spring Cloud Alibaba實戰(八) - Dubbo + NacosSpringCloud
- Spring Cloud Alibaba 整合 Nacos 實現服務配置中心SpringCloud
- Spring Cloud Gateway限流實戰SpringCloudGateway
- 基於Redis實現Spring Cloud Gateway的動態管理RedisSpringCloudGateway
- Spring Cloud Gateway實戰之二:更多路由配置方式SpringCloudGateway路由
- spring-cloud-gateway靜態路由SpringCloudGateway路由
- spring cloud gateway 原始碼解析(2)動態路由SpringCloudGateway原始碼路由
- Spring Cloud Alibaba微服務實戰SpringCloud微服務
- Spring Cloud Alibaba之NacosSpringCloud
- Spring Cloud Nacos實現動態配置載入的原始碼分析SpringCloud原始碼
- Spring Cloud Gateway實戰之一:初探SpringCloudGateway
- Spring Cloud Alibaba(2)---Nacos概述SpringCloud
- Spring Cloud Gateway 實現 gRpc 代理SpringCloudGatewayRPC
- Spring Cloud Alibaba基礎教程:使用Nacos實現服務註冊與發現SpringCloud
- Spring Cloud Alibaba(四)--Gateway與SentinelSpringCloudGateway
- Spring Cloud Alibaba | Nacos配置管理SpringCloud
- Spring Cloud Alibaba(5)---Nacos(配置中心)SpringCloud
- 微服務閘道器實戰——Spring Cloud Gateway微服務SpringCloudGateway
- Spring Cloud Gateway實戰之五:內建filterSpringCloudGatewayFilter
- Spring Cloud Gateway + Nacos(1)簡單配置SpringCloudGateway
- Spring Cloud Alibaba | Nacos服務註冊與發現SpringCloud
- Spring Cloud Alibaba(4)---Nacos(註冊中心)SpringCloud
- 最全面的改造Zuul閘道器為Spring Cloud Gateway(包含Zuul核心實現和Spring Cloud Gateway核心實現)ZuulSpringCloudGateway
- 【Gin-API系列】實現動態路由分組(七)API路由
- Spring Cloud Gateway實戰之四:內建predicate小結SpringCloudGateway
- Spring Cloud Gateway 整合阿里 Sentinel閘道器限流實戰!SpringCloudGateway阿里
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化SpringCloud持久化
- Spring Cloud Alibaba生態探索:Dubbo、Nacos及Sentinel的完美結合SpringCloud
- Spring Cloud Alibaba:Sentinel實現熔斷與限流SpringCloud
- Spring Cloud Gateway + oauth2 跨域配置實現SpringCloudGatewayOAuth跨域
- Spring Cloud Gateway 整合Eureka路由轉發SpringCloudGateway路由
- Spring Cloud Alibaba微服務生態的基礎實踐SpringCloud微服務
- Spring Cloud Gateway 擴充套件支援動態限流SpringCloudGateway套件
- 微服務閘道器Spring Cloud Gateway的應用實戰微服務SpringCloudGateway
- Spring Cloud Alibaba Nacos 之 灰度釋出(思路分享)SpringCloud
- 13 Spring Cloud Alibaba簡介和Nacos安裝SpringCloud
- spring cloud alibaba springboot nacos 版本對應CloudSpring Boot