【fix sonar】DynamicRouteLoader文件
This commit is contained in:
+40
-35
@@ -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;
|
||||||
|
|
||||||
@@ -59,14 +59,9 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
private static String updateRouteText = "update route : {}";
|
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;
|
||||||
}
|
}
|
||||||
@@ -75,11 +70,11 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
public void init() {
|
public void init() {
|
||||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||||
log.info("初始化路由,dataType:"+ dataType);
|
log.info("初始化路由,dataType:"+ dataType);
|
||||||
if (RouterDataType.nacos.toString().endsWith(dataType)) {
|
if (RouterDataType.NACOS.endsWith(dataType)) {
|
||||||
loadRoutesByNacos();
|
loadRoutesByNacos();
|
||||||
}
|
}
|
||||||
//从数据库加载路由
|
//从数据库加载路由
|
||||||
if (RouterDataType.database.toString().endsWith(dataType)) {
|
if (RouterDataType.DATABASE.endsWith(dataType)) {
|
||||||
loadRoutesByRedis();
|
loadRoutesByRedis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,7 +87,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
*/
|
*/
|
||||||
public Mono<Void> refresh() {
|
public Mono<Void> refresh() {
|
||||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||||
if (!RouterDataType.yml.toString().endsWith(dataType)) {
|
if (!RouterDataType.YML.endsWith(dataType)) {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
return Mono.empty();
|
return Mono.empty();
|
||||||
@@ -110,9 +105,10 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
try {
|
try {
|
||||||
String configInfo = "";
|
String configInfo = "";
|
||||||
if (configService == null) {
|
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)) {
|
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);
|
||||||
@@ -189,17 +185,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
Object predicates = obj.get("predicates");
|
Object predicates = obj.get("predicates");
|
||||||
if (predicates != null) {
|
if (predicates != null) {
|
||||||
JSONArray list = JSON.parseArray(predicates.toString());
|
JSONArray list = JSON.parseArray(predicates.toString());
|
||||||
List<PredicateDefinition> predicateDefinitionList = new ArrayList<>();
|
List<PredicateDefinition> predicateDefinitionList = getPredicateDefinitions(list);
|
||||||
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);
|
|
||||||
}
|
|
||||||
route.setPredicates(predicateDefinitionList);
|
route.setPredicates(predicateDefinitionList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,18 +194,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
JSONArray list = JSON.parseArray(filters.toString());
|
JSONArray list = JSON.parseArray(filters.toString());
|
||||||
List<FilterDefinition> filterDefinitionList = new ArrayList<>();
|
List<FilterDefinition> filterDefinitionList = new ArrayList<>();
|
||||||
if (ObjectUtil.isNotEmpty(list)) {
|
if (ObjectUtil.isNotEmpty(list)) {
|
||||||
for (Object map : list) {
|
addFilterDefinition(list, filterDefinitionList);
|
||||||
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);
|
|
||||||
}
|
|
||||||
route.setFilters(filterDefinitionList);
|
route.setFilters(filterDefinitionList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,6 +203,36 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
|||||||
return ls;
|
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() {
|
// private void loadRoutesByDataBase() {
|
||||||
// List<GatewayRouteVo> routeList = jdbcTemplate.query(SELECT_ROUTES, new RowMapper<GatewayRouteVo>() {
|
// List<GatewayRouteVo> routeList = jdbcTemplate.query(SELECT_ROUTES, new RowMapper<GatewayRouteVo>() {
|
||||||
|
|||||||
Reference in New Issue
Block a user