From 4b419a1bc038506c0a1d1d9fbea5ce6805b8b90d Mon Sep 17 00:00:00 2001 From: lanbinshen Date: Tue, 10 Mar 2026 20:57:47 +0800 Subject: [PATCH 1/3] Add registry port config --- .../seata/common/ConfigurationKeys.java | 15 ++++++++ .../core/rpc/netty/NettyServerBootstrap.java | 16 ++++++++- .../properties/server/ServerProperties.java | 10 ++++++ .../server/ServerPropertiesTest.java | 2 ++ .../seata/server/env/ContainerHelper.java | 11 ++++++ .../listener/ServerApplicationListener.java | 34 +++++++++++++++++++ .../store/VGroupMappingStoreManager.java | 15 +++++++- .../main/resources/application.example.yml | 1 + server/src/main/resources/application.yml | 1 + 9 files changed, 103 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/apache/seata/common/ConfigurationKeys.java b/common/src/main/java/org/apache/seata/common/ConfigurationKeys.java index b99c856a0fc..377481772a1 100644 --- a/common/src/main/java/org/apache/seata/common/ConfigurationKeys.java +++ b/common/src/main/java/org/apache/seata/common/ConfigurationKeys.java @@ -455,6 +455,21 @@ public interface ConfigurationKeys { */ String ENV_SEATA_PORT_KEY = "SEATA_PORT"; + /** + * The constant SERVER_REGISTRY_PORT_CAMEL. + */ + String SERVER_REGISTRY_PORT_CAMEL = SERVER_PREFIX + "registryPort"; + + /** + * The constant SERVER_REGISTRY_PORT_CONFIG. + */ + String SERVER_REGISTRY_PORT_CONFIG = SEATA_PREFIX + SERVER_PREFIX + "registry-port"; + + /** + * The constant ENV_SEATA_REGISTRY_PORT_KEY. + */ + String ENV_SEATA_REGISTRY_PORT_KEY = "SEATA_REGISTRY_PORT"; + /** * The constant RECOVERY_PREFIX. */ diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java index c1acba597d9..0ffaa3ff1c0 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java @@ -197,7 +197,9 @@ public void initChannel(SocketChannel ch) { Instance instance = Instance.getInstance(); // Lines 177-180 are just for compatibility with test cases if (instance.getTransaction() == null) { - Instance.getInstance().setTransaction(new Node.Endpoint(XID.getIpAddress(), XID.getPort(), "netty")); + int regPort = getRegistryPort(); + Instance.getInstance().setTransaction(new Node.Endpoint(XID.getIpAddress(), + regPort > 0 ? regPort : XID.getPort(), "netty")); } for (RegistryService registryService : MultiRegistryFactory.getInstances()) { registryService.register(Instance.getInstance()); @@ -210,6 +212,18 @@ public void initChannel(SocketChannel ch) { } } + private int getRegistryPort() { + int port = 0; + String strPort = ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.SERVER_REGISTRY_PORT_CAMEL); + if (strPort != null) { + try { + port = Integer.parseInt(strPort); + } catch (NumberFormatException ignored) { + } + } + return port; + } + @Override public void shutdown() { try { diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/main/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerProperties.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/main/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerProperties.java index 58ca093bbde..c321ff2591c 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/main/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerProperties.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/main/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerProperties.java @@ -33,6 +33,7 @@ public class ServerProperties { private Boolean enableParallelHandleBranch = false; private Integer retryDeadThreshold = 70000; private Integer servicePort; + private Integer registryPort; private Integer xaerNotaRetryTimeout = 60000; private Boolean applicationDataLimitCheck = false; @@ -101,6 +102,15 @@ public ServerProperties setServicePort(Integer servicePort) { return this; } + public Integer getRegistryPort() { + return registryPort; + } + + public ServerProperties setRegistryPort(Integer registryPort) { + this.registryPort = registryPort; + return this; + } + public Integer getXaerNotaRetryTimeout() { return xaerNotaRetryTimeout; } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/test/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/test/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerPropertiesTest.java index 60d8db4a121..b5df04e5c11 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/test/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-server/src/test/java/org/apache/seata/spring/boot/autoconfigure/properties/server/ServerPropertiesTest.java @@ -28,6 +28,7 @@ public void testMetricsProperties() { serverProperties.setRetryDeadThreshold(1); serverProperties.setApplicationDataLimit(1); serverProperties.setServicePort(1); + serverProperties.setRegistryPort(1); serverProperties.setEnableCheckAuth(true); serverProperties.setApplicationDataLimitCheck(true); serverProperties.setEnableParallelHandleBranch(true); @@ -41,6 +42,7 @@ public void testMetricsProperties() { Assertions.assertEquals(1, serverProperties.getRetryDeadThreshold()); Assertions.assertEquals(1, serverProperties.getApplicationDataLimit()); Assertions.assertEquals(1, serverProperties.getServicePort()); + Assertions.assertEquals(1, serverProperties.getRegistryPort()); Assertions.assertTrue(serverProperties.getEnableCheckAuth()); Assertions.assertTrue(serverProperties.getApplicationDataLimitCheck()); Assertions.assertTrue(serverProperties.getEnableParallelHandleBranch()); diff --git a/server/src/main/java/org/apache/seata/server/env/ContainerHelper.java b/server/src/main/java/org/apache/seata/server/env/ContainerHelper.java index 40d5a7033af..3bbca8f1fcc 100644 --- a/server/src/main/java/org/apache/seata/server/env/ContainerHelper.java +++ b/server/src/main/java/org/apache/seata/server/env/ContainerHelper.java @@ -20,6 +20,7 @@ import org.apache.seata.common.util.StringUtils; import static org.apache.seata.core.constants.ConfigurationKeys.ENV_SEATA_PORT_KEY; +import static org.apache.seata.core.constants.ConfigurationKeys.ENV_SEATA_REGISTRY_PORT_KEY; /** */ @@ -63,6 +64,16 @@ public static int getPort() { return NumberUtils.toInt(System.getenv(ENV_SEATA_PORT_KEY), 0); } + /** + * Gets registry port from container. + * This port is used for service registration only, separated from the listen port. + * + * @return the registry port, 0 if not set + */ + public static int getRegistryPort() { + return NumberUtils.toInt(System.getenv(ENV_SEATA_REGISTRY_PORT_KEY), 0); + } + /** * Gets server node from container. * diff --git a/server/src/main/java/org/apache/seata/server/spring/listener/ServerApplicationListener.java b/server/src/main/java/org/apache/seata/server/spring/listener/ServerApplicationListener.java index 21c0dcd822d..3f9b4c4538d 100644 --- a/server/src/main/java/org/apache/seata/server/spring/listener/ServerApplicationListener.java +++ b/server/src/main/java/org/apache/seata/server/spring/listener/ServerApplicationListener.java @@ -33,6 +33,9 @@ import static org.apache.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT; import static org.apache.seata.core.constants.ConfigurationKeys.ENV_SEATA_PORT_KEY; +import static org.apache.seata.core.constants.ConfigurationKeys.ENV_SEATA_REGISTRY_PORT_KEY; +import static org.apache.seata.core.constants.ConfigurationKeys.SERVER_REGISTRY_PORT_CAMEL; +import static org.apache.seata.core.constants.ConfigurationKeys.SERVER_REGISTRY_PORT_CONFIG; import static org.apache.seata.core.constants.ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL; import static org.apache.seata.core.constants.ConfigurationKeys.SERVER_SERVICE_PORT_CONFIG; @@ -65,6 +68,11 @@ public void onApplicationEvent(ApplicationEvent event) { String[] args = environmentPreparedEvent.getArgs(); + resolveServicePort(environment, args); + resolveRegistryPort(environment); + } + + private void resolveServicePort(ConfigurableEnvironment environment, String[] args) { // port: -p > -D > env > yml > default // -p 8091 @@ -107,6 +115,24 @@ public void onApplicationEvent(ApplicationEvent event) { setTargetPort(environment, servicePort, true); } + private void resolveRegistryPort(ConfigurableEnvironment environment) { + // registryPort: env > yml + // If not configured, the registration port equals the service port. + + // docker -e SEATA_REGISTRY_PORT=21908 + String envRegistryPort = environment.getProperty(ENV_SEATA_REGISTRY_PORT_KEY, String.class); + if (StringUtils.isNotBlank(envRegistryPort)) { + setTargetRegistryPort(environment, envRegistryPort); + return; + } + + // yml properties seata.server.registry-port=21908 + String configRegistryPort = environment.getProperty(SERVER_REGISTRY_PORT_CONFIG, String.class); + if (StringUtils.isNotBlank(configRegistryPort)) { + setTargetRegistryPort(environment, configRegistryPort); + } + } + private void setTargetPort(ConfigurableEnvironment environment, String port, boolean needAddPropertySource) { // get rpc port first, use to logback-spring.xml, @see the class named `SystemPropertyLoggerContextListener` System.setProperty(SERVER_SERVICE_PORT_CAMEL, port); @@ -119,6 +145,14 @@ private void setTargetPort(ConfigurableEnvironment environment, String port, boo } } + private void setTargetRegistryPort(ConfigurableEnvironment environment, String registryPort) { + System.setProperty(SERVER_REGISTRY_PORT_CAMEL, registryPort); + Properties pro = new Properties(); + pro.setProperty(SERVER_REGISTRY_PORT_CONFIG, registryPort); + pro.setProperty(SERVER_REGISTRY_PORT_CAMEL, registryPort); + environment.getPropertySources().addFirst(new PropertiesPropertySource("serverRegistryPortProperties", pro)); + } + /** * higher than LoggingApplicationListener * diff --git a/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java b/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java index f58a7d11fcd..e3a0741c30c 100644 --- a/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java +++ b/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java @@ -16,8 +16,11 @@ */ package org.apache.seata.server.store; +import org.apache.seata.common.ConfigurationKeys; import org.apache.seata.common.XID; import org.apache.seata.common.metadata.Instance; +import org.apache.seata.common.util.NumberUtils; +import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.core.store.MappingDO; import org.apache.seata.discovery.registry.MultiRegistryFactory; import org.apache.seata.discovery.registry.RegistryService; @@ -59,7 +62,17 @@ default void notifyMapping() { Map map = this.readVGroups(); instance.addMetadata("vGroup", map); try { - InetSocketAddress address = new InetSocketAddress(XID.getIpAddress(), XID.getPort()); + int registryPort = 0; + String strPort = ConfigurationFactory.getInstance() + .getConfig(ConfigurationKeys.SERVER_REGISTRY_PORT_CAMEL); + if (strPort != null) { + try { + registryPort = Integer.parseInt(strPort); + } catch (NumberFormatException ignored) { + } + } + int port = registryPort > 0 ? registryPort : XID.getPort(); + InetSocketAddress address = new InetSocketAddress(XID.getIpAddress(), port); for (RegistryService registryService : MultiRegistryFactory.getInstances()) { registryService.register(address); } diff --git a/server/src/main/resources/application.example.yml b/server/src/main/resources/application.example.yml index 15851222915..6750a82438f 100644 --- a/server/src/main/resources/application.example.yml +++ b/server/src/main/resources/application.example.yml @@ -146,6 +146,7 @@ seata: server: service-port: 8091 # If not configured, the default is '${server.port}' + registry-port: 21908 # If not configured, the registration port equals the service port max-commit-retry-timeout: -1 max-rollback-retry-timeout: -1 rollback-failed-unlock-enable: false diff --git a/server/src/main/resources/application.yml b/server/src/main/resources/application.yml index e7d7c3c2bc4..e1d56c30fcf 100644 --- a/server/src/main/resources/application.yml +++ b/server/src/main/resources/application.yml @@ -55,3 +55,4 @@ seata: mode: file # server: # service-port: 8091 #If not configured, the default is '${server.port} + 1000' + # registry-port: 21908 #If not configured, the registration port equals the service port From df5f2629d479b00c214f044f8e8327b341425834 Mon Sep 17 00:00:00 2001 From: lanbinshen Date: Tue, 10 Mar 2026 21:13:46 +0800 Subject: [PATCH 2/3] Add changes --- changes/en-us/2.x.md | 1 + changes/zh-cn/2.x.md | 1 + 2 files changed, 2 insertions(+) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index 6e23c028b91..a26a10008d1 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -25,6 +25,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 +- [[#8017](https://github.com/apache/incubator-seata/pull/8017)] support separate registry port from listen port for Docker port mapping ### bugfix: diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index 9af251bc571..b1b79130c11 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -26,6 +26,7 @@ - [[#7865](https://github.com/apache/incubator-seata/pull/7865)] 新增 Benchmark 命令行工具 - [[#7903](https://github.com/apache/incubator-seata/pull/7903)] 在Server Raft模式下支持Watch API的HTTP/2流推送 - [[#8002](https://github.com/apache/incubator-seata/pull/8002)] 为namingserver指标增加Grafana dashboard JSON +- [[#8017](https://github.com/apache/incubator-seata/pull/8017)] 支持注册端口与监听端口分离,适配Docker端口映射场景 ### bugfix: From 84f349820a66065c9d1f041933212ba515b47ce3 Mon Sep 17 00:00:00 2001 From: lanbinshen Date: Sat, 14 Mar 2026 18:06:25 +0800 Subject: [PATCH 3/3] Run spotless --- .../apache/seata/core/rpc/netty/NettyServerBootstrap.java | 5 +++-- .../apache/seata/server/store/VGroupMappingStoreManager.java | 4 +--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java index 0ffaa3ff1c0..6e39abeaf06 100644 --- a/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java +++ b/core/src/main/java/org/apache/seata/core/rpc/netty/NettyServerBootstrap.java @@ -198,8 +198,9 @@ public void initChannel(SocketChannel ch) { // Lines 177-180 are just for compatibility with test cases if (instance.getTransaction() == null) { int regPort = getRegistryPort(); - Instance.getInstance().setTransaction(new Node.Endpoint(XID.getIpAddress(), - regPort > 0 ? regPort : XID.getPort(), "netty")); + Instance.getInstance() + .setTransaction( + new Node.Endpoint(XID.getIpAddress(), regPort > 0 ? regPort : XID.getPort(), "netty")); } for (RegistryService registryService : MultiRegistryFactory.getInstances()) { registryService.register(Instance.getInstance()); diff --git a/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java b/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java index e3a0741c30c..e9aad93295c 100644 --- a/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java +++ b/server/src/main/java/org/apache/seata/server/store/VGroupMappingStoreManager.java @@ -19,7 +19,6 @@ import org.apache.seata.common.ConfigurationKeys; import org.apache.seata.common.XID; import org.apache.seata.common.metadata.Instance; -import org.apache.seata.common.util.NumberUtils; import org.apache.seata.config.ConfigurationFactory; import org.apache.seata.core.store.MappingDO; import org.apache.seata.discovery.registry.MultiRegistryFactory; @@ -63,8 +62,7 @@ default void notifyMapping() { instance.addMetadata("vGroup", map); try { int registryPort = 0; - String strPort = ConfigurationFactory.getInstance() - .getConfig(ConfigurationKeys.SERVER_REGISTRY_PORT_CAMEL); + String strPort = ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.SERVER_REGISTRY_PORT_CAMEL); if (strPort != null) { try { registryPort = Integer.parseInt(strPort);