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