This is an automated email from the ASF dual-hosted git repository.
asxing 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 6ba1579 refactor shenyu-register-center: code polish. (#2381)
6ba1579 is described below
commit 6ba15797f65f06658df5c3e134d9cc7f7d8a0c3f
Author: midnight2104 <[email protected]>
AuthorDate: Wed Nov 17 14:06:52 2021 +0800
refactor shenyu-register-center: code polish. (#2381)
* refactor shenyu-common: code polish
* refactor shenyu-common: code polish
* refactor shenyu-data-sync-center: code polish
* refactor shenyu-data-sync-center: code polish
* refactor shenyu-admin: code polish
* refactor shenyu-admin: add user permissions.
* refactor shenyu-admin: ignore password in api.
* refactor shenyu-register-center: code polish.
---
.../client/api/ShenyuClientRegisterRepository.java | 12 +--------
.../consul/ConsulClientRegisterRepository.java | 7 +++--
.../client/etcd/EtcdClientRegisterRepository.java | 13 ++++++---
.../client/http/HttpClientRegisterRepository.java | 10 ++++---
.../register/client/http/utils/OkHttpTools.java | 3 ---
.../nacos/NacosClientRegisterRepository.java | 11 +++++---
.../ZookeeperClientRegisterRepository.java | 7 +++--
.../common/config/ShenyuRegisterCenterConfig.java | 4 ++-
.../shenyu/register/common/dto/URIRegisterDTO.java | 31 +++++++++-------------
.../shenyu/register/common/enums/EventType.java | 3 +++
.../common/exception/ShenyuRegisterException.java | 30 ---------------------
.../common/path/RegisterPathConstants.java | 26 +-----------------
.../consul/ConsulServerRegisterRepository.java | 12 +++++----
.../server/consul/ShenyuConsulConfigWatch.java | 8 ++++--
.../register/server/etcd/client/EtcdClient.java | 13 ++++++---
.../nacos/NacosServerRegisterRepository.java | 5 +++-
.../ZookeeperServerRegisterRepository.java | 2 ++
17 files changed, 85 insertions(+), 112 deletions(-)
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/ShenyuClientRegisterRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/ShenyuClientRegisterRepository.java
index 8f6c231..084eb5e 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/ShenyuClientRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/ShenyuClientRegisterRepository.java
@@ -27,17 +27,7 @@ import org.apache.shenyu.spi.SPI;
*/
@SPI
public interface ShenyuClientRegisterRepository {
-
- /**
- * Path separator.
- */
- String PATH_SEPARATOR = "/";
-
- /**
- * Dot separator.
- */
- String DOT_SEPARATOR = ".";
-
+
/**
* Init.
*
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 0609c00..291b655 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
@@ -61,7 +61,9 @@ public class ConsulClientRegisterRepository implements
ShenyuClientRegisterRepos
registerURI(registerDTO);
}
- private void registerMetadata(final String rpcType, final String
contextPath, final MetaDataRegisterDTO metadata) {
+ private void registerMetadata(final String rpcType,
+ final String contextPath,
+ final MetaDataRegisterDTO metadata) {
String metadataNodeName = buildMetadataNodeName(metadata);
String metaDataPath =
RegisterPathConstants.buildMetaDataParentPath(rpcType, contextPath);
String realNode = RegisterPathConstants.buildRealNode(metaDataPath,
metadataNodeName);
@@ -76,7 +78,8 @@ public class ConsulClientRegisterRepository implements
ShenyuClientRegisterRepos
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
String nodeName;
String rpcType = metadata.getRpcType();
- if (RpcTypeEnum.HTTP.getName().equals(rpcType) ||
RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
+ if (RpcTypeEnum.HTTP.getName().equals(rpcType)
+ || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
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 c8fbd83..dc77333 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
@@ -76,18 +76,24 @@ public class EtcdClientRegisterRepository implements
ShenyuClientRegisterReposit
LogUtils.info(LOGGER, "{} etcd client register uri success: {}",
rpcType, registerDTO);
}
- private void registerMetadata(final String rpcType, final String
contextPath, final MetaDataRegisterDTO metadata) {
+ private void registerMetadata(final String rpcType,
+ final String contextPath,
+ final MetaDataRegisterDTO metadata) {
String metadataNodeName = buildMetadataNodeName(metadata);
String metaDataPath =
RegisterPathConstants.buildMetaDataParentPath(rpcType, contextPath);
String realNode = RegisterPathConstants.buildRealNode(metaDataPath,
metadataNodeName);
+
client.putEphemeral(realNode,
GsonUtils.getInstance().toJson(metadata));
LOGGER.info("register metadata success: {}", realNode);
}
- private void registerURI(final String rpcType, final String contextPath,
final URIRegisterDTO registerDTO) {
+ private void registerURI(final String rpcType,
+ final String contextPath,
+ final URIRegisterDTO registerDTO) {
String uriNodeName = buildURINodeName(registerDTO);
String uriPath = RegisterPathConstants.buildURIParentPath(rpcType,
contextPath);
String realNode = RegisterPathConstants.buildRealNode(uriPath,
uriNodeName);
+
client.putEphemeral(realNode,
GsonUtils.getInstance().toJson(registerDTO));
LOGGER.info("register uri data success: {}", realNode);
}
@@ -101,7 +107,8 @@ public class EtcdClientRegisterRepository implements
ShenyuClientRegisterReposit
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
String nodeName;
String rpcType = metadata.getRpcType();
- if (RpcTypeEnum.HTTP.getName().equals(rpcType) ||
RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
+ if (RpcTypeEnum.HTTP.getName().equals(rpcType)
+ || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
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 8911604..c9c6a9f 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
@@ -40,9 +40,13 @@ public class HttpClientRegisterRepository implements
ShenyuClientRegisterReposit
private static final Logger LOGGER =
LoggerFactory.getLogger(RegisterUtils.class);
private static final String META_PATH = "/shenyu-client/register-metadata";
-
+
+ private static final String META_TYPE = "metadata";
+
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();
@@ -59,12 +63,12 @@ public class HttpClientRegisterRepository implements
ShenyuClientRegisterReposit
*/
@Override
public void persistURI(final URIRegisterDTO registerDTO) {
- doRegister(registerDTO, URI_PATH, "uri");
+ doRegister(registerDTO, URI_PATH, URI_TYPE);
}
@Override
public void persistInterface(final MetaDataRegisterDTO metadata) {
- doRegister(metadata, META_PATH, "metadata");
+ doRegister(metadata, META_PATH, META_TYPE);
}
private <T> void doRegister(final T t, final String path, final String
type) {
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/OkHttpTools.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/OkHttpTools.java
index adad50e..2c33060 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/OkHttpTools.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-http/src/main/java/org/apache/shenyu/register/client/http/utils/OkHttpTools.java
@@ -17,7 +17,6 @@
package org.apache.shenyu.register.client.http.utils;
-import com.google.gson.Gson;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -38,8 +37,6 @@ public final class OkHttpTools {
private static final OkHttpTools OK_HTTP_TOOLS = new OkHttpTools();
- private static final Gson GSON = new Gson();
-
private final OkHttpClient client;
private OkHttpTools() {
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 0c336ea..a74527c 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
@@ -114,8 +114,11 @@ public class NacosClientRegisterRepository implements
ShenyuClientRegisterReposi
registerService(rpcType, contextPath, host, port, registerDTO);
}
- private synchronized void registerService(final String rpcType, final
String contextPath, final String host,
- final int port, final
URIRegisterDTO registerDTO) {
+ private synchronized void registerService(final String rpcType,
+ final String contextPath,
+ final String host,
+ final int port,
+ final URIRegisterDTO
registerDTO) {
if (registerService) {
return;
}
@@ -128,6 +131,7 @@ public class NacosClientRegisterRepository implements
ShenyuClientRegisterReposi
metadataMap.put(Constants.CONTEXT_PATH, contextPath);
metadataMap.put("uriMetadata",
GsonUtils.getInstance().toJson(registerDTO));
instance.setMetadata(metadataMap);
+
String serviceName =
RegisterPathConstants.buildServiceInstancePath(rpcType);
try {
namingService.registerInstance(serviceName, instance);
@@ -137,7 +141,8 @@ public class NacosClientRegisterRepository implements
ShenyuClientRegisterReposi
LOGGER.info("register service uri success: {}", serviceName);
}
- private synchronized void registerConfig(final String rpcType, final
String contextPath,
+ private synchronized void registerConfig(final String rpcType,
+ final String contextPath,
final MetaDataRegisterDTO
metadata) {
metadataCache.add(GsonUtils.getInstance().toJson(metadata));
String configName =
RegisterPathConstants.buildServiceConfigPath(rpcType, contextPath);
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 b37e14f..7036bf1 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
@@ -82,7 +82,9 @@ public class ZookeeperClientRegisterRepository implements
ShenyuClientRegisterRe
zkClient.close();
}
- private void registerMetadata(final String rpcType, final String
contextPath, final MetaDataRegisterDTO metadata) {
+ private void registerMetadata(final String rpcType,
+ final String contextPath,
+ final MetaDataRegisterDTO metadata) {
String metadataNodeName = buildMetadataNodeName(metadata);
String metaDataPath =
RegisterPathConstants.buildMetaDataParentPath(rpcType, contextPath);
if (!zkClient.exists(metaDataPath)) {
@@ -119,7 +121,8 @@ public class ZookeeperClientRegisterRepository implements
ShenyuClientRegisterRe
private String buildMetadataNodeName(final MetaDataRegisterDTO metadata) {
String nodeName;
String rpcType = metadata.getRpcType();
- if (RpcTypeEnum.HTTP.getName().equals(rpcType) ||
RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
+ if (RpcTypeEnum.HTTP.getName().equals(rpcType)
+ || RpcTypeEnum.SPRING_CLOUD.getName().equals(rpcType)) {
nodeName = String.join("-", metadata.getContextPath(),
metadata.getRuleName().replace("/", "-"));
} else {
nodeName =
RegisterPathConstants.buildNodeName(metadata.getServiceName(),
metadata.getMethodName());
diff --git
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/config/ShenyuRegisterCenterConfig.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/config/ShenyuRegisterCenterConfig.java
index a58e994..daf7aeb 100644
---
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/config/ShenyuRegisterCenterConfig.java
+++
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/config/ShenyuRegisterCenterConfig.java
@@ -32,7 +32,9 @@ public final class ShenyuRegisterCenterConfig extends
PropertiesConfig {
}
- public ShenyuRegisterCenterConfig(final String registerType, final String
serverLists, final Properties props) {
+ public ShenyuRegisterCenterConfig(final String registerType,
+ final String serverLists,
+ final Properties props) {
this.registerType = registerType;
this.serverLists = serverLists;
this.setProps(props);
diff --git
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/URIRegisterDTO.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/URIRegisterDTO.java
index 6fce83d..6de13df 100644
---
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/URIRegisterDTO.java
+++
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/URIRegisterDTO.java
@@ -41,8 +41,9 @@ public class URIRegisterDTO implements DataTypeParent {
private EventType eventType;
- public URIRegisterDTO(final String appName, final String contextPath,
final String rpcType,
- final String host, final Integer port, final
EventType eventType) {
+ public URIRegisterDTO(final String appName, final String contextPath,
+ final String rpcType, final String host,
+ final Integer port, final EventType eventType) {
this.appName = appName;
this.contextPath = contextPath;
this.rpcType = rpcType;
@@ -228,26 +229,26 @@ public class URIRegisterDTO implements DataTypeParent {
public int hashCode() {
return Objects.hash(getAppName(), getContextPath(), getRpcType(),
getHost(), getPort(), getEventType());
}
-
+
@Override
public String toString() {
return "URIRegisterDTO{"
- + "appName='"
+ + "appName='"
+ appName
+ ", contextPath='"
+ contextPath
- + ", rpcType='"
+ + ", rpcType='"
+ rpcType
- + ", host='"
+ + ", host='"
+ host
- + ", port="
- + port
- + ", eventType="
- + eventType
+ + ", port="
+ + port
+ + ", eventType="
+ + eventType
+ '}';
-
+
}
-
+
public static final class Builder {
private String appName;
@@ -269,7 +270,6 @@ public class URIRegisterDTO implements DataTypeParent {
* appName.
*
* @param appName appName
- *
* @return Builder
*/
public Builder appName(final String appName) {
@@ -281,7 +281,6 @@ public class URIRegisterDTO implements DataTypeParent {
* contextPath.
*
* @param contextPath contextPath
- *
* @return Builder
*/
public Builder contextPath(final String contextPath) {
@@ -293,7 +292,6 @@ public class URIRegisterDTO implements DataTypeParent {
* rpcType.
*
* @param rpcType rpcType
- *
* @return Builder
*/
public Builder rpcType(final String rpcType) {
@@ -305,7 +303,6 @@ public class URIRegisterDTO implements DataTypeParent {
* host.
*
* @param host host
- *
* @return Builder
*/
public Builder host(final String host) {
@@ -317,7 +314,6 @@ public class URIRegisterDTO implements DataTypeParent {
* port.
*
* @param port port
- *
* @return Builder
*/
public Builder port(final Integer port) {
@@ -329,7 +325,6 @@ public class URIRegisterDTO implements DataTypeParent {
* eventType.
*
* @param eventType eventType
- *
* @return Builder
*/
public Builder eventType(final EventType eventType) {
diff --git
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/enums/EventType.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/enums/EventType.java
index 5bcca95..3f84453 100644
---
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/enums/EventType.java
+++
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/enums/EventType.java
@@ -27,14 +27,17 @@ public enum EventType {
* Register event type.
*/
REGISTER,
+
/**
* Updated event type.
*/
UPDATED,
+
/**
* Deleted event type.
*/
DELETED,
+
/**
* Ignored event type.
*/
diff --git
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/exception/ShenyuRegisterException.java
b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/exception/ShenyuRegisterException.java
deleted file mode 100644
index 21dfb14..0000000
---
a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/exception/ShenyuRegisterException.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shenyu.register.common.exception;
-
-/**
- * Shenyu register exception.
- */
-public final class ShenyuRegisterException extends RuntimeException {
-
- private static final long serialVersionUID = -6417179023552012152L;
-
- public ShenyuRegisterException(final Exception cause) {
- super(cause);
- }
-}
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 a6b0f48..2fbe020 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
@@ -80,31 +80,7 @@ public class RegisterPathConstants {
public static String buildURIParentPath(final String rpcType, final String
contextPath) {
return String.join(SEPARATOR, ROOT_PATH, "uri", rpcType, contextPath);
}
-
- /**
- * Build uri read node string.
- *
- * @param rpcType the rpc type
- * @param contextPath the context path
- * @param nodeName the node name
- * @return the string
- */
- public static String buildURIReadNode(final String rpcType, final String
contextPath, final String nodeName) {
- return buildRealNode(buildURIParentPath(rpcType, contextPath),
nodeName);
- }
-
- /**
- * Build meta data child path string.
- *
- * @param rpcType the rpc type
- * @param contextPath the context path
- * @param nodeName the node name
- * @return the string
- */
- public static String buildMetaDataReadNode(final String rpcType, final
String contextPath, final String nodeName) {
- return buildRealNode(buildMetaDataParentPath(rpcType, contextPath),
nodeName);
- }
-
+
/**
* Build real node string.
*
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 5e67a31..55273d3 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
@@ -40,6 +40,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
@Join
public class ConsulServerRegisterRepository implements
ShenyuServerRegisterRepository {
@@ -58,7 +59,8 @@ public class ConsulServerRegisterRepository implements
ShenyuServerRegisterRepos
private final Map<String, Long> indexMap = new HashMap<>();
@Override
- public void init(final ShenyuServerRegisterPublisher publisher, final
ShenyuRegisterCenterConfig config) {
+ public void init(final ShenyuServerRegisterPublisher publisher,
+ final ShenyuRegisterCenterConfig config) {
this.publisher = publisher;
}
@@ -114,7 +116,7 @@ public class ConsulServerRegisterRepository implements
ShenyuServerRegisterRepos
List<ServiceInstance> instances = discoveryClient.getAllInstances();
instances.forEach(serviceInstance -> {
String data = serviceInstance.getMetadata().get("uri");
- if (null != data) {
+ if (Objects.nonNull(data)) {
URIRegisterDTO uriRegisterDTO =
GsonUtils.getInstance().fromJson(data, URIRegisterDTO.class);
String contextPath = uriRegisterDTO.getContextPath();
map.putIfAbsent(contextPath, new ArrayList<>());
@@ -127,11 +129,11 @@ public class ConsulServerRegisterRepository implements
ShenyuServerRegisterRepos
}
private boolean metadataChanged(final String path, final long index) {
- boolean result = !indexMap.containsKey(path) || indexMap.get(path) <
index;
- if (result) {
+ boolean hasResult = !indexMap.containsKey(path) || indexMap.get(path)
< index;
+ if (hasResult) {
indexMap.put(path, index);
}
- return result;
+ return hasResult;
}
}
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 e178a8b..8ed230c 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
@@ -32,6 +32,7 @@ import org.springframework.context.SmartLifecycle;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@@ -58,12 +59,15 @@ public class ShenyuConsulConfigWatch implements
SmartLifecycle {
private ScheduledFuture<?> watchFuture;
- public ShenyuConsulConfigWatch(final ShenyuRegisterCenterConfig config,
final ApplicationEventPublisher publisher) {
+ public ShenyuConsulConfigWatch(final ShenyuRegisterCenterConfig config,
+ final ApplicationEventPublisher publisher) {
this.watchDelay =
Integer.parseInt(config.getProps().getProperty("delay", "1"));
this.waitTime =
Integer.parseInt(config.getProps().getProperty("wait-time", "55"));
executor = new ScheduledThreadPoolExecutor(1,
ShenyuThreadFactory.create("consul-config-watch", true));
+
String metadataPath = config.getProps().getProperty("metadata-path",
"shenyu/register");
consulIndexes.put(metadataPath, 0L);
+
this.publisher = publisher;
}
@@ -79,7 +83,7 @@ public class ShenyuConsulConfigWatch implements
SmartLifecycle {
if (response.getValue() != null &&
!response.getValue().isEmpty()) {
Long newIndex = response.getConsulIndex();
- if (newIndex != null &&
!newIndex.equals(currentIndex)) {
+ if (Objects.nonNull(newIndex) &&
!newIndex.equals(currentIndex)) {
if (!this.consulIndexes.containsValue(newIndex)
&& !currentIndex.equals(-1L)) {
LOGGER.trace("Context " + context + " has new
index " + newIndex);
diff --git
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/client/EtcdClient.java
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/client/EtcdClient.java
index 4581572..07b6e6c 100644
---
a/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/client/EtcdClient.java
+++
b/shenyu-register-center/shenyu-register-server/shenyu-register-server-etcd/src/main/java/org/apache/shenyu/register/server/etcd/client/EtcdClient.java
@@ -33,6 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
+import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
@@ -88,6 +89,7 @@ public class EtcdClient {
/**
* read data.
+ *
* @param key key
* @return string of data
*/
@@ -95,14 +97,17 @@ public class EtcdClient {
KV kv = client.getKVClient();
ByteSequence storeKey =
Optional.ofNullable(key).map(ByteSequence::fromString).orElse(null);
GetResponse response = null;
+
try {
response = kv.get(storeKey).get();
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("read(key:{}) error.", key, e);
}
- if (response == null) {
+
+ if (Objects.isNull(response)) {
return null;
}
+
LOGGER.debug(String.valueOf(response.getHeader()));
Node info =
response.getKvs().stream().map(EtcdClient::kv2NodeInfo).findFirst().orElse(null);
assert info != null;
@@ -111,6 +116,7 @@ public class EtcdClient {
/**
* get children of path.
+ *
* @param path path
* @return list of children
*/
@@ -136,7 +142,8 @@ public class EtcdClient {
/**
* subscribe children change.
- * @param key key
+ *
+ * @param key key
* @param handler event handler
*/
public void subscribeChildChanges(final String key, final
EtcdListenHandler handler) {
@@ -163,7 +170,7 @@ public class EtcdClient {
response.getEvents().forEach(watchEvent -> {
KeyValue keyValue = watchEvent.getKeyValue();
Node info = kv2NodeInfo(keyValue);
- // 跳过根节点的变化
+ // skip root node change
if (watchEvent.getKeyValue().getKey().equals(storeKey)) {
return;
}
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 d588cee..eecbf85 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
@@ -81,7 +81,8 @@ public class NacosServerRegisterRepository implements
ShenyuServerRegisterReposi
}
@Override
- public void init(final ShenyuServerRegisterPublisher publisher, final
ShenyuRegisterCenterConfig config) {
+ public void init(final ShenyuServerRegisterPublisher publisher,
+ final ShenyuRegisterCenterConfig config) {
this.publisher = publisher;
String serverAddr = config.getServerLists();
Properties properties = config.getProps();
@@ -96,12 +97,14 @@ public class NacosServerRegisterRepository implements
ShenyuServerRegisterReposi
nacosProperties.put(PropertyKeyConst.ACCESS_KEY,
properties.getProperty(PropertyKeyConst.ACCESS_KEY, ""));
// secret key for namespace
nacosProperties.put(PropertyKeyConst.SECRET_KEY,
properties.getProperty(PropertyKeyConst.SECRET_KEY, ""));
+
try {
this.configService =
ConfigFactory.createConfigService(nacosProperties);
this.namingService =
NamingFactory.createNamingService(nacosProperties);
} catch (NacosException e) {
throw new ShenyuException(e);
}
+
subscribe();
}
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 30778f1..f4b58ed 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
@@ -51,10 +51,12 @@ public class ZookeeperServerRegisterRepository implements
ShenyuServerRegisterRe
public void init(final ShenyuServerRegisterPublisher publisher, final
ShenyuRegisterCenterConfig config) {
this.init(config);
this.publisher = publisher;
+
Properties props = config.getProps();
int sessionTimeout =
Integer.parseInt(props.getProperty("sessionTimeout", "30000"));
int connectionTimeout =
Integer.parseInt(props.getProperty("connectionTimeout", "3000"));
this.zkClient = new ZkClient(config.getServerLists(), sessionTimeout,
connectionTimeout);
+
initSubscribe();
}