From 9dff4a130e7c41a2516c55e0ad9fb789b18a4aec Mon Sep 17 00:00:00 2001 From: kennylong Date: Tue, 4 Apr 2023 16:15:36 +0800 Subject: [PATCH] feature: add config for customized local port (#923) Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../PolarisConfigDataLocationResolver.java | 2 + .../polaris/registry/PolarisRegistration.java | 5 +++ ...larisServiceRegistryAutoConfiguration.java | 4 +- .../registry/PolarisRegistrationTest.java | 41 ++++++++++++------- .../PolarisContextAutoConfiguration.java | 2 + .../config/PolarisContextProperties.java | 18 +++++++- 7 files changed, 57 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30e12b89c..be5304155 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change Log --- +- [feature: add config for customized local port.](https://github.com/Tencent/spring-cloud-tencent/pull/923) - [feat:support webclient and gateway report call metrics](https://github.com/Tencent/spring-cloud-tencent/pull/924) - [feature: optimize polaris-discovery-example/discovery-callee-service, add client-ip return.](https://github.com/Tencent/spring-cloud-tencent/pull/939) - [docs:prevent the release of the final version of the sdk.](https://github.com/Tencent/spring-cloud-tencent/pull/943) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java index 146dbea34..82026b1da 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java @@ -264,6 +264,8 @@ public class PolarisConfigDataLocationResolver implements List modifierList = modifierList(polarisConfigProperties, polarisContextProperties); return SDKContext.initContextByConfig(polarisContextProperties.configuration(modifierList, () -> { return loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); + }, () -> { + return loadPolarisConfigProperties(resolverContext, Integer.class, "spring.cloud.polaris.local-port"); })); } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 7a5fb4609..1c9d902f9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -25,6 +25,7 @@ import java.util.Objects; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.client.api.SDKContext; @@ -76,6 +77,7 @@ public class PolarisRegistration implements Registration { public PolarisRegistration( PolarisDiscoveryProperties polarisDiscoveryProperties, + @Nullable PolarisContextProperties polarisContextProperties, @Nullable ConsulContextProperties consulContextProperties, SDKContext context, StaticMetadataManager staticMetadataManager, @Nullable NacosContextProperties nacosContextProperties, @@ -89,6 +91,9 @@ public class PolarisRegistration implements Registration { this.servletWebServerApplicationContext = servletWebServerApplicationContext; this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext; host = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); + if (polarisContextProperties != null) { + port = polarisContextProperties.getLocalPort(); + } } @Override diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index a421ec307..be5d244f9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.registry; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; @@ -65,11 +66,12 @@ public class PolarisServiceRegistryAutoConfiguration { @ConditionalOnBean(AutoServiceRegistrationProperties.class) public PolarisRegistration polarisRegistration( PolarisDiscoveryProperties polarisDiscoveryProperties, + PolarisContextProperties polarisContextProperties, @Autowired(required = false) ConsulContextProperties consulContextProperties, SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties, @Autowired(required = false) ServletWebServerApplicationContext servletWebServerApplicationContext, @Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) { - return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, + return new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties, context, staticMetadataManager, nacosContextProperties, servletWebServerApplicationContext, reactiveWebServerApplicationContext); } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java index ebcc5ea4b..b0b7daa96 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java @@ -22,6 +22,7 @@ import java.util.Map; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.polaris.api.config.Configuration; @@ -62,6 +63,10 @@ public class PolarisRegistrationTest { private PolarisRegistration polarisRegistration3; + private PolarisRegistration polarisRegistration4; + + private static int testLocalPort = 10086; + @BeforeEach void setUp() { // mock PolarisDiscoveryProperties @@ -70,23 +75,26 @@ public class PolarisRegistrationTest { doReturn("http").when(polarisDiscoveryProperties).getProtocol(); doReturn(true).when(polarisDiscoveryProperties).isRegisterEnabled(); + // mock PolarisContextProperties + PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class); + doReturn(testLocalPort).when(polarisContextProperties).getLocalPort(); + // mock ConsulContextProperties ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class); doReturn(true).when(consulContextProperties).isEnabled(); doReturn(true).when(consulContextProperties).isRegister(); // mock NacosContextProperties - nacosContextProperties = new NacosContextProperties(); - nacosContextProperties.setEnabled(true); - nacosContextProperties.setRegisterEnabled(true); - nacosContextProperties.setContextPath("/"); - nacosContextProperties.setClusterName("cluster"); - nacosContextProperties.setGroup(""); - nacosContextProperties.setDiscoveryEnabled(true); - nacosContextProperties.setPassword(""); - nacosContextProperties.setUsername(""); - nacosContextProperties.setServerAddr(""); - nacosContextProperties.toString(); + nacosContextProperties = mock(NacosContextProperties.class); + doReturn(true).when(nacosContextProperties).isEnabled(); + doReturn(true).when(nacosContextProperties).isRegisterEnabled(); + doReturn("/").when(nacosContextProperties).getContextPath(); + doReturn("cluster").when(nacosContextProperties).getClusterName(); + doReturn("").when(nacosContextProperties).getGroup(); + doReturn(true).when(nacosContextProperties).isDiscoveryEnabled(); + doReturn("").when(nacosContextProperties).getPassword(); + doReturn("").when(nacosContextProperties).getUsername(); + doReturn("").when(nacosContextProperties).getServerAddr(); // mock SDKContext APIConfig apiConfig = mock(APIConfig.class); @@ -114,15 +122,19 @@ public class PolarisRegistrationTest { ReactiveWebServerApplicationContext reactiveWebServerApplicationContext = mock(ReactiveWebServerApplicationContext.class); doReturn(reactiveWebServer).when(reactiveWebServerApplicationContext).getWebServer(); - polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, + polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties, polarisContext, staticMetadataManager, nacosContextProperties, servletWebServerApplicationContext, null); - polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, + polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties, polarisContext, staticMetadataManager, nacosContextProperties, null, reactiveWebServerApplicationContext); - polarisRegistration3 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, + polarisRegistration3 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties, + polarisContext, staticMetadataManager, nacosContextProperties, + null, null); + + polarisRegistration4 = new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties, polarisContext, staticMetadataManager, nacosContextProperties, null, null); } @@ -147,6 +159,7 @@ public class PolarisRegistrationTest { catch (RuntimeException e) { assertThat(e.getMessage()).isEqualTo("Unsupported web type."); } + assertThat(polarisRegistration4.getPort()).isEqualTo(testLocalPort); } @Test diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index f0f143cad..a0256ae12 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -51,6 +51,8 @@ public class PolarisContextAutoConfiguration { public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List modifierList) throws PolarisException { return SDKContext.initContextByConfig(properties.configuration(modifierList, () -> { return environment.getProperty("spring.cloud.client.ip-address"); + }, () -> { + return environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0); })); } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java index 696218a28..f77eaa4f5 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java @@ -52,6 +52,11 @@ public class PolarisContextProperties { */ private String localIpAddress; + /** + * current server local port. + */ + private Integer localPort; + /** * If polaris enabled. */ @@ -67,7 +72,7 @@ public class PolarisContextProperties { */ private String service; - public Configuration configuration(List modifierList, Supplier ipAddressSupplier) { + public Configuration configuration(List modifierList, Supplier ipAddressSupplier, Supplier portSupplier) { // 1. Read user-defined polaris.yml configuration ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory .defaultConfig(ConfigProvider.DEFAULT_CONFIG); @@ -78,6 +83,9 @@ public class PolarisContextProperties { defaultHost = ipAddressSupplier.get(); this.localIpAddress = defaultHost; } + if (this.localPort == null || this.localPort <= 0) { + this.localPort = portSupplier.get(); + } configuration.getGlobal().getAPI().setBindIP(defaultHost); @@ -110,6 +118,14 @@ public class PolarisContextProperties { this.localIpAddress = localIpAddress; } + public Integer getLocalPort() { + return localPort; + } + + public void setLocalPort(Integer localPort) { + this.localPort = localPort; + } + public Boolean getEnabled() { return enabled; }