【fix sonar】DynamicRouteLoader文件

This commit is contained in:
梁琦涛
2023-03-16 14:25:51 +08:00
parent e31e623d63
commit 3608e1f014
@@ -48,7 +48,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
// private InMemoryRouteDefinitionRepository repository;
private InMemoryRouteDefinitionRepository repository;
private DynamicRouteService dynamicRouteService;
@@ -59,14 +59,9 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
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;
}
@@ -75,11 +70,11 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
public void init() {
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
log.info("初始化路由,dataType"+ dataType);
if (RouterDataType.nacos.toString().endsWith(dataType)) {
if (RouterDataType.NACOS.endsWith(dataType)) {
loadRoutesByNacos();
}
//从数据库加载路由
if (RouterDataType.database.toString().endsWith(dataType)) {
if (RouterDataType.DATABASE.endsWith(dataType)) {
loadRoutesByRedis();
}
}
@@ -92,7 +87,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
*/
public Mono<Void> refresh() {
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
if (!RouterDataType.yml.toString().endsWith(dataType)) {
if (!RouterDataType.YML.endsWith(dataType)) {
this.init();
}
return Mono.empty();
@@ -110,9 +105,10 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
try {
String configInfo = "";
if (configService == null) {
log.warn("initConfigService fail");
log.warn("initConfigService fail");
}else {
configInfo = configService.getConfig(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.DEFAULT_TIMEOUT);
}
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);
@@ -189,17 +185,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
Object predicates = obj.get("predicates");
if (predicates != null) {
JSONArray list = JSON.parseArray(predicates.toString());
List<PredicateDefinition> predicateDefinitionList = new ArrayList<>();
for (Object map : list) {
JSONObject json = (JSONObject) map;
PredicateDefinition predicateDefinition = new PredicateDefinition();
predicateDefinition.setName(json.getString("name"));
JSONArray jsonArray = json.getJSONArray("args");
for (int j = 0; j < jsonArray.size(); j++) {
predicateDefinition.addArg("_genkey" + j, jsonArray.get(j).toString());
}
predicateDefinitionList.add(predicateDefinition);
}
List<PredicateDefinition> predicateDefinitionList = getPredicateDefinitions(list);
route.setPredicates(predicateDefinitionList);
}
@@ -208,18 +194,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
JSONArray list = JSON.parseArray(filters.toString());
List<FilterDefinition> filterDefinitionList = new ArrayList<>();
if (ObjectUtil.isNotEmpty(list)) {
for (Object map : list) {
JSONObject json = (JSONObject) map;
JSONArray jsonArray = json.getJSONArray("args");
String name = json.getString("name");
FilterDefinition filterDefinition = new FilterDefinition();
for (Object o : jsonArray) {
JSONObject params = (JSONObject) o;
filterDefinition.addArg(params.getString("key"), params.get("value").toString());
}
filterDefinition.setName(name);
filterDefinitionList.add(filterDefinition);
}
addFilterDefinition(list, filterDefinitionList);
route.setFilters(filterDefinitionList);
}
}
@@ -228,6 +203,36 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
return ls;
}
private static void addFilterDefinition(JSONArray list, List<FilterDefinition> filterDefinitionList) {
for (Object map : list) {
JSONObject json = (JSONObject) map;
JSONArray jsonArray = json.getJSONArray("args");
String name = json.getString("name");
FilterDefinition filterDefinition = new FilterDefinition();
for (Object o : jsonArray) {
JSONObject params = (JSONObject) o;
filterDefinition.addArg(params.getString("key"), params.get("value").toString());
}
filterDefinition.setName(name);
filterDefinitionList.add(filterDefinition);
}
}
private static List<PredicateDefinition> getPredicateDefinitions(JSONArray list) {
List<PredicateDefinition> predicateDefinitionList = new ArrayList<>();
for (Object map : list) {
JSONObject json = (JSONObject) map;
PredicateDefinition predicateDefinition = new PredicateDefinition();
predicateDefinition.setName(json.getString("name"));
JSONArray jsonArray = json.getJSONArray("args");
for (int j = 0; j < jsonArray.size(); j++) {
predicateDefinition.addArg("_genkey" + j, jsonArray.get(j).toString());
}
predicateDefinitionList.add(predicateDefinition);
}
return predicateDefinitionList;
}
// private void loadRoutesByDataBase() {
// List<GatewayRouteVo> routeList = jdbcTemplate.query(SELECT_ROUTES, new RowMapper<GatewayRouteVo>() {