From 22b4a8cd9579be01eb6083af330e445a9568f99c Mon Sep 17 00:00:00 2001 From: xuziyang <767637918@qq.com> Date: Sun, 16 Jul 2023 23:17:40 +0800 Subject: [PATCH] [ISSUE #4686] Admin actively synchronizes http service offline events to the gateway (#4846) * [ISSUE #4686] divide plugin active offline support shenyu-admin cluster * Revert "[ISSUE #4686] divide plugin active offline support shenyu-admin cluster" This reverts commit 9812311d036aa7be50614c7131794cbd91d09786. * [ISSUE #4686] Admin actively synchronizes http service offline events to the gateway --------- Co-authored-by: dragon-zhang --- .../URIRegisterExecutorSubscriber.java | 8 ++++++-- .../cache/UpstreamCacheManager.java | 20 +++++++------------ .../handler/DividePluginDataHandler.java | 6 +++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/URIRegisterExecutorSubscriber.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/URIRegisterExecutorSubscriber.java index 5c08bf6de09a..e41e6e26efa6 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/URIRegisterExecutorSubscriber.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/URIRegisterExecutorSubscriber.java @@ -82,8 +82,12 @@ public void executor(final Collection dataList) { offline.add(d); } } - service.registerURI(selectorName, register); - service.offline(selectorName, offline); + if (CollectionUtils.isNotEmpty(register)) { + service.registerURI(selectorName, register); + } + if (CollectionUtils.isNotEmpty(offline)) { + service.offline(selectorName, offline); + } }); }); } diff --git a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java index 630b9f15588f..f5a27b8d6b93 100644 --- a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java +++ b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCacheManager.java @@ -19,7 +19,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.apache.commons.collections4.CollectionUtils; import org.apache.shenyu.common.concurrent.ShenyuThreadFactory; import org.apache.shenyu.common.config.ShenyuConfig; import org.apache.shenyu.common.config.ShenyuConfig.UpstreamCheck; @@ -139,21 +138,16 @@ public void removeByKey(final String key) { /** * Submit . * - * @param selectorId the selector id + * @param selectorId the selector id * @param upstreamList the upstream list */ public void submit(final String selectorId, final List upstreamList) { List validUpstreamList = upstreamList.stream().filter(Upstream::isStatus).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(validUpstreamList)) { - List existUpstream = MapUtils.computeIfAbsent(UPSTREAM_MAP, selectorId, k -> Lists.newArrayList()); - existUpstream.stream().filter(upstream -> !validUpstreamList.contains(upstream)) - .forEach(upstream -> task.triggerRemoveOne(selectorId, upstream)); - validUpstreamList.stream().filter(upstream -> !existUpstream.contains(upstream)) - .forEach(upstream -> task.triggerAddOne(selectorId, upstream)); - UPSTREAM_MAP.put(selectorId, validUpstreamList); - } else { - UPSTREAM_MAP.remove(selectorId); - task.triggerRemoveAll(selectorId); - } + List existUpstream = MapUtils.computeIfAbsent(UPSTREAM_MAP, selectorId, k -> Lists.newArrayList()); + existUpstream.stream().filter(upstream -> !validUpstreamList.contains(upstream)) + .forEach(upstream -> task.triggerRemoveOne(selectorId, upstream)); + validUpstreamList.stream().filter(upstream -> !existUpstream.contains(upstream)) + .forEach(upstream -> task.triggerAddOne(selectorId, upstream)); + UPSTREAM_MAP.put(selectorId, validUpstreamList); } } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandler.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandler.java index 1dda62a41cf9..e43ec6bfd6b8 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandler.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandler.java @@ -17,7 +17,6 @@ package org.apache.shenyu.plugin.divide.handler; -import org.apache.commons.collections4.CollectionUtils; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.RuleData; import org.apache.shenyu.common.dto.SelectorData; @@ -34,6 +33,7 @@ import org.apache.shenyu.plugin.base.utils.CacheKeyUtils; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -47,10 +47,10 @@ public class DividePluginDataHandler implements PluginDataHandler { @Override public void handlerSelector(final SelectorData selectorData) { - List upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), DivideUpstream.class); - if (CollectionUtils.isEmpty(upstreamList)) { + if (Objects.isNull(selectorData) || Objects.isNull(selectorData.getId())) { return; } + List upstreamList = GsonUtils.getInstance().fromList(selectorData.getHandle(), DivideUpstream.class); UpstreamCacheManager.getInstance().submit(selectorData.getId(), convertUpstreamList(upstreamList)); // the update is also need to clean, but there is no way to // distinguish between crate and update, so it is always clean