This is an automated email from the ASF dual-hosted git repository.
lw1243925457 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new b33bf2b [refactor]<register-client> clean code for register client
(#2424)
b33bf2b is described below
commit b33bf2be48629859ea7c22770f1510863e817f45
Author: kaitoShy <[email protected]>
AuthorDate: Sun Nov 21 07:37:50 2021 +0800
[refactor]<register-client> clean code for register client (#2424)
* clean code
* [refactor]<register-client> clean code,Remove warning codes
---
.../apache/shenyu/common/constant/Constants.java | 12 +++++++-
.../common/constant/DefaultPathConstants.java | 19 ++++++------
.../org/apache/shenyu/common/utils/Md5Utils.java | 1 +
.../consul/ConsulClientRegisterRepository.java | 35 ++++++++++++++--------
.../client/etcd/EtcdClientRegisterRepository.java | 17 +++++++----
.../shenyu-register-client-http/pom.xml | 6 ++++
.../client/http/HttpClientRegisterRepository.java | 11 +++----
.../register/client/http/utils/RegisterUtils.java | 5 +++-
.../nacos/NacosClientRegisterRepository.java | 10 +++++--
.../ZookeeperClientRegisterRepository.java | 12 ++++++--
.../common/path/RegisterPathConstants.java | 4 +--
.../consul/ConsulServerRegisterRepository.java | 3 +-
.../server/consul/ShenyuConsulConfigWatch.java | 14 ++++-----
.../server/etcd/EtcdServerRegisterRepository.java | 18 ++++++-----
.../nacos/NacosServerRegisterRepository.java | 3 +-
.../ZookeeperServerRegisterRepository.java | 11 ++++---
16 files changed, 116 insertions(+), 65 deletions(-)
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
index ec245d2..8e58e9b 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java
@@ -201,11 +201,21 @@ public interface Constants {
* The constant REWRITE_URI.
*/
String REWRITE_URI = "rewrite_uri";
-
+
+ /**
+ * The constant URI.
+ */
+ String URI = "uri";
+
/**
* The constant LINE_SEPARATOR.
*/
String LINE_SEPARATOR = System.getProperty("line.separator");
+
+ /**
+ * The constant URL_SEPARATOR.
+ */
+ String PATH_SEPARATOR = "/";
/**
* hystrix withExecutionIsolationSemaphoreMaxConcurrentRequests.
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/DefaultPathConstants.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/DefaultPathConstants.java
index a98bbac..119a748 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/DefaultPathConstants.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/DefaultPathConstants.java
@@ -18,7 +18,7 @@
package org.apache.shenyu.common.constant;
/**
- * ZkPathConstants.
+ * DefaultPathConstants.
*/
public final class DefaultPathConstants implements Constants {
@@ -51,7 +51,6 @@ public final class DefaultPathConstants implements Constants {
*/
public static final String META_DATA = PRE_FIX + "/metaData";
- private static final String SEPARATOR = "/";
/**
* acquire app_auth_path.
@@ -60,7 +59,7 @@ public final class DefaultPathConstants implements Constants {
* @return app_auth_path string
*/
public static String buildAppAuthPath(final String appKey) {
- return String.join(SEPARATOR, APP_AUTH_PARENT, appKey);
+ return String.join(PATH_SEPARATOR, APP_AUTH_PARENT, appKey);
}
/**
@@ -70,7 +69,7 @@ public final class DefaultPathConstants implements Constants {
* @return the string
*/
public static String buildMetaDataPath(final String path) {
- return String.join(SEPARATOR, META_DATA, path);
+ return String.join(PATH_SEPARATOR, META_DATA, path);
}
/**
@@ -79,7 +78,7 @@ public final class DefaultPathConstants implements Constants {
* @return zk path for plugin
*/
public static String buildPluginParentPath() {
- return String.join(SEPARATOR, PLUGIN_PARENT);
+ return String.join(PATH_SEPARATOR, PLUGIN_PARENT);
}
/**
@@ -89,7 +88,7 @@ public final class DefaultPathConstants implements Constants {
* @return zk path for plugin
*/
public static String buildPluginPath(final String pluginName) {
- return String.join(SEPARATOR, PLUGIN_PARENT, pluginName);
+ return String.join(PATH_SEPARATOR, PLUGIN_PARENT, pluginName);
}
/**
@@ -99,7 +98,7 @@ public final class DefaultPathConstants implements Constants {
* @return zk path for selector
*/
public static String buildSelectorParentPath(final String pluginName) {
- return String.join(SEPARATOR, SELECTOR_PARENT, pluginName);
+ return String.join(PATH_SEPARATOR, SELECTOR_PARENT, pluginName);
}
/**
@@ -110,7 +109,7 @@ public final class DefaultPathConstants implements
Constants {
* @return zk full path for selector
*/
public static String buildSelectorRealPath(final String pluginName, final
String selectorId) {
- return String.join(SEPARATOR, SELECTOR_PARENT, pluginName, selectorId);
+ return String.join(PATH_SEPARATOR, SELECTOR_PARENT, pluginName,
selectorId);
}
/**
@@ -120,7 +119,7 @@ public final class DefaultPathConstants implements
Constants {
* @return zk rule parent path.
*/
public static String buildRuleParentPath(final String pluginName) {
- return String.join(SEPARATOR, RULE_PARENT, pluginName);
+ return String.join(PATH_SEPARATOR, RULE_PARENT, pluginName);
}
/**
@@ -132,7 +131,7 @@ public final class DefaultPathConstants implements
Constants {
* @return /shenyu/rule/pluginName/selectorId-ruleId
*/
public static String buildRulePath(final String pluginName, final String
selectorId, final String ruleId) {
- return String.join(SEPARATOR, buildRuleParentPath(pluginName),
String.join(SELECTOR_JOIN_RULE, selectorId, ruleId));
+ return String.join(PATH_SEPARATOR, buildRuleParentPath(pluginName),
String.join(SELECTOR_JOIN_RULE, selectorId, ruleId));
}
}
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/Md5Utils.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/Md5Utils.java
index 86f580a..a406705 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/Md5Utils.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/Md5Utils.java
@@ -40,6 +40,7 @@ public class Md5Utils {
*
* @param src the src
* @param charset the charset
+ *
* @return the string
*/
private static String md5(final String src, final String charset) {
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-consul/src/main/java/org/apache/shenyu/register/client/consul/ConsulClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-consul/src/main/java/org/apache/shenyu/register/client/consul/ConsulClientRegisterRepository.java
index 291b655..0a068ee 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-consul/src/main/java/org/apache/shenyu/register/client/consul/ConsulClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-consul/src/main/java/org/apache/shenyu/register/client/consul/ConsulClientRegisterRepository.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.register.client.consul;
import com.ecwid.consul.v1.kv.KeyValueClient;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.ContextPathUtils;
import org.apache.shenyu.common.utils.GsonUtils;
@@ -29,20 +30,28 @@ import
org.apache.shenyu.register.common.path.RegisterPathConstants;
import org.apache.shenyu.spi.Join;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
+import java.util.Objects;
+
+import static org.apache.shenyu.common.constant.Constants.PATH_SEPARATOR;
+import static
org.apache.shenyu.common.constant.DefaultPathConstants.SELECTOR_JOIN_RULE;
+
@Join
public class ConsulClientRegisterRepository implements
ShenyuClientRegisterRepository {
private static final Logger LOGGER =
LoggerFactory.getLogger(ConsulClientRegisterRepository.class);
- @Autowired
- private ConsulRegistration consulRegistration;
-
- @Autowired
- private KeyValueClient keyValueClient;
-
+ private final ConsulRegistration consulRegistration;
+
+ private final KeyValueClient keyValueClient;
+
+ public ConsulClientRegisterRepository(final ConsulRegistration
consulRegistration,
+ final KeyValueClient keyValueClient)
{
+ this.consulRegistration = consulRegistration;
+ this.keyValueClient = keyValueClient;
+ }
+
@Override
public void persistInterface(final MetaDataRegisterDTO metadata) {
String rpcType = metadata.getRpcType();
@@ -72,18 +81,20 @@ public class ConsulClientRegisterRepository implements
ShenyuClientRegisterRepos
}
private void registerURI(final URIRegisterDTO metadata) {
- consulRegistration.getService().getMeta().put("uri",
GsonUtils.getInstance().toJson(metadata));
+ consulRegistration.getService().getMeta().put(Constants.URI,
GsonUtils.getInstance().toJson(metadata));
}
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
String nodeName;
String rpcType = metadata.getRpcType();
- if (RpcTypeEnum.HTTP.getName().equals(rpcType)
- || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
- nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
+ if (Objects.equals(RpcTypeEnum.HTTP.getName(), rpcType)
+ || Objects.equals(RpcTypeEnum.SPRING_CLOUD.getName(),
rpcType)) {
+ nodeName = String.join(SELECTOR_JOIN_RULE,
+ metadata.getContextPath(),
+ metadata.getRuleName().replace(PATH_SEPARATOR,
SELECTOR_JOIN_RULE));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
}
- return nodeName.startsWith("/") ? nodeName.substring(1) : nodeName;
+ return nodeName.startsWith(PATH_SEPARATOR) ? nodeName.substring(1) :
nodeName;
}
}
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-etcd/src/main/java/org/apache/shenyu/register/client/etcd/EtcdClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-etcd/src/main/java/org/apache/shenyu/register/client/etcd/EtcdClientRegisterRepository.java
index dc77333..55c7e90 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-etcd/src/main/java/org/apache/shenyu/register/client/etcd/EtcdClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-etcd/src/main/java/org/apache/shenyu/register/client/etcd/EtcdClientRegisterRepository.java
@@ -17,6 +17,7 @@
package org.apache.shenyu.register.client.etcd;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.ContextPathUtils;
import org.apache.shenyu.common.utils.GsonUtils;
@@ -30,8 +31,13 @@ import org.apache.shenyu.spi.Join;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Objects;
import java.util.Properties;
+import static org.apache.shenyu.common.constant.Constants.PATH_SEPARATOR;
+import static
org.apache.shenyu.common.constant.DefaultPathConstants.SELECTOR_JOIN_RULE;
+
+
/**
* etcd register repository.
*/
@@ -101,19 +107,20 @@ public class EtcdClientRegisterRepository implements
ShenyuClientRegisterReposit
private String buildURINodeName(final URIRegisterDTO registerDTO) {
String host = registerDTO.getHost();
int port = registerDTO.getPort();
- return String.join(":", host, Integer.toString(port));
+ return String.join(Constants.COLONS, host, Integer.toString(port));
}
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
String nodeName;
String rpcType = metadata.getRpcType();
- if (RpcTypeEnum.HTTP.getName().equals(rpcType)
- || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
- nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
+ if (Objects.equals(RpcTypeEnum.HTTP.getName(), rpcType)
+ || Objects.equals(RpcTypeEnum.SPRING_CLOUD.getName(),
rpcType)) {
+ nodeName = String.join(SELECTOR_JOIN_RULE,
metadata.getContextPath(),
+ metadata.getRuleName().replace(PATH_SEPARATOR,
SELECTOR_JOIN_RULE));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
}
- return nodeName.startsWith("/") ? nodeName.substring(1) : nodeName;
+ return nodeName.startsWith(PATH_SEPARATOR) ? nodeName.substring(1) :
nodeName;
}
}
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/pom.xml
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/pom.xml
index 67ada64..f1062cb 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/pom.xml
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/pom.xml
@@ -28,6 +28,12 @@
<dependencies>
<dependency>
<groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-common</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-register-client-api</artifactId>
<version>${project.version}</version>
</dependency>
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/HttpClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/HttpClientRegisterRepository.java
index c9c6a9f..69596d8 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/HttpClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/HttpClientRegisterRepository.java
@@ -19,7 +19,8 @@ package org.apache.shenyu.register.client.http;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
-import com.google.gson.Gson;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
import org.apache.shenyu.register.client.http.utils.RegisterUtils;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
@@ -45,12 +46,8 @@ public class HttpClientRegisterRepository implements
ShenyuClientRegisterReposit
private static final String URI_PATH = "/shenyu-client/register-uri";
- private static final String URI_TYPE = "uri";
-
private List<String> serverList;
- private Gson gson = new Gson();
-
@Override
public void init(final ShenyuRegisterCenterConfig config) {
this.serverList =
Lists.newArrayList(Splitter.on(",").split(config.getServerLists()));
@@ -63,7 +60,7 @@ public class HttpClientRegisterRepository implements
ShenyuClientRegisterReposit
*/
@Override
public void persistURI(final URIRegisterDTO registerDTO) {
- doRegister(registerDTO, URI_PATH, URI_TYPE);
+ doRegister(registerDTO, URI_PATH, Constants.URI);
}
@Override
@@ -74,7 +71,7 @@ public class HttpClientRegisterRepository implements
ShenyuClientRegisterReposit
private <T> void doRegister(final T t, final String path, final String
type) {
for (String server : serverList) {
try {
- RegisterUtils.doRegister(gson.toJson(t), server + path, type);
+ RegisterUtils.doRegister(GsonUtils.getInstance().toJson(t),
server + path, type);
return;
} catch (Exception e) {
LOGGER.error("register admin url :{} is fail, will retry",
server);
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/RegisterUtils.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/RegisterUtils.java
index 93c9664..9687b68 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/RegisterUtils.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/RegisterUtils.java
@@ -21,6 +21,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.util.Objects;
+
+import static org.apache.shenyu.common.constant.Constants.SUCCESS;
/**
* RegisterUtils.
@@ -42,7 +45,7 @@ public final class RegisterUtils {
*/
public static void doRegister(final String json, final String url, final
String type) throws IOException {
String result = OkHttpTools.getInstance().post(url, json);
- if ("success".equals(result)) {
+ if (Objects.equals(SUCCESS, result)) {
LOGGER.info("{} client register success: {} ", type, json);
} else {
LOGGER.error("{} client register error: {} ", type, json);
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-nacos/src/main/java/org/apache/shenyu/register/client/nacos/NacosClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-nacos/src/main/java/org/apache/shenyu/register/client/nacos/NacosClientRegisterRepository.java
index a74527c..785f957 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-nacos/src/main/java/org/apache/shenyu/register/client/nacos/NacosClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-nacos/src/main/java/org/apache/shenyu/register/client/nacos/NacosClientRegisterRepository.java
@@ -50,6 +50,10 @@ import java.util.concurrent.ConcurrentLinkedQueue;
public class NacosClientRegisterRepository implements
ShenyuClientRegisterRepository {
private static final Logger LOGGER =
LoggerFactory.getLogger(NacosClientRegisterRepository.class);
+
+ private static final String NAMESPACE = "nacosNameSpace";
+
+ private static final String URI_META_DATA = "uriMetadata";
private ConfigService configService;
@@ -65,8 +69,8 @@ public class NacosClientRegisterRepository implements
ShenyuClientRegisterReposi
Properties properties = config.getProps();
Properties nacosProperties = new Properties();
nacosProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
- String nameSpace = "nacosNameSpace";
- nacosProperties.put(PropertyKeyConst.NAMESPACE,
properties.getProperty(nameSpace));
+
+ nacosProperties.put(PropertyKeyConst.NAMESPACE,
properties.getProperty(NAMESPACE));
// the nacos authentication username
nacosProperties.put(PropertyKeyConst.USERNAME,
properties.getProperty(PropertyKeyConst.USERNAME, ""));
// the nacos authentication password
@@ -129,7 +133,7 @@ public class NacosClientRegisterRepository implements
ShenyuClientRegisterReposi
instance.setPort(port);
Map<String, String> metadataMap = new HashMap<>();
metadataMap.put(Constants.CONTEXT_PATH, contextPath);
- metadataMap.put("uriMetadata",
GsonUtils.getInstance().toJson(registerDTO));
+ metadataMap.put(URI_META_DATA,
GsonUtils.getInstance().toJson(registerDTO));
instance.setMetadata(metadataMap);
String serviceName =
RegisterPathConstants.buildServiceInstancePath(rpcType);
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-zookeeper/src/main/java/org/apache/shenyu/register/client/zookeeper/ZookeeperClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-zookeeper/src/main/java/org/apache/shenyu/register/client/zookeeper/ZookeeperClientRegisterRepository.java
index 7036bf1..52d206f 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-zookeeper/src/main/java/org/apache/shenyu/register/client/zookeeper/ZookeeperClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-zookeeper/src/main/java/org/apache/shenyu/register/client/zookeeper/ZookeeperClientRegisterRepository.java
@@ -19,6 +19,8 @@ package org.apache.shenyu.register.client.zookeeper;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.constant.DefaultPathConstants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.ContextPathUtils;
import org.apache.shenyu.common.utils.GsonUtils;
@@ -36,6 +38,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import static org.apache.shenyu.common.constant.Constants.PATH_SEPARATOR;
+
/**
* The type Zookeeper client register repository.
*/
@@ -115,7 +119,7 @@ public class ZookeeperClientRegisterRepository implements
ShenyuClientRegisterRe
private String buildURINodeName(final URIRegisterDTO registerDTO) {
String host = registerDTO.getHost();
int port = registerDTO.getPort();
- return String.join(":", host, Integer.toString(port));
+ return String.join(Constants.COLONS, host, Integer.toString(port));
}
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
@@ -123,11 +127,13 @@ public class ZookeeperClientRegisterRepository implements
ShenyuClientRegisterRe
String rpcType = metadata.getRpcType();
if (RpcTypeEnum.HTTP.getName().equals(rpcType)
|| RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
- nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
+ nodeName = String.join(DefaultPathConstants.SELECTOR_JOIN_RULE,
+ metadata.getContextPath(),
+ metadata.getRuleName().replace(PATH_SEPARATOR,
DefaultPathConstants.SELECTOR_JOIN_RULE));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
}
- return nodeName.startsWith("/") ? nodeName.substring(1) : nodeName;
+ return nodeName.startsWith(PATH_SEPARATOR) ? nodeName.substring(1) :
nodeName;
}
private class ZkStateListener implements IZkStateListener {
diff --git
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/path/RegisterPathConstants.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/path/RegisterPathConstants.java
index 2fbe020..79cf881 100644
---
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/path/RegisterPathConstants.java
+++
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/path/RegisterPathConstants.java
@@ -101,7 +101,7 @@ public class RegisterPathConstants {
*/
public static String buildServiceInstancePath(final String rpcType) {
return String.join(SEPARATOR, ROOT_PATH, "service", rpcType)
- .replace("/", ".").substring(1);
+ .replace("/", DOT_SEPARATOR).substring(1);
}
/**
@@ -114,7 +114,7 @@ public class RegisterPathConstants {
*/
public static String buildServiceConfigPath(final String rpcType, final
String contextPath) {
final String serviceConfigPathOrigin = String.join(SEPARATOR,
ROOT_PATH, "service", rpcType, contextPath)
- .replace("/", ".").replace("*", "");
+ .replace("/", DOT_SEPARATOR).replace("*", "");
final String serviceConfigPathAfterSubstring =
serviceConfigPathOrigin.substring(1);
if (serviceConfigPathAfterSubstring.endsWith(".")) {
return serviceConfigPathAfterSubstring.substring(0,
serviceConfigPathAfterSubstring.length() - 1);
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ConsulServerRegisterRepository.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ConsulServerRegisterRepository.java
index 55273d3..c05cda9 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ConsulServerRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ConsulServerRegisterRepository.java
@@ -21,6 +21,7 @@ import com.ecwid.consul.v1.kv.model.GetValue;
import com.google.common.collect.Lists;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
@@ -115,7 +116,7 @@ public class ConsulServerRegisterRepository implements
ShenyuServerRegisterRepos
Map<String, List<URIRegisterDTO>> map = new HashMap<>();
List<ServiceInstance> instances = discoveryClient.getAllInstances();
instances.forEach(serviceInstance -> {
- String data = serviceInstance.getMetadata().get("uri");
+ String data = serviceInstance.getMetadata().get(Constants.URI);
if (Objects.nonNull(data)) {
URIRegisterDTO uriRegisterDTO =
GsonUtils.getInstance().fromJson(data, URIRegisterDTO.class);
String contextPath = uriRegisterDTO.getContextPath();
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ShenyuConsulConfigWatch.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ShenyuConsulConfigWatch.java
index 8ed230c..f1624b6 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ShenyuConsulConfigWatch.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-consul/src/main/java/org/apache/shenyu/register/server/consul/ShenyuConsulConfigWatch.java
@@ -25,10 +25,10 @@ import
org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.SmartLifecycle;
+import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -42,7 +42,7 @@ public class ShenyuConsulConfigWatch implements
SmartLifecycle {
private static final Logger LOGGER =
LoggerFactory.getLogger(ShenyuConsulConfigWatch.class);
- @Autowired
+ @Resource
private ConsulClient consul;
private final ScheduledThreadPoolExecutor executor;
@@ -86,21 +86,21 @@ public class ShenyuConsulConfigWatch implements
SmartLifecycle {
if (Objects.nonNull(newIndex) &&
!newIndex.equals(currentIndex)) {
if (!this.consulIndexes.containsValue(newIndex)
&& !currentIndex.equals(-1L)) {
- LOGGER.trace("Context " + context + " has new
index " + newIndex);
+ LOGGER.trace("Context {} has new index {}",
context, newIndex);
Map<String, GetValue> valueMap =
extractGetValue(response);
publisher.publishEvent(new
ConsulConfigChangedEvent(this, newIndex, valueMap));
} else if (LOGGER.isTraceEnabled()) {
- LOGGER.info("Event for index already published
for context " + context);
+ LOGGER.info("Event for index already published
for context {}", context);
}
this.consulIndexes.put(context, newIndex);
} else if (LOGGER.isTraceEnabled()) {
- LOGGER.trace("Same index for context " + context);
+ LOGGER.trace("Same index for context {}", context);
}
} else if (LOGGER.isTraceEnabled()) {
- LOGGER.trace("No value for context " + context);
+ LOGGER.trace("No value for context {}", context);
}
} catch (Exception e) {
- LOGGER.warn("Error querying consul Key/Values for context
'" + context + "'. Message: " + e.getMessage());
+ LOGGER.warn("Error querying consul Key/Values for context
'{}'. Message: {}", context, e.getMessage());
}
}
}
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/EtcdServerRegisterRepository.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/EtcdServerRegisterRepository.java
index ff35f7b..5b36e2a 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/EtcdServerRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/EtcdServerRegisterRepository.java
@@ -18,6 +18,8 @@
package org.apache.shenyu.register.server.etcd;
import com.google.common.collect.Lists;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
@@ -32,10 +34,10 @@ import org.apache.shenyu.spi.Join;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
-import java.util.ArrayList;
/**
@@ -98,7 +100,7 @@ public class EtcdServerRegisterRepository implements
ShenyuServerRegisterReposit
String rpcPath =
RegisterPathConstants.buildURIContextPathParent(rpcType);
Set<String> contextList = new HashSet<>();
client.getChildren(rpcPath).forEach(dataPath -> {
- String context = dataPath.split("/")[4];
+ String context = dataPath.split(Constants.PATH_SEPARATOR)[4];
contextList.add(context);
});
contextList.forEach(context -> registerUriChildrenList(rpcPath,
context));
@@ -107,24 +109,24 @@ public class EtcdServerRegisterRepository implements
ShenyuServerRegisterReposit
@Override
public void updateHandler(final String path, final String value) {
- String[] paths = path.split("/");
+ String[] paths = path.split(Constants.PATH_SEPARATOR);
registerUriChildrenList(rpcPath, paths[4]);
}
@Override
public void deleteHandler(final String path, final String value) {
- String[] paths = path.split("/");
+ String[] paths = path.split(Constants.PATH_SEPARATOR);
registerUriChildrenList(rpcPath, paths[4]);
}
});
}
private void registerUriChildrenList(final String rpcPath, final String
context) {
- String contextPath = String.join("/", rpcPath, context);
- List<URIRegisterDTO> uriRegisterDTOList = new ArrayList<>();
+ String contextPath = String.join(Constants.PATH_SEPARATOR, rpcPath,
context);
+ List<URIRegisterDTO> uriRegisterDTOList = new LinkedList<>();
client.getChildren(contextPath).forEach(path ->
uriRegisterDTOList.add(GsonUtils.getInstance().fromJson(client.read(path),
URIRegisterDTO.class)));
- if (uriRegisterDTOList.isEmpty()) {
- URIRegisterDTO uriRegisterDTO =
URIRegisterDTO.builder().contextPath("/" + context).build();
+ if (CollectionUtils.isEmpty(uriRegisterDTOList)) {
+ URIRegisterDTO uriRegisterDTO =
URIRegisterDTO.builder().contextPath(Constants.PATH_SEPARATOR +
context).build();
uriRegisterDTOList.add(uriRegisterDTO);
}
publishURI(uriRegisterDTOList);
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-nacos/src/main/java/org/apache/shenyu/register/server/nacos/NacosServerRegisterRepository.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-nacos/src/main/java/org/apache/shenyu/register/server/nacos/NacosServerRegisterRepository.java
index eecbf85..dce5da6 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-nacos/src/main/java/org/apache/shenyu/register/server/nacos/NacosServerRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-nacos/src/main/java/org/apache/shenyu/register/server/nacos/NacosServerRegisterRepository.java
@@ -27,6 +27,7 @@ import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.google.common.collect.Lists;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.constant.NacosPathConstants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.exception.ShenyuException;
@@ -204,7 +205,7 @@ public class NacosServerRegisterRepository implements
ShenyuServerRegisterReposi
}
if (registerDTOList.isEmpty()) {
URIRegisterDTO uriRegisterDTO = new URIRegisterDTO();
- uriRegisterDTO.setContextPath("/" + contextPath);
+ uriRegisterDTO.setContextPath(Constants.PATH_SEPARATOR +
contextPath);
registerDTOList.add(uriRegisterDTO);
}
publishRegisterURI(registerDTOList);
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-zookeeper/src/main/java/org/apache/shenyu/register/server/zookeeper/ZookeeperServerRegisterRepository.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-zookeeper/src/main/java/org/apache/shenyu/register/server/zookeeper/ZookeeperServerRegisterRepository.java
index f4b58ed..ed0dc09 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-zookeeper/src/main/java/org/apache/shenyu/register/server/zookeeper/ZookeeperServerRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-zookeeper/src/main/java/org/apache/shenyu/register/server/zookeeper/ZookeeperServerRegisterRepository.java
@@ -22,6 +22,7 @@ import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
@@ -33,6 +34,7 @@ import
org.apache.shenyu.register.server.api.ShenyuServerRegisterRepository;
import org.apache.shenyu.spi.Join;
import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
@@ -139,14 +141,15 @@ public class ZookeeperServerRegisterRepository implements
ShenyuServerRegisterRe
}
private void registerURIChildrenList(final List<String> childrenList,
final String uriParentPath) {
- List<URIRegisterDTO> registerDTOList = new ArrayList<>();
+ List<URIRegisterDTO> registerDTOList = new LinkedList<>();
childrenList.forEach(addPath -> {
String realPath =
RegisterPathConstants.buildRealNode(uriParentPath, addPath);
registerDTOList.add(GsonUtils.getInstance().fromJson(zkClient.readData(realPath).toString(),
URIRegisterDTO.class));
});
- if (registerDTOList.isEmpty()) {
- String contextPath = StringUtils.substringAfterLast(uriParentPath,
"/");
- URIRegisterDTO uriRegisterDTO =
URIRegisterDTO.builder().contextPath("/" + contextPath).build();
+
+ if (CollectionUtils.isEmpty(registerDTOList)) {
+ String contextPath = StringUtils.substringAfterLast(uriParentPath,
Constants.PATH_SEPARATOR);
+ URIRegisterDTO uriRegisterDTO =
URIRegisterDTO.builder().contextPath(Constants.PATH_SEPARATOR +
contextPath).build();
registerDTOList.add(uriRegisterDTO);
}
publishRegisterURI(registerDTOList);