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 7925422b18 fix(dubbo): preserve registry address when rewriting 
namespace (#7425)
7925422b18 is described below

commit 7925422b181fbe06706e9ad611c533351d2a03b3
Author: lymerin <[email protected]>
AuthorDate: Sun Oct 4 19:17:36 2026 +0800

    fix(dubbo): preserve registry address when rewriting namespace (#7425)
---
 .../apache/dubbo/cache/ApacheDubboConfigCache.java | 10 ++--
 .../dubbo/cache/ApacheDubboConfigCacheTest.java    | 65 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 6 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 b058798985..be07938593 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
@@ -39,6 +39,7 @@ import jakarta.annotation.Nonnull;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.constants.CommonConstants;
 import org.apache.dubbo.config.ApplicationConfig;
 import org.apache.dubbo.config.ConsumerConfig;
@@ -474,12 +475,9 @@ 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);
-        if (!currentRegistryConfig.getAddress().contains(Constants.NAMESPACE)) 
{
-            registryConfigNew.setAddress(currentRegistryConfig.getAddress() + 
"?" + Constants.NAMESPACE + "=" + namespace);
-        } else {
-            String newAddress = 
currentRegistryConfig.getAddress().substring(0, 
currentRegistryConfig.getAddress().indexOf(Constants.NAMESPACE) + 1) + 
Constants.NAMESPACE + "=" + namespace;
-            registryConfigNew.setAddress(newAddress);
-        }
+        
registryConfigNew.setAddress(URL.valueOf(currentRegistryConfig.getAddress())
+                .addParameter(Constants.NAMESPACE, namespace)
+                .toFullString());
         reference.setRegistry(registryConfigNew);
     }
 
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 7280e31034..82ee981096 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
@@ -18,10 +18,12 @@
 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.ReferenceConfig;
 import org.apache.dubbo.config.RegistryConfig;
 import org.apache.dubbo.rpc.service.GenericService;
 import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.common.dto.RuleData;
 import org.apache.shenyu.common.dto.SelectorData;
 import org.apache.shenyu.common.dto.convert.plugin.DubboRegisterConfig;
 import org.apache.shenyu.common.dto.convert.selector.DubboUpstream;
@@ -30,9 +32,12 @@ import 
org.apache.shenyu.plugin.dubbo.common.cache.DubboParam;
 import org.junit.jupiter.api.BeforeEach;
 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.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.junit.jupiter.MockitoSettings;
 import org.mockito.quality.Strictness;
+import org.springframework.test.util.ReflectionTestUtils;
 
 import java.lang.reflect.Field;
 import java.util.List;
@@ -135,6 +140,66 @@ public final class ApacheDubboConfigCacheTest {
         assertNotNull(apacheDubboConfigCacheMock.build(metaData, ""));
     }
 
+    @ParameterizedTest
+    @CsvSource({
+        "zookeeper://127.0.0.1:2181?namespace=old, 
zookeeper://127.0.0.1:2181?namespace=new",
+        "zookeeper://127.0.0.1:2181, zookeeper://127.0.0.1:2181?namespace=new",
+        "zookeeper://127.0.0.1:2181?group=g, 
zookeeper://127.0.0.1:2181?group=g&namespace=new",
+        "zookeeper://127.0.0.1:2181?namespace=old&group=g&timeout=5000, 
zookeeper://127.0.0.1:2181?namespace=new&group=g&timeout=5000",
+        "zookeeper://127.0.0.1:2181?group=g&namespace=old&timeout=5000, 
zookeeper://127.0.0.1:2181?group=g&namespace=new&timeout=5000",
+        "zookeeper://my-namespace-svc:2181, 
zookeeper://my-namespace-svc:2181?namespace=new",
+        "zookeeper://127.0.0.1:2181/namespace?group=namespace, 
zookeeper://127.0.0.1:2181/namespace?group=namespace&namespace=new",
+        "zookeeper://user:[email protected]:2181?group=g%26x, 
zookeeper://user:[email protected]:2181?group=g%26x&namespace=new"
+    })
+    public void testBuildReferenceWithNamespace(final String address, final 
String expectedAddress) {
+        ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+        RegistryConfig originalRegistry = new RegistryConfig();
+        originalRegistry.setAddress(address);
+        ReflectionTestUtils.setField(configCache, "registryConfig", 
originalRegistry);
+        MetaData metaData = new MetaData();
+        metaData.setServiceName("org.apache.shenyu.test.DemoService");
+
+        ReferenceConfig<GenericService> reference = 
ReflectionTestUtils.invokeMethod(configCache, "buildReference", metaData, 
"new");
+
+        assertNotNull(reference);
+        assertRegistryAddress(expectedAddress, 
reference.getRegistry().getAddress());
+        assertNotSame(originalRegistry, reference.getRegistry());
+        assertEquals(address, originalRegistry.getAddress());
+        assertSame(originalRegistry, ReflectionTestUtils.getField(configCache, 
"registryConfig"));
+        assertFalse(reference.getRegistry().isRegister());
+    }
+
+    @Test
+    public void testBuildUpstreamReferenceWithNamespace() {
+        DubboUpstream upstream = 
DubboUpstream.builder().protocol("zookeeper").build();
+        String address = 
"zookeeper://127.0.0.1:2181?namespace=old&group=g&timeout=5000";
+        upstream.setRegistry(address);
+        MetaData metaData = new MetaData();
+        metaData.setServiceName("org.apache.shenyu.test.DemoService");
+        RuleData ruleData = new RuleData();
+        ruleData.setId("namespace-rewrite-rule");
+
+        ApacheDubboConfigCache configCache = new ApacheDubboConfigCache();
+        ReferenceConfig<GenericService> reference = 
ReflectionTestUtils.invokeMethod(configCache, "buildReference", metaData, 
ruleData, "new", upstream);
+
+        assertNotNull(reference);
+        
assertRegistryAddress("zookeeper://127.0.0.1:2181?namespace=new&group=g&timeout=5000",
 reference.getRegistry().getAddress());
+        assertEquals(address, upstream.getRegistry());
+        assertFalse(reference.getRegistry().isRegister());
+    }
+
+    private void assertRegistryAddress(final String expectedAddress, final 
String actualAddress) {
+        URL expected = URL.valueOf(expectedAddress);
+        URL actual = URL.valueOf(actualAddress);
+        assertEquals(expected.getProtocol(), actual.getProtocol());
+        assertEquals(expected.getHost(), actual.getHost());
+        assertEquals(expected.getPort(), actual.getPort());
+        assertEquals(expected.getPath(), actual.getPath());
+        assertEquals(expected.getUsername(), actual.getUsername());
+        assertEquals(expected.getPassword(), actual.getPassword());
+        assertEquals(expected.getParameters(), actual.getParameters());
+    }
+
     @Test
     public void testInvalidate() {
         this.apacheDubboConfigCache.invalidate("/test");

Reply via email to