【fix sonar】 DynamicRouteLoader.java文件
This commit is contained in:
+19
-10
@@ -48,7 +48,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
private InMemoryRouteDefinitionRepository repository;
|
||||
// private InMemoryRouteDefinitionRepository repository;
|
||||
|
||||
private DynamicRouteService dynamicRouteService;
|
||||
|
||||
@@ -56,10 +56,17 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
private static String updateRouteText = "update route : {}";
|
||||
|
||||
public DynamicRouteLoader(InMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
/*public DynamicRouteLoader(InMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
this.repository = repository;
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisUtil = redisUtil;
|
||||
}*/
|
||||
public DynamicRouteLoader(DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisUtil = redisUtil;
|
||||
}
|
||||
@@ -100,11 +107,12 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
private void loadRoutesByNacos() {
|
||||
List<RouteDefinition> routes = Lists.newArrayList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
}
|
||||
try {
|
||||
String configInfo = configService.getConfig(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.DEFAULT_TIMEOUT);
|
||||
String configInfo = "";
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
}
|
||||
else configInfo = configService.getConfig(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.DEFAULT_TIMEOUT);
|
||||
if (StringUtils.isNotBlank(configInfo)) {
|
||||
log.info("获取网关当前配置:\r\n{}", configInfo);
|
||||
routes = JSON.parseArray(configInfo, RouteDefinition.class);
|
||||
@@ -114,7 +122,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (RouteDefinition definition : routes) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
log.info(updateRouteText, definition.toString());
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
@@ -144,7 +152,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
}
|
||||
}
|
||||
for (MyRouteDefinition definition : routes) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
log.info(updateRouteText, definition.toString());
|
||||
Integer status=definition.getStatus();
|
||||
if(status.equals(0)){
|
||||
dynamicRouteService.delete(definition.getId());
|
||||
@@ -278,7 +286,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
log.info("进行网关更新:\n\r{}", configInfo);
|
||||
List<RouteDefinition> definitionList = JSON.parseArray(configInfo, RouteDefinition.class);
|
||||
for (RouteDefinition definition : definitionList) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
log.info(updateRouteText, definition.toString());
|
||||
dynamicRouteService.update(definition);
|
||||
}
|
||||
}
|
||||
@@ -304,7 +312,8 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("serverAddr", GatewayRoutersConfiguration.SERVER_ADDR);
|
||||
properties.setProperty("namespace", GatewayRoutersConfiguration.NAMESPACE);
|
||||
return configService = NacosFactory.createConfigService(properties);
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
return configService;
|
||||
} catch (Exception e) {
|
||||
log.error("创建ConfigService异常", e);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user