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 2023/02/21 04:03:49 UTC

[shenyu] branch master updated: [type:refactor] refactor code (#4400)

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 11d542bec [type:refactor] refactor code (#4400)
11d542bec is described below

commit 11d542becb1d8b64be5ffc5cc6e063a4f5c36e42
Author: mahaitao <15...@163.com>
AuthorDate: Tue Feb 21 12:03:43 2023 +0800

    [type:refactor] refactor code (#4400)
    
    * feat:refine code
    
    * feat:fix
    
    * feat:fix
    
    ---------
    
    Co-authored-by: mahaitao617 <ma...@mahaitao617deMacBook-Pro.local>
---
 .../server/consul/ShenyuConsulConfigWatch.java     | 51 +++++++++++-----------
 1 file changed, 25 insertions(+), 26 deletions(-)

diff --git a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ShenyuConsulConfigWatch.java b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ShenyuConsulConfigWatch.java
index 34c00fdbe..ce1f9ed5b 100644
--- a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ShenyuConsulConfigWatch.java
+++ b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ShenyuConsulConfigWatch.java
@@ -21,6 +21,7 @@ import com.ecwid.consul.v1.ConsulClient;
 import com.ecwid.consul.v1.QueryParams;
 import com.ecwid.consul.v1.Response;
 import com.ecwid.consul.v1.kv.model.GetValue;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
 import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
 import org.slf4j.Logger;
@@ -81,38 +82,36 @@ public class ShenyuConsulConfigWatch implements SmartLifecycle {
     }
 
     private void watchConfigKeyValues() {
-        if (this.running.get()) {
-            for (String context : this.consulIndexes.keySet()) {
-                try {
-                    Long currentIndex = this.consulIndexes.get(context);
-                    if (currentIndex == null) {
-                        currentIndex = -1L;
-                    }
-                    Response<List<GetValue>> response = this.consul.getKVValues(context, null, new QueryParams(waitTime, currentIndex));
-                    if (response.getValue() != null && !response.getValue().isEmpty()) {
-                        Long newIndex = response.getConsulIndex();
-
-                        if (Objects.nonNull(newIndex) && !newIndex.equals(currentIndex)) {
-                            if (!this.consulIndexes.containsValue(newIndex)
-                                    && !currentIndex.equals(-1L)) {
-                                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);
-                            }
-                            this.consulIndexes.put(context, newIndex);
+        if (!this.running.get()) {
+            return;
+        }
+        consulIndexes.keySet().forEach(context -> {
+            try {
+                Long currentIndex = this.consulIndexes.computeIfAbsent(context, k -> -1L);
+                Response<List<GetValue>> response = this.consul.getKVValues(context, null, new QueryParams(waitTime, currentIndex));
+                if (CollectionUtils.isNotEmpty(response.getValue())) {
+                    Long newIndex = response.getConsulIndex();
+
+                    if (Objects.nonNull(newIndex) && !newIndex.equals(currentIndex)) {
+                        if (!this.consulIndexes.containsValue(newIndex)
+                                && !currentIndex.equals(-1L)) {
+                            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.trace("Same index for context {}", context);
+                            LOGGER.info("Event for index already published for context {}", context);
                         }
+                        this.consulIndexes.put(context, newIndex);
                     } else if (LOGGER.isTraceEnabled()) {
-                        LOGGER.trace("No value for context {}", context);
+                        LOGGER.trace("Same index for context {}", context);
                     }
-                } catch (Exception e) {
-                    LOGGER.warn("Error querying consul Key/Values for context '{}'. Message: {}", context, e.getMessage());
+                } else if (LOGGER.isTraceEnabled()) {
+                    LOGGER.warn("No value for context {}", context);
                 }
+            } catch (Exception e) {
+                LOGGER.error("Error querying consul Key/Values for context '{}'. Message: {}", context, e.getMessage());
             }
-        }
+        });
     }
 
     @Override