This is an automated email from the ASF dual-hosted git repository.
Aias00 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 65ce5a433f fix(plugin-apache-dubbo): refresh registry and consumer
configuration. (#7554)
65ce5a433f is described below
commit 65ce5a433f19075ebe86fb1a730ce9b64a67faad
Author: redyyang <[email protected]>
AuthorDate: Tue Oct 13 10:14:40 2026 +0800
fix(plugin-apache-dubbo): refresh registry and consumer configuration.
(#7554)
Co-authored-by: redyyang <[email protected]>
---
.../apache/dubbo/cache/ApacheDubboConfigCache.java | 28 +++--
.../dubbo/cache/ApacheDubboConfigCacheTest.java | 135 +++++++++++++++++++++
.../handler/ApacheDubboPluginDataHandlerTest.java | 50 ++++++++
3 files changed, 205 insertions(+), 8 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
index be07938593..66d4b1947a 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
@@ -162,13 +162,15 @@ public final class ApacheDubboConfigCache extends
DubboConfigCache {
Optional.ofNullable(dubboRegisterConfig.getGroup()).ifPresent(registryConfigTemp::setGroup);
registryConfig = registryConfigTemp;
}
- if (Objects.isNull(consumerConfig)) {
- consumerConfig = new ConsumerConfig();
- consumerConfig.refresh();
-
Optional.ofNullable(dubboRegisterConfig.getThreadpool()).ifPresent(consumerConfig::setThreadpool);
-
Optional.ofNullable(dubboRegisterConfig.getCorethreads()).ifPresent(consumerConfig::setCorethreads);
-
Optional.ofNullable(dubboRegisterConfig.getThreads()).ifPresent(consumerConfig::setThreads);
-
Optional.ofNullable(dubboRegisterConfig.getQueues()).ifPresent(consumerConfig::setQueues);
+ // Refresh a new config so removed overrides fall back to Dubbo
defaults instead of stale values.
+ ConsumerConfig consumerConfigTemp = new ConsumerConfig();
+ consumerConfigTemp.refresh();
+
Optional.ofNullable(dubboRegisterConfig.getThreadpool()).ifPresent(consumerConfigTemp::setThreadpool);
+
Optional.ofNullable(dubboRegisterConfig.getCorethreads()).ifPresent(consumerConfigTemp::setCorethreads);
+
Optional.ofNullable(dubboRegisterConfig.getThreads()).ifPresent(consumerConfigTemp::setThreads);
+
Optional.ofNullable(dubboRegisterConfig.getQueues()).ifPresent(consumerConfigTemp::setQueues);
+ if (needUpdateConsumerConfig(consumerConfigTemp)) {
+ consumerConfig = consumerConfigTemp;
}
}
@@ -177,7 +179,16 @@ public final class ApacheDubboConfigCache extends
DubboConfigCache {
return true;
}
return !Objects.equals(dubboRegisterConfig.getProtocol(),
registryConfig.getProtocol())
- || !Objects.equals(dubboRegisterConfig.getRegister(),
registryConfig.getAddress());
+ || !Objects.equals(dubboRegisterConfig.getRegister(),
registryConfig.getAddress())
+ || !Objects.equals(dubboRegisterConfig.getGroup(),
registryConfig.getGroup());
+ }
+
+ private boolean needUpdateConsumerConfig(final ConsumerConfig
updatedConfig) {
+ return Objects.isNull(consumerConfig)
+ || !Objects.equals(updatedConfig.getThreadpool(),
consumerConfig.getThreadpool())
+ || !Objects.equals(updatedConfig.getCorethreads(),
consumerConfig.getCorethreads())
+ || !Objects.equals(updatedConfig.getThreads(),
consumerConfig.getThreads())
+ || !Objects.equals(updatedConfig.getQueues(),
consumerConfig.getQueues());
}
/**
@@ -475,6 +486,7 @@ public final class ApacheDubboConfigCache extends
DubboConfigCache {
private void changeRegistryAddressNamespace(final RegistryConfig
currentRegistryConfig, final ReferenceConfig<GenericService> reference, final
String namespace) {
RegistryConfig registryConfigNew = new RegistryConfig();
registryConfigNew.setRegister(false);
+ registryConfigNew.setGroup(currentRegistryConfig.getGroup());
registryConfigNew.setAddress(URL.valueOf(currentRegistryConfig.getAddress())
.addParameter(Constants.NAMESPACE, namespace)
.toFullString());
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCacheTest.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCacheTest.java
index 82ee981096..6d25112115 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCacheTest.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCacheTest.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.plugin.apache.dubbo.cache;
import com.google.common.cache.LoadingCache;
import org.apache.dubbo.common.URL;
+import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
@@ -34,6 +35,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@@ -41,11 +43,13 @@ import org.springframework.test.util.ReflectionTestUtils;
import java.lang.reflect.Field;
import java.util.List;
+import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -108,6 +112,137 @@ public final class ApacheDubboConfigCacheTest {
assertNotSame(registryConfig, registryConfig1);
}
+ @ParameterizedTest
+ @ValueSource(strings = {"", "test-namespace"})
+ public void testInitAppliesGroupOnlyChanges(final String namespace) {
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ DubboRegisterConfig config = createRegisterConfig("fixed", 4, 32, 64);
+ config.setGroup("first-group");
+ configCache.init(config);
+ ReferenceConfig<GenericService> first = createReference(configCache,
namespace);
+
+ config.setGroup("second-group");
+ configCache.init(config);
+ ReferenceConfig<GenericService> second = createReference(configCache,
namespace);
+
+ assertEquals("first-group", first.getRegistry().getGroup());
+ assertEquals("second-group", second.getRegistry().getGroup());
+ assertEquals(config.getRegister(), ((RegistryConfig)
ReflectionTestUtils.getField(configCache, "registryConfig")).getAddress());
+ assertSame(first.getConsumer(), second.getConsumer());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"", "test-namespace"})
+ public void testInitClearsRegistryGroup(final String namespace) {
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ DubboRegisterConfig config = createRegisterConfig("fixed", 4, 32, 64);
+ config.setGroup("first-group");
+ configCache.init(config);
+ RegistryConfig first = (RegistryConfig)
ReflectionTestUtils.getField(configCache, "registryConfig");
+
+ config.setGroup(null);
+ configCache.init(config);
+
+ assertEquals("first-group", first.getGroup());
+ assertNull(((RegistryConfig) ReflectionTestUtils.getField(configCache,
"registryConfig")).getGroup());
+ assertNull(createReference(configCache,
namespace).getRegistry().getGroup());
+ }
+
+ @ParameterizedTest
+ @CsvSource({"cached, 4, 32, 64", "fixed, 8, 32, 64", "fixed, 4, 64, 64",
"fixed, 4, 32, 128"})
+ public void testInitUpdatesEachConsumerPoolField(final String threadpool,
final Integer corethreads, final Integer threads, final Integer queues) {
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ configCache.init(createRegisterConfig("fixed", 4, 32, 64));
+ final ReferenceConfig<GenericService> first =
createReference(configCache, "");
+
+ configCache.init(createRegisterConfig(threadpool, corethreads,
threads, queues));
+ ReferenceConfig<GenericService> second = createReference(configCache,
"");
+
+ assertEquals(threadpool, second.getConsumer().getThreadpool());
+ assertEquals(corethreads, second.getConsumer().getCorethreads());
+ assertEquals(threads, second.getConsumer().getThreads());
+ assertEquals(queues, second.getConsumer().getQueues());
+ assertEquals("fixed", first.getConsumer().getThreadpool());
+ assertEquals(4, first.getConsumer().getCorethreads());
+ assertEquals(32, first.getConsumer().getThreads());
+ assertEquals(64, first.getConsumer().getQueues());
+ assertSame(first.getRegistry(), second.getRegistry());
+ }
+
+ @ParameterizedTest
+ @CsvSource({", 4, 32, 64", "fixed, , 32, 64", "fixed, 4, , 64", "fixed, 4,
32, ", ", , , "})
+ public void testInitRestoresDefaultsForClearedConsumerFields(final String
threadpool, final Integer corethreads, final Integer threads, final Integer
queues) {
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ configCache.init(createRegisterConfig("fixed", 4, 32, 64));
+ ConsumerConfig defaults = new ConsumerConfig();
+ defaults.refresh();
+
+ configCache.init(createRegisterConfig(threadpool, corethreads,
threads, queues));
+ ConsumerConfig actual = createReference(configCache, "").getConsumer();
+
+ assertEquals(Objects.isNull(threadpool) ? defaults.getThreadpool() :
threadpool, actual.getThreadpool());
+ assertEquals(Objects.isNull(corethreads) ? defaults.getCorethreads() :
corethreads, actual.getCorethreads());
+ assertEquals(Objects.isNull(threads) ? defaults.getThreads() :
threads, actual.getThreads());
+ assertEquals(Objects.isNull(queues) ? defaults.getQueues() : queues,
actual.getQueues());
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {true, false})
+ public void testInitReusesUnchangedConfiguration(final boolean
explicitPoolConfig) {
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ DubboRegisterConfig config = explicitPoolConfig ?
createRegisterConfig("fixed", 4, 32, 64) : createRegisterConfig(null, null,
null, null);
+ config.setGroup("same-group");
+ configCache.init(config);
+ ReferenceConfig<GenericService> first = createReference(configCache,
"");
+
+
configCache.init(GsonUtils.getInstance().fromJson(GsonUtils.getInstance().toJson(config),
DubboRegisterConfig.class));
+ ReferenceConfig<GenericService> second = createReference(configCache,
"");
+
+ assertSame(first.getRegistry(), second.getRegistry());
+ assertSame(first.getConsumer(), second.getConsumer());
+ }
+
+ @Test
+ public void testInitRestoresConfiguredDubboDefault() {
+ final String property = "dubbo.consumer.threads";
+ final String previous = System.getProperty(property);
+ try {
+ System.setProperty(property, "19");
+ ConsumerConfig defaults = new ConsumerConfig();
+ defaults.refresh();
+ assertEquals(19, defaults.getThreads());
+ ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+ configCache.init(createRegisterConfig("fixed", 4, 32, 64));
+
+ configCache.init(createRegisterConfig("fixed", 4, null, 64));
+
+ assertEquals(19, createReference(configCache,
"").getConsumer().getThreads());
+ } finally {
+ if (Objects.isNull(previous)) {
+ System.clearProperty(property);
+ } else {
+ System.setProperty(property, previous);
+ }
+ }
+ }
+
+ private DubboRegisterConfig createRegisterConfig(final String threadpool,
final Integer corethreads, final Integer threads, final Integer queues) {
+ DubboRegisterConfig config = new DubboRegisterConfig();
+ config.setProtocol("zookeeper");
+ config.setRegister("zookeeper://127.0.0.1:2181");
+ config.setThreadpool(threadpool);
+ config.setCorethreads(corethreads);
+ config.setThreads(threads);
+ config.setQueues(queues);
+ return config;
+ }
+
+ private ReferenceConfig<GenericService> createReference(final
ApacheDubboConfigCache configCache, final String namespace) {
+ MetaData metaData = new MetaData();
+ metaData.setServiceName("org.apache.shenyu.test.DemoService");
+ return ReflectionTestUtils.invokeMethod(configCache, "buildReference",
metaData, namespace);
+ }
+
@Test
public void testInitRef() {
MetaData metaData = new MetaData();
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboPluginDataHandlerTest.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboPluginDataHandlerTest.java
index 9617889c97..96af1adfc6 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboPluginDataHandlerTest.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboPluginDataHandlerTest.java
@@ -17,16 +17,31 @@
package org.apache.shenyu.plugin.apache.dubbo.handler;
+import com.google.common.cache.LoadingCache;
+import org.apache.dubbo.config.ConsumerConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.rpc.service.GenericService;
import org.apache.shenyu.common.dto.convert.plugin.DubboRegisterConfig;
import org.apache.shenyu.common.dto.PluginData;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.plugin.apache.dubbo.cache.ApacheDubboConfigCache;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
/**
* The Test Case For ApacheDubboPluginDataHandler.
@@ -56,4 +71,39 @@ public final class ApacheDubboPluginDataHandlerTest {
public void pluginNamed() {
assertThat(apacheDubboPluginDataHandler.pluginNamed(),
is(PluginEnum.DUBBO.getName()));
}
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void configUpdateShouldApplyBeforeDestroyingReferences() {
+ final ApacheDubboConfigCache configCache = new
ApacheDubboConfigCache();
+ DubboRegisterConfig config = new DubboRegisterConfig();
+ config.setProtocol("zookeeper");
+ config.setRegister("zookeeper://127.0.0.1:2181");
+ config.setGroup("old-group");
+ config.setThreads(32);
+ configCache.init(config);
+ final LoadingCache<String, ReferenceConfig<GenericService>> references
=
+ (LoadingCache<String, ReferenceConfig<GenericService>>)
ReflectionTestUtils.getField(configCache, "cache");
+ ReferenceConfig<GenericService> reference =
mock(ReferenceConfig.class);
+ AtomicReference<String> groupAtRemoval = new AtomicReference<>();
+ AtomicReference<Integer> threadsAtRemoval = new AtomicReference<>();
+ doAnswer(invocation -> {
+ groupAtRemoval.set(((RegistryConfig)
ReflectionTestUtils.getField(configCache, "registryConfig")).getGroup());
+ threadsAtRemoval.set(((ConsumerConfig)
ReflectionTestUtils.getField(configCache, "consumerConfig")).getThreads());
+ return null;
+ }).when(reference).destroy();
+ references.put("old-reference", reference);
+
+ config.setGroup("new-group");
+ config.setThreads(64);
+ try (MockedStatic<ApacheDubboConfigCache> mocked =
mockStatic(ApacheDubboConfigCache.class)) {
+
mocked.when(ApacheDubboConfigCache::getInstance).thenReturn(configCache);
+ apacheDubboPluginDataHandler.initConfigCache(config);
+ }
+
+ verify(reference).destroy();
+ assertEquals("new-group", groupAtRemoval.get());
+ assertEquals(64, threadsAtRemoval.get());
+ assertTrue(references.asMap().isEmpty());
+ }
}