From 4cbcc257a5c2e25a04c3c0bd3937e136a2ad7378 Mon Sep 17 00:00:00 2001 From: tianwenbo Date: Thu, 9 Mar 2023 17:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=20sonar=E3=80=91=20DynamicRouteLoa?= =?UTF-8?q?der.java=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/loader/DynamicRouteLoader.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/jero-boot/jero-cloud-module/jero-cloud-gateway/src/main/java/com/jero/loader/DynamicRouteLoader.java b/jero-boot/jero-cloud-module/jero-cloud-gateway/src/main/java/com/jero/loader/DynamicRouteLoader.java index 478c55e4..7b314dd7 100644 --- a/jero-boot/jero-cloud-module/jero-cloud-gateway/src/main/java/com/jero/loader/DynamicRouteLoader.java +++ b/jero-boot/jero-cloud-module/jero-cloud-gateway/src/main/java/com/jero/loader/DynamicRouteLoader.java @@ -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 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 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;