From c11f90b51c2c1bc3194d032483ce3e3499867086 Mon Sep 17 00:00:00 2001 From: somiljain2006 Date: Mon, 23 Mar 2026 10:15:54 +0530 Subject: [PATCH 1/2] Support custom registry ip and port for Nacos registration --- .../nacos/NacosRegistryServiceImpl.java | 58 +++++++++- .../nacos/NacosRegistryServiceImplTest.java | 101 +++++++++++++++--- 2 files changed, 140 insertions(+), 19 deletions(-) diff --git a/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java b/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java index d8649de50c8..1f16ec60958 100644 --- a/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java +++ b/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java @@ -61,6 +61,8 @@ public class NacosRegistryServiceImpl implements RegistryService private static final String PRO_APPLICATION_KEY = "application"; private static final String PRO_CLIENT_APPLICATION = "clientApplication"; private static final String PRO_GROUP_KEY = "group"; + private static final String PRO_IP_KEY = "ip"; + private static final String PRO_PORT_KEY = "port"; private static final String USER_NAME = "username"; private static final String PASSWORD = "password"; private static final String ACCESS_KEY = "accessKey"; @@ -118,27 +120,67 @@ static NacosRegistryServiceImpl getInstance() { @Override public void register(InetSocketAddress address) throws Exception { NetUtil.validAddress(address); + InetSocketAddress registryAddress = getRegistryAddress(address); getNamingInstance() .registerInstance( getServiceName(), getServiceGroup(), - address.getAddress().getHostAddress(), - address.getPort(), + registryAddress.getAddress().getHostAddress(), + registryAddress.getPort(), getClusterName()); } @Override public void unregister(InetSocketAddress address) throws Exception { NetUtil.validAddress(address); + InetSocketAddress registryAddress = getRegistryAddress(address); getNamingInstance() .deregisterInstance( getServiceName(), getServiceGroup(), - address.getAddress().getHostAddress(), - address.getPort(), + registryAddress.getAddress().getHostAddress(), + registryAddress.getPort(), getClusterName()); } + private InetSocketAddress getRegistryAddress(InetSocketAddress address) { + String ipKey = getNacosIpFileKey(); + String portKey = getNacosPortFileKey(); + + String overrideIp = System.getProperty(ipKey); + if (StringUtils.isBlank(overrideIp)) { + overrideIp = getFileConfig().getConfig(ipKey); + } + + int overridePort = 0; + String sysPort = System.getProperty(portKey); + if (StringUtils.isNotBlank(sysPort)) { + try { + overridePort = Integer.parseInt(sysPort); + } catch (NumberFormatException e) { + LOGGER.warn("Invalid system property for Nacos registry port: {}", sysPort); + } + } + if (overridePort <= 0) { + overridePort = getFileConfig().getInt(portKey, 0); + } + + if (StringUtils.isBlank(overrideIp) && overridePort <= 0) { + return address; + } + + String defaultIp = address.getAddress().getHostAddress(); + int defaultPort = address.getPort(); + + String finalIp = StringUtils.isNotBlank(overrideIp) ? overrideIp : defaultIp; + int finalPort = overridePort > 0 ? overridePort : defaultPort; + + InetSocketAddress registryAddress = new InetSocketAddress(finalIp, finalPort); + NetUtil.validAddress(registryAddress); + + return registryAddress; + } + @Override public void subscribe(String cluster, EventListener listener) throws Exception { List clusters = new ArrayList<>(); @@ -471,4 +513,12 @@ private static String getNacosContextPathKey() { REGISTRY_TYPE, CONTEXT_PATH); } + + private static String getNacosIpFileKey() { + return "registry" + CONFIG_SPLIT_CHAR + REGISTRY_TYPE + CONFIG_SPLIT_CHAR + PRO_IP_KEY; + } + + private static String getNacosPortFileKey() { + return "registry" + CONFIG_SPLIT_CHAR + REGISTRY_TYPE + CONFIG_SPLIT_CHAR + PRO_PORT_KEY; + } } diff --git a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java index fcef4e3c9d7..a397904f2a7 100644 --- a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java +++ b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java @@ -53,6 +53,7 @@ public class NacosRegistryServiceImplTest { private MockedStatic mockedConfigurationFactory; private Configuration mockedRegisterServiceConfiguration; + private Configuration mockedFileConfiguration; private NacosRegistryServiceImpl nacosRegistryService; @@ -63,6 +64,8 @@ public class NacosRegistryServiceImplTest { private static final String APPLICATION_KEY = "registry.nacos.application"; private static final String GROUP_KEY = "registry.nacos.group"; private static final String CLUSTER_KEY = "registry.nacos.cluster"; + private static final String IP_KEY = "registry.nacos.ip"; + private static final String PORT_KEY = "registry.nacos.port"; private static final String NACOS_MOCKED_APPLICATION = "MOCKED_APP"; private static final String NACOS_MOCKED_GROUP = "MOCKED_GROUP"; @@ -73,36 +76,33 @@ public class NacosRegistryServiceImplTest { public void beforeEach() throws Exception { mockedNamingService = mock(NamingService.class); mockedNamingMaintainService = mock(NamingMaintainService.class); + mockedFileConfiguration = mock(Configuration.class); mockedConfigurationFactory = mockStatic(ConfigurationFactory.class); mockedRegisterServiceConfiguration = mock(Configuration.class); mockedConfigurationFactory .when(ConfigurationFactory::getInstance) .thenReturn(mockedRegisterServiceConfiguration); - - Configuration mockedCurrentNacosConfiguration = mock(Configuration.class); ReflectionUtil.modifyStaticFinalField( - ConfigurationFactory.class, "CURRENT_FILE_INSTANCE", mockedCurrentNacosConfiguration); + ConfigurationFactory.class, "CURRENT_FILE_INSTANCE", mockedFileConfiguration); // default config expectations - when(mockedCurrentNacosConfiguration.getConfig(SLB_PATTERN_KEY)).thenReturn(""); - when(mockedCurrentNacosConfiguration.getConfig(SERVER_ADDR_KEY)).thenReturn("127.0.0.1"); + when(mockedFileConfiguration.getConfig(SLB_PATTERN_KEY)).thenReturn(""); + when(mockedFileConfiguration.getConfig(SERVER_ADDR_KEY)).thenReturn("127.0.0.1"); // Mock for getServiceName() -> registry.nacos.application - when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY)).thenReturn(NACOS_MOCKED_APPLICATION); - when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY, "seata-server")) - .thenReturn(NACOS_MOCKED_APPLICATION); + when(mockedFileConfiguration.getConfig(APPLICATION_KEY)).thenReturn(NACOS_MOCKED_APPLICATION); + when(mockedFileConfiguration.getConfig(APPLICATION_KEY, "seata-server")).thenReturn(NACOS_MOCKED_APPLICATION); // Mock for getServiceGroup() -> registry.nacos.group - when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY)).thenReturn(NACOS_MOCKED_GROUP); - when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY, "DEFAULT_GROUP")) - .thenReturn(NACOS_MOCKED_GROUP); + when(mockedFileConfiguration.getConfig(GROUP_KEY)).thenReturn(NACOS_MOCKED_GROUP); + when(mockedFileConfiguration.getConfig(GROUP_KEY, "DEFAULT_GROUP")).thenReturn(NACOS_MOCKED_GROUP); // Mock for getClusterName() -> registry.nacos.cluster - when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY)).thenReturn(NACOS_MOCKED_CLUSTER); - when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY, "default")).thenReturn(NACOS_MOCKED_CLUSTER); + when(mockedFileConfiguration.getConfig(CLUSTER_KEY)).thenReturn(NACOS_MOCKED_CLUSTER); + when(mockedFileConfiguration.getConfig(CLUSTER_KEY, "default")).thenReturn(NACOS_MOCKED_CLUSTER); - when(mockedCurrentNacosConfiguration.getConfig(CONTEXT_PATH_KEY)).thenReturn("/foo"); + when(mockedFileConfiguration.getConfig(CONTEXT_PATH_KEY)).thenReturn("/foo"); nacosRegistryService = NacosRegistryServiceImpl.getInstance(); @@ -114,7 +114,7 @@ public void beforeEach() throws Exception { @AfterEach public void afterEach() { // Clear any system properties set by tests - Arrays.asList("username", "password", "accessKey", "secretKey", "ramRoleName", "contextPath") + Arrays.asList("username", "password", "accessKey", "secretKey", "ramRoleName", "contextPath", IP_KEY, PORT_KEY) .forEach(System::clearProperty); // reset static fields to avoid test interdependence @@ -426,4 +426,75 @@ private EventListener captureSubscribedListener(String expectedCluster) throws N eventListenerCaptor.capture()); return eventListenerCaptor.getValue(); } + + @Test + public void shouldRegisterWithCustomIpAndPort() throws Exception { + InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8091); + + when(mockedFileConfiguration.getConfig(eq(IP_KEY))).thenReturn("192.168.1.100"); + when(mockedFileConfiguration.getInt(eq(PORT_KEY), eq(0))).thenReturn(21908); + + nacosRegistryService.register(inetSocketAddress); + + verify(mockedNamingService) + .registerInstance( + NACOS_MOCKED_APPLICATION, NACOS_MOCKED_GROUP, "192.168.1.100", 21908, NACOS_MOCKED_CLUSTER); + } + + @Test + public void shouldUnregisterWithCustomIpAndPort() throws Exception { + InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8091); + + when(mockedFileConfiguration.getConfig(eq(IP_KEY))).thenReturn("192.168.1.100"); + when(mockedFileConfiguration.getInt(eq(PORT_KEY), eq(0))).thenReturn(21908); + + nacosRegistryService.unregister(inetSocketAddress); + + verify(mockedNamingService) + .deregisterInstance( + NACOS_MOCKED_APPLICATION, NACOS_MOCKED_GROUP, "192.168.1.100", 21908, NACOS_MOCKED_CLUSTER); + } + + @Test + public void shouldFallbackToOriginalAddressWhenNoOverrideConfigured() throws Exception { + InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8091); + + nacosRegistryService.register(address); + + verify(mockedNamingService) + .registerInstance( + NACOS_MOCKED_APPLICATION, NACOS_MOCKED_GROUP, "127.0.0.1", 8091, NACOS_MOCKED_CLUSTER); + } + + @Test + public void shouldRegisterWithSystemPropertyOverride() throws Exception { + InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8091); + + System.setProperty(IP_KEY, "10.0.0.5"); + System.setProperty(PORT_KEY, "33333"); + + when(mockedFileConfiguration.getConfig(eq(IP_KEY))).thenReturn(null); + when(mockedFileConfiguration.getInt(eq(PORT_KEY), eq(0))).thenReturn(0); + + nacosRegistryService.register(inetSocketAddress); + + verify(mockedNamingService).registerInstance(anyString(), anyString(), eq("10.0.0.5"), eq(33333), anyString()); + } + + @Test + public void shouldPreferSystemPropertyOverFileConfig() throws Exception { + InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8091); + + System.setProperty(IP_KEY, "10.0.0.5"); + System.setProperty(PORT_KEY, "33333"); + + when(mockedFileConfiguration.getConfig(eq(IP_KEY))).thenReturn("192.168.1.100"); + when(mockedFileConfiguration.getInt(eq(PORT_KEY), eq(0))).thenReturn(21908); + + nacosRegistryService.register(address); + + verify(mockedNamingService) + .registerInstance( + NACOS_MOCKED_APPLICATION, NACOS_MOCKED_GROUP, "10.0.0.5", 33333, NACOS_MOCKED_CLUSTER); + } } From 3927e70d06464e81214644bdc3b523a53b52303f Mon Sep 17 00:00:00 2001 From: somiljain2006 Date: Mon, 23 Mar 2026 11:37:37 +0530 Subject: [PATCH 2/2] Registered pr changes --- changes/en-us/2.x.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index e3fd0e15314..262c064735c 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -26,6 +26,7 @@ Add changes here for all PR submitted to the 2.x branch. - [[#7865](https://github.com/apache/incubator-seata/pull/7865)] add Benchmark CLI tool - [[#7903](https://github.com/apache/incubator-seata/pull/7903)] Support HTTP/2 stream push for the Watch API in Server Raft mode - [[#8002](https://github.com/apache/incubator-seata/pull/8002)] add Grafana dashboard JSON for NamingServer metrics +- [[#8026](https://github.com/apache/incubator-seata/pull/8026)] Support custom registry ip and port for Nacos registration ### bugfix: