format code

This commit is contained in:
Francis Dong
2020-12-08 12:35:58 +08:00
parent cedb2bd2b1
commit 38a60fc00e

View File

@@ -50,10 +50,9 @@ import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/** /**
* *
* @author francis * @author francis
@@ -62,21 +61,21 @@ import java.util.function.Function;
*/ */
@Component @Component
public class ConfigLoader { public class ConfigLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class);
/** /**
* 聚合配置存放Hash的Key * 聚合配置存放Hash的Key
*/ */
private static final String AGGREGATE_HASH_KEY = "fizz_aggregate_config"; private static final String AGGREGATE_HASH_KEY = "fizz_aggregate_config";
private static Map<String, String> aggregateResources = null; private static Map<String, String> aggregateResources = null;
private static Map<String, ConfigInfo> resourceKey2ConfigInfoMap = null; private static Map<String, ConfigInfo> resourceKey2ConfigInfoMap = null;
private static Map<String, String> aggregateId2ResourceKeyMap = null; private static Map<String, String> aggregateId2ResourceKeyMap = null;
@Resource @Resource
private AppConfigProperties appConfigProperties; private AppConfigProperties appConfigProperties;
@Resource(name = AGGREGATE_REACTIVE_REDIS_TEMPLATE) @Resource(name = AGGREGATE_REACTIVE_REDIS_TEMPLATE)
private ReactiveStringRedisTemplate reactiveStringRedisTemplate; private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
@@ -95,11 +94,11 @@ public class ConfigLoader {
clientInputConfig.setHeaders(cfgNode.select("$.headers").toObject(Map.class)); clientInputConfig.setHeaders(cfgNode.select("$.headers").toObject(Map.class));
clientInputConfig.setMethod(cfgNode.select("$.method").getString()); clientInputConfig.setMethod(cfgNode.select("$.method").getString());
clientInputConfig.setPath(cfgNode.select("$.path").getString()); clientInputConfig.setPath(cfgNode.select("$.path").getString());
if(clientInputConfig.getPath().startsWith(TEST_PATH_PREFIX)) { if (clientInputConfig.getPath().startsWith(TEST_PATH_PREFIX)) {
// always enable debug for testing // always enable debug for testing
clientInputConfig.setDebug(true); clientInputConfig.setDebug(true);
}else { } else {
if(cfgNode.select("$.debug") != null) { if (cfgNode.select("$.debug") != null) {
clientInputConfig.setDebug(cfgNode.select("$.debug").getBoolean()); clientInputConfig.setDebug(cfgNode.select("$.debug").getBoolean());
} }
} }
@@ -122,7 +121,7 @@ public class ConfigLoader {
for (Map<String, Object> stepConfig : stepConfigs) { for (Map<String, Object> stepConfig : stepConfigs) {
// set the specified env URL // set the specified env URL
this.handleRequestURL(stepConfig); this.handleRequestURL(stepConfig);
Step step = new Step.Builder().read(stepConfig); Step step = new Step.Builder().read(stepConfig);
step.setName((String) stepConfig.get("name")); step.setName((String) stepConfig.get("name"));
if (stepConfig.get("stop") != null) { if (stepConfig.get("stop") != null) {
@@ -162,13 +161,13 @@ public class ConfigLoader {
} }
return aggregateResources.get(resourceKey); return aggregateResources.get(resourceKey);
} }
private void handleRequestURL(Map<String, Object> stepConfig) { private void handleRequestURL(Map<String, Object> stepConfig) {
List<Object> requests = (List<Object>) stepConfig.get("requests"); List<Object> requests = (List<Object>) stepConfig.get("requests");
for (Object obj : requests) { for (Object obj : requests) {
Map<String, Object> request = (Map<String, Object>) obj; Map<String, Object> request = (Map<String, Object>) obj;
String envUrl = (String) request.get(appConfigProperties.getEnv() + "Url"); String envUrl = (String) request.get(appConfigProperties.getEnv() + "Url");
if(!StringUtils.isEmpty(envUrl)) { if (!StringUtils.isEmpty(envUrl)) {
request.put("url", request.get(appConfigProperties.getEnv() + "Url")); request.put("url", request.get(appConfigProperties.getEnv() + "Url"));
} }
} }
@@ -178,7 +177,7 @@ public class ConfigLoader {
public synchronized void init() throws Exception { public synchronized void init() throws Exception {
if (aggregateResources == null) { if (aggregateResources == null) {
aggregateResources = new ConcurrentHashMap<>(1024); aggregateResources = new ConcurrentHashMap<>(1024);
resourceKey2ConfigInfoMap = new ConcurrentHashMap<>(1024); resourceKey2ConfigInfoMap = new ConcurrentHashMap<>(1024);
aggregateId2ResourceKeyMap = new ConcurrentHashMap<>(1024); aggregateId2ResourceKeyMap = new ConcurrentHashMap<>(1024);
} }
@@ -204,7 +203,7 @@ public class ConfigLoader {
}); });
} }
} }
public synchronized void addConfig(String configStr) { public synchronized void addConfig(String configStr) {
if (aggregateResources == null) { if (aggregateResources == null) {
try { try {
@@ -221,7 +220,7 @@ public class ConfigLoader {
String configId = cfgNode.select("$.id").getString(); String configId = cfgNode.select("$.id").getString();
String configName = cfgNode.select("$.name").getString(); String configName = cfgNode.select("$.name").getString();
long version = cfgNode.select("$.version").getLong(); long version = cfgNode.select("$.version").getLong();
LOGGER.debug("add aggregation config, key={} config={}", resourceKey, configStr); LOGGER.debug("add aggregation config, key={} config={}", resourceKey, configStr);
if (StringUtils.hasText(configId)) { if (StringUtils.hasText(configId)) {
String existResourceKey = aggregateId2ResourceKeyMap.get(configId); String existResourceKey = aggregateId2ResourceKeyMap.get(configId);
@@ -244,7 +243,7 @@ public class ConfigLoader {
JSONArray idArray = JSON.parseArray(configIds); JSONArray idArray = JSON.parseArray(configIds);
idArray.forEach(it -> { idArray.forEach(it -> {
String configId = (String) it; String configId = (String) it;
String existResourceKey =aggregateId2ResourceKeyMap.get(configId); String existResourceKey = aggregateId2ResourceKeyMap.get(configId);
if (StringUtils.hasText(existResourceKey)) { if (StringUtils.hasText(existResourceKey)) {
LOGGER.debug("delete aggregation config: {}", existResourceKey); LOGGER.debug("delete aggregation config: {}", existResourceKey);
aggregateResources.remove(existResourceKey); aggregateResources.remove(existResourceKey);
@@ -264,7 +263,7 @@ public class ConfigLoader {
} }
} }
String key = method.toUpperCase() + ":" + path; String key = method.toUpperCase() + ":" + path;
if(aggregateResources.containsKey(key) && aggregateResources.get(key) != null) { if (aggregateResources.containsKey(key) && aggregateResources.get(key) != null) {
String configStr = aggregateResources.get(key); String configStr = aggregateResources.get(key);
Input input = null; Input input = null;
Pipeline pipeline = null; Pipeline pipeline = null;
@@ -282,6 +281,7 @@ public class ConfigLoader {
} }
return null; return null;
} }
private ConfigInfo buildConfigInfo(String configId, String configName, String method, String path, long version) { private ConfigInfo buildConfigInfo(String configId, String configName, String method, String path, long version) {
String serviceName = this.extractServiceName(path); String serviceName = this.extractServiceName(path);
ConfigInfo configInfo = new ConfigInfo(); ConfigInfo configInfo = new ConfigInfo();
@@ -294,26 +294,27 @@ public class ConfigLoader {
return configInfo; return configInfo;
} }
private static final String FORMAL_PATH_PREFIX = "/proxy/"; private static final String FORMAL_PATH_PREFIX = "/proxy/";
private static final int FORMAL_PATH_SERVICE_NAME_START_INDEX = 7; private static final int FORMAL_PATH_SERVICE_NAME_START_INDEX = 7;
private static final String TEST_PATH_PREFIX = "/proxytest/"; private static final String TEST_PATH_PREFIX = "/proxytest/";
private static final int TEST_PATH_SERVICE_NAME_START_INDEX = 11; private static final int TEST_PATH_SERVICE_NAME_START_INDEX = 11;
private String extractServiceName(String path) { private String extractServiceName(String path) {
if (path != null) { if (path != null) {
if (path.startsWith(FORMAL_PATH_PREFIX)) { if (path.startsWith(FORMAL_PATH_PREFIX)) {
int endIndex = path.indexOf(FORWARD_SLASH, FORMAL_PATH_SERVICE_NAME_START_INDEX); int endIndex = path.indexOf(FORWARD_SLASH, FORMAL_PATH_SERVICE_NAME_START_INDEX);
if (endIndex > FORMAL_PATH_SERVICE_NAME_START_INDEX) { if (endIndex > FORMAL_PATH_SERVICE_NAME_START_INDEX) {
return path.substring(FORMAL_PATH_SERVICE_NAME_START_INDEX, endIndex); return path.substring(FORMAL_PATH_SERVICE_NAME_START_INDEX, endIndex);
} }
} else if (path.startsWith(TEST_PATH_PREFIX)) { } else if (path.startsWith(TEST_PATH_PREFIX)) {
int endIndex = path.indexOf(FORWARD_SLASH, TEST_PATH_SERVICE_NAME_START_INDEX); int endIndex = path.indexOf(FORWARD_SLASH, TEST_PATH_SERVICE_NAME_START_INDEX);
if (endIndex > TEST_PATH_SERVICE_NAME_START_INDEX) { if (endIndex > TEST_PATH_SERVICE_NAME_START_INDEX) {
return path.substring(TEST_PATH_SERVICE_NAME_START_INDEX, endIndex); return path.substring(TEST_PATH_SERVICE_NAME_START_INDEX, endIndex);
} }
} }
} }
return null; return null;
} }
public static class ConfigInfo implements Serializable { public static class ConfigInfo implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -353,12 +354,9 @@ public class ConfigLoader {
return false; return false;
} }
ConfigInfo that = (ConfigInfo) o; ConfigInfo that = (ConfigInfo) o;
return Objects.equals(configId, that.configId) && return Objects.equals(configId, that.configId) && Objects.equals(configName, that.configName)
Objects.equals(configName, that.configName) && && Objects.equals(serviceName, that.serviceName) && Objects.equals(method, that.method)
Objects.equals(serviceName, that.serviceName) && && Objects.equals(path, that.path) && Objects.equals(version, that.version);
Objects.equals(method, that.method) &&
Objects.equals(path, that.path) &&
Objects.equals(version, that.version);
} }
@Override @Override