You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/02 03:48:12 UTC

[incubator-shenyu] branch master updated: [ISSUE #3475] Fix sofa, motan and dubbo don't work issue caused by Curator version incompatible in bootstrap (#3476)

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/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new e30b66701 [ISSUE #3475] Fix sofa, motan and dubbo don't work issue caused by Curator version incompatible in bootstrap (#3476)
e30b66701 is described below

commit e30b6670139dda90c8ff66c23f8635a14b4514e2
Author: Han Gao <dh...@hotmail.com>
AuthorDate: Thu Jun 2 11:48:05 2022 +0800

    [ISSUE #3475] Fix sofa, motan and dubbo don't work issue caused by Curator version incompatible in bootstrap (#3476)
    
    * Fix sofa, motan and dubbo don't work issue caused by Curator version incompatible in bootstrap
    
    * update bootstrap LICENSE file
---
 .../src/main/release-docs/LICENSE                  |  6 +-
 .../sync/data/zookeeper/ZookeeperClient.java       | 38 +++++++-----
 .../data/zookeeper/ZookeeperSyncDataService.java   | 68 ++++++++++++++--------
 3 files changed, 68 insertions(+), 44 deletions(-)

diff --git a/shenyu-dist/shenyu-bootstrap-dist/src/main/release-docs/LICENSE b/shenyu-dist/shenyu-bootstrap-dist/src/main/release-docs/LICENSE
index 8a7f734a9..1b708c3c0 100644
--- a/shenyu-dist/shenyu-bootstrap-dist/src/main/release-docs/LICENSE
+++ b/shenyu-dist/shenyu-bootstrap-dist/src/main/release-docs/LICENSE
@@ -236,9 +236,9 @@ The text of each license is the standard Apache 2.0 license.
     commons-jxpath 1.3: http://commons.apache.org/jxpath/, Apache 2.0
     commons-math 2.2: https://github.com/apache/commons-math, Apache 2.0
     conscrypt-openjdk-uber 2.5.1: https://conscrypt.org, Apache 2.0
-    curator-client 5.2.1:  https://github.com/apache/curator, Apache 2.0
-    curator-framework 5.2.1:  https://github.com/apache/curator, Apache 2.0
-    curator-recipes 5.2.1:  https://github.com/apache/curator, Apache 2.0
+    curator-client 4.0.1:  https://github.com/apache/curator, Apache 2.0
+    curator-framework 4.0.1:  https://github.com/apache/curator, Apache 2.0
+    curator-recipes 4.0.1:  https://github.com/apache/curator, Apache 2.0
     dubbo 2.6.5: https://github.com/apache/dubbo, Apache 2.0
     error_prone_annotations 2.5.1: https://github.com/google/error-prone, Apache 2.0
     failsafe 2.3.3: https://github.com/jhalterman/failsafe, Apache 2.0
diff --git a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java
index a4b9fbdf5..b49ab5e1f 100644
--- a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java
+++ b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperClient.java
@@ -21,8 +21,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.recipes.cache.ChildData;
-import org.apache.curator.framework.recipes.cache.CuratorCache;
-import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
+import org.apache.curator.framework.recipes.cache.TreeCache;
+import org.apache.curator.framework.recipes.cache.TreeCacheListener;
 import org.apache.curator.retry.ExponentialBackoffRetry;
 import org.apache.curator.utils.CloseableUtils;
 import org.apache.shenyu.common.exception.ShenyuException;
@@ -35,7 +35,6 @@ import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 
 public class ZookeeperClient {
@@ -46,7 +45,7 @@ public class ZookeeperClient {
 
     private final CuratorFramework client;
 
-    private final Map<String, CuratorCache> caches = new ConcurrentHashMap<>();
+    private final Map<String, TreeCache> caches = new ConcurrentHashMap<>();
 
     public ZookeeperClient(final ZookeeperConfig zookeeperConfig) {
         this.config = zookeeperConfig;
@@ -84,7 +83,7 @@ public class ZookeeperClient {
      */
     public void close() {
         // close all caches
-        for (Map.Entry<String, CuratorCache> cache : caches.entrySet()) {
+        for (Map.Entry<String, TreeCache> cache : caches.entrySet()) {
             CloseableUtils.closeQuietly(cache.getValue());
         }
         // close client
@@ -136,12 +135,15 @@ public class ZookeeperClient {
      * @return value.
      */
     public String get(final String key) {
-        CuratorCache cache = findFromcache(key);
+        TreeCache cache = findFromcache(key);
         if (Objects.isNull(cache)) {
             return getDirectly(key);
         }
-        Optional<ChildData> data = cache.get(key);
-        return data.map(childData -> new String(childData.getData(), StandardCharsets.UTF_8)).orElseGet(() -> getDirectly(key));
+        ChildData data = cache.getCurrentData(key);
+        if (Objects.isNull(data)) {
+            return getDirectly(key);
+        }
+        return Objects.isNull(data.getData()) ? null : new String(data.getData(), StandardCharsets.UTF_8);
     }
 
     /**
@@ -208,7 +210,7 @@ public class ZookeeperClient {
      * @param path path.
      * @return cache.
      */
-    public CuratorCache getCache(final String path) {
+    public TreeCache getCache(final String path) {
         return caches.get(path);
     }
 
@@ -218,15 +220,19 @@ public class ZookeeperClient {
      * @param listeners listeners.
      * @return cache.
      */
-    public CuratorCache addCache(final String path, final CuratorCacheListener... listeners) {
-        CuratorCache cache = CuratorCache.build(client, path);
+    public TreeCache addCache(final String path, final TreeCacheListener... listeners) {
+        TreeCache cache = TreeCache.newBuilder(client, path).build();
         caches.put(path, cache);
         if (listeners != null && listeners.length > 0) {
-            for (CuratorCacheListener listener : listeners) {
-                cache.listenable().addListener(listener);
+            for (TreeCacheListener listener : listeners) {
+                cache.getListenable().addListener(listener);
             }
         }
-        cache.start();
+        try {
+            cache.start();
+        } catch (Exception e) {
+            throw new ShenyuException("failed to add curator cache.", e);
+        }
         return cache;
     }
 
@@ -235,8 +241,8 @@ public class ZookeeperClient {
      * @param key key.
      * @return cache.
      */
-    private CuratorCache findFromcache(final String key) {
-        for (Map.Entry<String, CuratorCache> cache : caches.entrySet()) {
+    private TreeCache findFromcache(final String key) {
+        for (Map.Entry<String, TreeCache> cache : caches.entrySet()) {
             if (key.startsWith(cache.getKey())) {
                 return cache.getValue();
             }
diff --git a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperSyncDataService.java b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperSyncDataService.java
index 8252d6b93..2a8573176 100644
--- a/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperSyncDataService.java
+++ b/shenyu-sync-data-center/shenyu-sync-data-zookeeper/src/main/java/org/apache/shenyu/sync/data/zookeeper/ZookeeperSyncDataService.java
@@ -18,9 +18,12 @@
 package org.apache.shenyu.sync.data.zookeeper;
 
 import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
+import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.recipes.cache.ChildData;
-import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
+import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
+import org.apache.curator.framework.recipes.cache.TreeCacheListener;
 import org.apache.shenyu.common.constant.DefaultPathConstants;
 import org.apache.shenyu.common.dto.AppAuthData;
 import org.apache.shenyu.common.dto.MetaData;
@@ -168,14 +171,36 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
         }
     }
 
-    class PluginCacheListener implements CuratorCacheListener {
+    abstract class AbstractDataSyncListener implements TreeCacheListener {
+        @Override
+        public final void childEvent(final CuratorFramework client, final TreeCacheEvent event) {
+            ChildData childData = event.getData();
+            if (null == childData) {
+                return;
+            }
+            String path = childData.getPath();
+            if (Strings.isNullOrEmpty(path)) {
+                return;
+            }
+            event(event.getType(), path, childData);
+        }
+
+        /**
+         * data sync event.
+         *
+         * @param type tree cache event type.
+         * @param path tree cache event path.
+         * @param data tree cache event data.
+         */
+        protected abstract void event(TreeCacheEvent.Type type, String path, ChildData data);
+    }
+
+    class PluginCacheListener extends AbstractDataSyncListener {
 
         private static final String PLUGIN_PATH = DefaultPathConstants.PLUGIN_PARENT + "/*";
 
         @Override
-        public void event(final Type type, final ChildData oldData, final ChildData data) {
-            String path = Objects.isNull(data) ? oldData.getPath() : data.getPath();
-
+        public void event(final TreeCacheEvent.Type type, final String path, final ChildData data) {
             // if not uri register path, return.
             if (!PathMatchUtils.match(PLUGIN_PATH, path)) {
                 return;
@@ -184,7 +209,7 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
             String pluginName = path.substring(path.lastIndexOf("/") + 1);
 
             // delete a plugin
-            if (type.equals(Type.NODE_DELETED)) {
+            if (type.equals(TreeCacheEvent.Type.NODE_REMOVED)) {
                 final PluginData pluginData = new PluginData();
                 pluginData.setName(pluginName);
                 Optional.ofNullable(pluginDataSubscriber).ifPresent(e -> e.unSubscribe(pluginData));
@@ -196,21 +221,20 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
         }
     }
 
-    class SelectorCacheListener implements CuratorCacheListener {
+    class SelectorCacheListener extends AbstractDataSyncListener {
 
         // /shenyu/selector/{plugin}/{selector}
         private static final String SELECTOR_PATH = DefaultPathConstants.SELECTOR_PARENT + "/*/*";
 
         @Override
-        public void event(final Type type, final ChildData oldData, final ChildData data) {
-            String path = Objects.isNull(data) ? oldData.getPath() : data.getPath();
+        public void event(final TreeCacheEvent.Type type, final String path, final ChildData data) {
 
             // if not uri register path, return.
             if (!PathMatchUtils.match(SELECTOR_PATH, path)) {
                 return;
             }
 
-            if (type.equals(Type.NODE_DELETED)) {
+            if (type.equals(TreeCacheEvent.Type.NODE_REMOVED)) {
                 unCacheSelectorData(path);
             }
 
@@ -220,20 +244,18 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
         }
     }
 
-    class MetadataCacheListener implements CuratorCacheListener {
+    class MetadataCacheListener extends AbstractDataSyncListener {
 
         private static final String META_DATA_PATH = DefaultPathConstants.META_DATA + "/*";
 
         @Override
-        public void event(final Type type, final ChildData oldData, final ChildData data) {
-            String path = Objects.isNull(data) ? oldData.getPath() : data.getPath();
-
+        public void event(final TreeCacheEvent.Type type, final String path, final ChildData data) {
             // if not uri register path, return.
             if (!PathMatchUtils.match(META_DATA_PATH, path)) {
                 return;
             }
 
-            if (type.equals(Type.NODE_DELETED)) {
+            if (type.equals(TreeCacheEvent.Type.NODE_REMOVED)) {
                 final String realPath = path.substring(DefaultPathConstants.META_DATA.length() + 1);
                 MetaData metaData = new MetaData();
                 try {
@@ -250,20 +272,18 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
         }
     }
 
-    class AuthCacheListener implements CuratorCacheListener {
+    class AuthCacheListener extends AbstractDataSyncListener {
 
         private static final String APP_AUTH_PATH = DefaultPathConstants.APP_AUTH_PARENT + "/*";
 
         @Override
-        public void event(final Type type, final ChildData oldData, final ChildData data) {
-            String path = Objects.isNull(data) ? oldData.getPath() : data.getPath();
-
+        public void event(final TreeCacheEvent.Type type, final String path, final ChildData data) {
             // if not uri register path, return.
             if (!PathMatchUtils.match(APP_AUTH_PATH, path)) {
                 return;
             }
 
-            if (type.equals(Type.NODE_DELETED)) {
+            if (type.equals(TreeCacheEvent.Type.NODE_REMOVED)) {
                 unCacheAuthData(path);
             }
 
@@ -273,21 +293,19 @@ public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable
         }
     }
 
-    class RuleCacheListener implements CuratorCacheListener {
+    class RuleCacheListener extends AbstractDataSyncListener {
 
         // /shenyu/rule/{plugin}/{rule}
         private static final String RULE_PATH = DefaultPathConstants.RULE_PARENT + "/*/*";
 
         @Override
-        public void event(final Type type, final ChildData oldData, final ChildData data) {
-            String path = Objects.isNull(data) ? oldData.getPath() : data.getPath();
-
+        public void event(final TreeCacheEvent.Type type, final String path, final ChildData data) {
             // if not uri register path, return.
             if (!PathMatchUtils.match(RULE_PATH, path)) {
                 return;
             }
 
-            if (type.equals(Type.NODE_DELETED)) {
+            if (type.equals(TreeCacheEvent.Type.NODE_REMOVED)) {
                 unCacheRuleData(path);
             }