Closed Xyk859852 closed 2 years ago
Can you provide details? Eg: configuration content, error stack information.
configuration content :
[
{
"filters": [{
"name":"RewritePath",
"args":{
"regexp":"/system-web-app/(?
From the source code here, it can only be resolved JsonObject
Your application scenario should be dynamic routing. You can refer:
@Autowired
private NacosConfigManager nacosConfigManager;
@Autowired
private RouteDefinitionWriter routeDefinitionWriter;
@Autowired
private ApplicationEventPublisher publisher;
public void refreshRoutes(String configInfo) {
String config = nacosConfigManager.getConfigService().getConfig(dataId, group, 5000);
List<RouteDefinition> routeDefinitions = covertoRouteDefinition(configInfo);
modifyRoutes(routeDefinitions);
this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
private void modifyRoutes(List<RouteDefinition> routeDefinitions) {
routeDefinitions.forEach(routeDefinition -> routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe());
}
I try
Using the method you provided, there will still be a problem and the existing error reporting problem will not be solved
Your application scenario should be dynamic routing. You can refer:
@Autowired private NacosConfigManager nacosConfigManager; @Autowired private RouteDefinitionWriter routeDefinitionWriter; @Autowired private ApplicationEventPublisher publisher; public void refreshRoutes(String configInfo) { String config = nacosConfigManager.getConfigService().getConfig(dataId, group, 5000); List<RouteDefinition> routeDefinitions = covertoRouteDefinition(configInfo); modifyRoutes(routeDefinitions); this.publisher.publishEvent(new RefreshRoutesEvent(this)); } private void modifyRoutes(List<RouteDefinition> routeDefinitions) { routeDefinitions.forEach(routeDefinition -> routeDefinitionWriter.save(Mono.just(routeDefinition)).subscribe()); }
File separate configuration. eg:
dynamic-route.data-id=xxx.json
dynamic-route.group=DEFAULT
You need to pull the configuration information through ConfigService and write it through routeDefinitionWriter#save
This is what I did. My route reads and writes are OK, but there is a problem when Nacos is refreshed at the end. I understand that it has nothing to do with the Gateway route
The piece of code that my Nacos listens to is fine, but there is a problem with the last Nacos refresh itself
Don't use onfiguration Item spring.cloud.nacos.config.shared-configs
. You can use ConfigService to pull configuration from nacos.
它实际上是通过这个JSON对象从根路径一层层解析成为properties格式的key , 如果是数组类型,那么就没有根节点了.
如果是要做网关路由动态发布的话,可以生成这样的格式
{
"spring":
{
"cloud":
{
"gateway":
{
"routes":
[
// 路由配置...
]
}
}
}
}
网关层只需监听这个配置 , Spring Cloud Gateway 官方的 PropertiesRouteDefinitionLocator 会自动实时加载路由变更 , 因为上面这个格式的JSON被解析后对应的yaml就是
spring:
cloud:
gateway:
routes:
- 路由配置...
使用该方式需要注意Nacos上是否已经有其他配置文件也配置了 "spring.cloud.gateway.routes" (会相互覆盖) , 解决方案也比较简单 , 监听对应的配置文件 , 参考
@Component
public class CustomPropertiesRouteDefinitionLocator implements RouteDefinitionLocator {
private final Environment environment;
public CustomPropertiesRouteDefinitionLocator(Environment environment) {
this.environment = environment;
}
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
List<RouteDefinition> routes = Binder.get(this.environment)
.bindOrCreate("spring.cloud.gateway.custom-routes", GatewayProperties.class)
.getRoutes();
return Flux.fromIterable(routes);
}
}
When using SpringCloudAlibaba, Nacos configuration center, using sharedConfigs or extensionConfigs to load the configuration, when the configuration is in JsonArray format, is there any exception when the configuration is refreshed? For example, can the parse object only be a JsonObject Object not a JSONArray??
The corresponding version is SpringCloudAlibaba: 2021.0.1.0 SpringCloud: 2021.0.1, SpringBoot: 2.6.8