Python 官方文档:入门教程 => 点击学习
目录背景路由的加载实现动态路由背景 在之前我们了解的spring cloud Gateway配置路由方式有两种方式 通过配置文件 spring: cloud: gatew
在之前我们了解的spring cloud Gateway
配置路由方式有两种方式
spring:
cloud:
gateway:
routes:
- id: test
predicates:
- Path=/ms/test
private void refreshGatewayProperties(ConfiGChangeEvent changeEvent) {
log.info("Refreshing GatewayProperties!");
preDestroyGatewayProperties(changeEvent);
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
refreshGatewayRouteDefinition();
log.info("GatewayProperties refreshed!");
}
private synchronized void preDestroyGatewayProperties(ConfigChangeEvent changeEvent) {
log.info("Pre Destroy GatewayProperties!");
final boolean needClearRoutes = this.checkNeedClear(changeEvent, ID_PATTERN, this.gatewayProperties.getRoutes()
.size());
if (needClearRoutes) {
this.gatewayProperties.setRoutes(new ArrayList<>());
}
final boolean needClearDefaultFilters = this.checkNeedClear(changeEvent, DEFAULT_FILTER_PATTERN, this.gatewayProperties.getDefaultFilters()
.size());
if (needClearDefaultFilters) {
this.gatewayProperties.setDefaultFilters(new ArrayList<>());
}
log.info("Pre Destroy GatewayProperties finished!");
}
private void refreshGatewayRouteDefinition() {
log.info("Refreshing Gateway RouteDefinition!");
this.publisher.publishEvent(new RefreshRoutesEvent(this));
log.info("Gateway RouteDefinition refreshed!");
}
private boolean checkNeedClear(ConfigChangeEvent changeEvent, String pattern, int existSize) {
return changeEvent.changedKeys().stream().filter(key -> key.matches(pattern))
.filter(key -> {
ConfigChange change = changeEvent.getChange(key);
return PropertyChangeType.DELETED.equals(change.getChangeType());
}).count() == existSize;
}
}
然后我们在apollo添加namespace
:route.yml
配置内容如下:
spring:
cloud:
gateway:
routes:
- id: test
predicates:
- Path=/ms/test/*
filters:
- StripPrefix=2
uri: Http://localhost:9000
然后我们可以通过访问地址: http:localhost:8080/ms/test/health
看删除后是否是404,加上后是否可以正常动态路由
值得注意的是上面@ApolloConfigChangeListener
中如果没有添加新的namespace
,value
可以不用填写,如果配置文件是yml配置文件,在监听的时候需要指定文件后缀
以上就是Spring Cloud Gateway动态路由Apollo实现详解的详细内容,更多关于Spring Cloud Gateway Apollo的资料请关注编程网其它相关文章!
--结束END--
本文标题: Spring Cloud Gateway动态路由Apollo实现详解
本文链接: https://www.lsjlt.com/news/170409.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0