This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new ddda44b72 [ISSUE #4850] apollo as registry, client metadata and uri 
cannot be synchronized to admin (#4851)
ddda44b72 is described below

commit ddda44b7246bf887482a5edeee94c0a32f66ac47
Author: xuziyang <[email protected]>
AuthorDate: Sun Jul 16 16:20:37 2023 +0800

    [ISSUE #4850] apollo as registry, client metadata and uri cannot be 
synchronized to admin (#4851)
---
 .../ApolloClientServerRegisterRepository.java      | 41 ++++++++++++-------
 .../apollo/ApolloClientRegisterRepository.java     | 44 ++++++++++++---------
 .../common/path/RegisterPathConstants.java         | 46 ++++++++--------------
 3 files changed, 67 insertions(+), 64 deletions(-)

diff --git 
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-apollo/src/main/java/org/apache/shenyu/register/client/server/apollo/ApolloClientServerRegisterRepository.java
 
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-apollo/src/main/java/org/apache/shenyu/register/client/server/apollo/ApolloClientServerRegisterRepository.java
index bee2a27b8..613a28c15 100644
--- 
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-apollo/src/main/java/org/apache/shenyu/register/client/server/apollo/ApolloClientServerRegisterRepository.java
+++ 
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-apollo/src/main/java/org/apache/shenyu/register/client/server/apollo/ApolloClientServerRegisterRepository.java
@@ -22,19 +22,19 @@ import com.ctrip.framework.apollo.ConfigService;
 import com.ctrip.framework.apollo.core.ConfigConsts;
 import com.ctrip.framework.apollo.model.ConfigChange;
 import com.google.common.collect.Lists;
-import org.apache.shenyu.common.constant.ApolloPathConstants;
 import org.apache.shenyu.common.utils.GsonUtils;
 import 
org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterPublisher;
 import 
org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterRepository;
 import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.apache.shenyu.register.common.path.RegisterPathConstants;
 import org.apache.shenyu.spi.Join;
 
-import java.util.List;
+import java.util.Collections;
 import java.util.Optional;
 import java.util.Properties;
-import java.util.stream.Collectors;
+import java.util.Set;
 
 /**
  * apollo register center.
@@ -72,30 +72,41 @@ public class ApolloClientServerRegisterRepository 
implements ShenyuClientServerR
     }
 
     private void initSubscribe() {
+        // on startup, read data
+        Set<String> propertyNames = this.config.getPropertyNames();
+        for (String propertyName : propertyNames) {
+            String property = this.config.getProperty(propertyName, "{}");
+            if 
(propertyName.startsWith(RegisterPathConstants.REGISTER_METADATA_INSTANCE_ROOT_PATH))
 {
+                this.publishMetadata(property);
+            } else if 
(propertyName.startsWith(RegisterPathConstants.REGISTER_URI_INSTANCE_ROOT_PATH))
 {
+                this.publishRegisterURI(property);
+            }
+        }
+
+        // monitor metadata changes
         this.config.addChangeListener(changeEvent -> {
             for (String changedKey : changeEvent.changedKeys()) {
                 ConfigChange configChange = changeEvent.getChange(changedKey);
-                if 
(changedKey.startsWith(ApolloPathConstants.REGISTER_METADATA_ID)) {
-                    this.publishMetadata(configChange.getNewValue());
+                this.publishMetadata(configChange.getNewValue());
+            }
+        }, null, 
Collections.singleton(RegisterPathConstants.REGISTER_METADATA_INSTANCE_ROOT_PATH));
 
-                } else if 
(changedKey.startsWith(ApolloPathConstants.REGISTER_URI_ID)) {
-                    this.publishRegisterURI(configChange.getNewValue());
-                }
+        // monitor uri changes
+        this.config.addChangeListener(changeEvent -> {
+            for (String changedKey : changeEvent.changedKeys()) {
+                ConfigChange configChange = changeEvent.getChange(changedKey);
+                this.publishRegisterURI(configChange.getNewValue());
             }
-        });
+        }, null, 
Collections.singleton(RegisterPathConstants.REGISTER_URI_INSTANCE_ROOT_PATH));
     }
 
     private void publishMetadata(final String metadata) {
         
publisher.publish(Lists.newArrayList(GsonUtils.getInstance().fromJson(metadata, 
MetaDataRegisterDTO.class)));
     }
 
-    @SuppressWarnings("unchecked")
     private void publishRegisterURI(final String uriMetadata) {
-        List<String> metadataList = 
GsonUtils.getInstance().fromJson(uriMetadata, List.class);
-        List<URIRegisterDTO> registerDTOList = metadataList.stream()
-                .map(metadata -> GsonUtils.getInstance().fromJson(metadata, 
URIRegisterDTO.class))
-                .collect(Collectors.toList());
-        publisher.publish(registerDTOList);
+        URIRegisterDTO uriRegisterDTO = 
GsonUtils.getInstance().fromJson(uriMetadata, URIRegisterDTO.class);
+        publisher.publish(Lists.newArrayList(uriRegisterDTO));
     }
 
     @Override
diff --git 
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-apollo/src/main/java/org/apache/shenyu/register/client/apollo/ApolloClientRegisterRepository.java
 
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-apollo/src/main/java/org/apache/shenyu/register/client/apollo/ApolloClientRegisterRepository.java
index 306f5fb33..2633bb948 100644
--- 
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-apollo/src/main/java/org/apache/shenyu/register/client/apollo/ApolloClientRegisterRepository.java
+++ 
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-apollo/src/main/java/org/apache/shenyu/register/client/apollo/ApolloClientRegisterRepository.java
@@ -19,7 +19,6 @@ package org.apache.shenyu.register.client.apollo;
 
 import com.ctrip.framework.apollo.core.ConfigConsts;
 import org.apache.shenyu.common.constant.Constants;
-import org.apache.shenyu.common.exception.ShenyuException;
 import org.apache.shenyu.common.utils.ContextPathUtils;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.common.utils.LogUtils;
@@ -32,8 +31,9 @@ import org.apache.shenyu.spi.Join;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Objects;
 import java.util.Properties;
-import java.util.concurrent.ConcurrentLinkedQueue;
+
 
 /**
  * apollo register center client.
@@ -43,8 +43,6 @@ public class ApolloClientRegisterRepository implements 
ShenyuClientRegisterRepos
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(ApolloClientRegisterRepository.class);
 
-    private final ConcurrentLinkedQueue<String> metadataCache = new 
ConcurrentLinkedQueue<>();
-
     private ApolloClient apolloClient;
 
     @Override
@@ -70,9 +68,8 @@ public class ApolloClientRegisterRepository implements 
ShenyuClientRegisterRepos
 
     @Override
     public void persistInterface(final MetaDataRegisterDTO metadata) {
-        String rpcType = metadata.getRpcType();
-        String contextPath = 
ContextPathUtils.buildRealNode(metadata.getContextPath(), 
metadata.getAppName());
-        registerConfig(rpcType, contextPath, metadata);
+        registerMetadata(metadata);
+        LogUtils.info(LOGGER, "{} apollo client register metadata success: 
{}", metadata.getRpcType(), metadata);
     }
 
     @Override
@@ -87,8 +84,8 @@ public class ApolloClientRegisterRepository implements 
ShenyuClientRegisterRepos
                              final String contextPath,
                              final URIRegisterDTO registerDTO) {
         String uriNodeName = buildURINodeName(registerDTO);
-        String uriPath = RegisterPathConstants.buildURIParentKey(rpcType, 
contextPath);
-        String realNode = RegisterPathConstants.buildNodeName(uriPath, 
uriNodeName);
+        String uriPath = RegisterPathConstants.buildURIParentPath(rpcType, 
contextPath);
+        String realNode = RegisterPathConstants.buildRealNode(uriPath, 
uriNodeName);
         apolloClient.createOrUpdateItem(realNode, 
GsonUtils.getInstance().toJson(registerDTO), "register uri");
         apolloClient.publishNamespace("publish config", "");
         LOGGER.info("register uri data success: {}", realNode);
@@ -100,16 +97,25 @@ public class ApolloClientRegisterRepository implements 
ShenyuClientRegisterRepos
         return String.join(Constants.COLONS, host, Integer.toString(port));
     }
 
-    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);
-        try {
-            this.apolloClient.createOrUpdateItem(configName, 
GsonUtils.getInstance().toJson(metadataCache), "register config");
-            this.apolloClient.publishNamespace("publish config", "");
-        } catch (Exception e) {
-            throw new ShenyuException(e);
+    private void registerMetadata(final MetaDataRegisterDTO metadata) {
+        String rpcType = metadata.getRpcType();
+        String contextPath = 
ContextPathUtils.buildRealNode(metadata.getContextPath(), 
metadata.getAppName());
+        String metadataNodeName = 
RegisterPathConstants.buildNodeName(metadata.getServiceName(), 
metadata.getMethodName());
+        String metaDataPath = 
RegisterPathConstants.buildMetaDataParentPath(rpcType, contextPath);
+        String realNode = RegisterPathConstants.buildRealNode(metaDataPath, 
metadataNodeName);
+
+        String oldValue = apolloClient.getItemValue(realNode);
+        // no change in metadata, no need to update
+        if (oldValue != null) {
+            MetaDataRegisterDTO oldMetaData = 
GsonUtils.getInstance().fromJson(oldValue, MetaDataRegisterDTO.class);
+            if (Objects.equals(oldMetaData, metadata)) {
+                return;
+            }
         }
+        // update metadata
+        String metadataJson = GsonUtils.getInstance().toJson(metadata);
+        this.apolloClient.createOrUpdateItem(realNode, metadataJson, "register 
config");
+        this.apolloClient.publishNamespace("publish config", "");
     }
+
 }
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 242a7c4bb..cd8059ff5 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
@@ -18,7 +18,7 @@
 package org.apache.shenyu.register.common.path;
 
 /**
- * zookeeper register center.
+ *  register center path constants.
  */
 public class RegisterPathConstants {
 
@@ -35,10 +35,20 @@ public class RegisterPathConstants {
     public static final String REGISTER_METADATA_INSTANCE_PATH = 
"/shenyu/register/metadata/*/*/*";
 
     /**
-     * root path of zookeeper register center.
+     * root path of  register center.
      */
     public static final String ROOT_PATH = "/shenyu/register";
 
+    /**
+     * root path of uri register.
+     */
+    public static final String REGISTER_URI_INSTANCE_ROOT_PATH = ROOT_PATH + 
"/uri";
+
+    /**
+     * root path of metadata register.
+     */
+    public static final String REGISTER_METADATA_INSTANCE_ROOT_PATH = 
ROOT_PATH + "/metadata";
+
     /**
      * constants of separator.
      */
@@ -56,7 +66,7 @@ public class RegisterPathConstants {
      * @return path string
      */
     public static String buildMetaDataContextPathParent(final String rpcType) {
-        return String.join(SEPARATOR, ROOT_PATH, "metadata", rpcType);
+        return String.join(SEPARATOR, REGISTER_METADATA_INSTANCE_ROOT_PATH, 
rpcType);
     }
     
     /**
@@ -67,7 +77,7 @@ public class RegisterPathConstants {
      * @return path string
      */
     public static String buildMetaDataParentPath(final String rpcType, final 
String contextPath) {
-        return String.join(SEPARATOR, ROOT_PATH, "metadata", rpcType, 
contextPath);
+        return String.join(SEPARATOR, REGISTER_METADATA_INSTANCE_ROOT_PATH, 
rpcType, contextPath);
     }
     
     /**
@@ -78,7 +88,7 @@ public class RegisterPathConstants {
      * @return the string
      */
     public static String buildURIContextPathParent(final String rpcType) {
-        return String.join(SEPARATOR, ROOT_PATH, "uri", rpcType);
+        return String.join(SEPARATOR, REGISTER_URI_INSTANCE_ROOT_PATH, 
rpcType);
     }
     
     /**
@@ -90,7 +100,7 @@ public class RegisterPathConstants {
      * @return the string
      */
     public static String buildURIParentPath(final String rpcType, final String 
contextPath) {
-        return String.join(SEPARATOR, ROOT_PATH, "uri", rpcType, contextPath);
+        return String.join(SEPARATOR, REGISTER_URI_INSTANCE_ROOT_PATH, 
rpcType, contextPath);
     }
     
     /**
@@ -166,28 +176,4 @@ public class RegisterPathConstants {
         return String.join(DOT_SEPARATOR, serviceName, methodName);
     }
 
-    /**
-     * Build apollo config uri parent path string.
-     * build child path of "shenyu.register.uri.{rpcType}.{contextPath}".
-     *
-     * @param rpcType the rpc type
-     * @param contextPath the context path
-     * @return the string
-     */
-    public static String buildURIParentKey(final String rpcType, final String 
contextPath) {
-        return String.join(DOT_SEPARATOR, "shenyu.register", "uri", rpcType, 
contextPath);
-    }
-
-
-    /**
-     * Build apollo config metadata parent path string.
-     * build child path of "shenyu.register.metadata.{rpcType}.{contextPath}".
-     *
-     * @param rpcType the rpc type
-     * @param contextPath the context path
-     * @return the string
-     */
-    public static String buildMetadataParentKey(final String rpcType, final 
String contextPath) {
-        return String.join(DOT_SEPARATOR, "shenyu.register", "metadata", 
rpcType, contextPath);
-    }
 }

Reply via email to