feature: add config for customized local port (#956)

pull/958/head
kennylong 2 years ago committed by GitHub
parent fcb24dff13
commit 90a7beda26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,3 +5,4 @@
- [feat:support webclient and gateway report call metrics](https://github.com/Tencent/spring-cloud-tencent/pull/942) - [feat:support webclient and gateway report call metrics](https://github.com/Tencent/spring-cloud-tencent/pull/942)
- [docs:prevent the release of the final version of the sdk.](https://github.com/Tencent/spring-cloud-tencent/pull/945) - [docs:prevent the release of the final version of the sdk.](https://github.com/Tencent/spring-cloud-tencent/pull/945)
- [fix: fix nacos CircuitBreaker disable bug.](https://github.com/Tencent/spring-cloud-tencent/pull/947) - [fix: fix nacos CircuitBreaker disable bug.](https://github.com/Tencent/spring-cloud-tencent/pull/947)
- [feature: add config for customized local port.](https://github.com/Tencent/spring-cloud-tencent/pull/956)

@ -25,6 +25,7 @@ import java.util.Objects;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; 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.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
@ -76,6 +77,7 @@ public class PolarisRegistration implements Registration {
public PolarisRegistration( public PolarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable PolarisContextProperties polarisContextProperties,
@Nullable ConsulContextProperties consulContextProperties, @Nullable ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager, SDKContext context, StaticMetadataManager staticMetadataManager,
@Nullable NacosContextProperties nacosContextProperties, @Nullable NacosContextProperties nacosContextProperties,
@ -89,6 +91,9 @@ public class PolarisRegistration implements Registration {
this.servletWebServerApplicationContext = servletWebServerApplicationContext; this.servletWebServerApplicationContext = servletWebServerApplicationContext;
this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext; this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext;
host = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); host = polarisContext.getConfig().getGlobal().getAPI().getBindIP();
if (polarisContextProperties != null) {
port = polarisContextProperties.getLocalPort();
}
} }
@Override @Override

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; 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.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
@ -65,11 +66,12 @@ public class PolarisServiceRegistryAutoConfiguration {
@ConditionalOnBean(AutoServiceRegistrationProperties.class) @ConditionalOnBean(AutoServiceRegistrationProperties.class)
public PolarisRegistration polarisRegistration( public PolarisRegistration polarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
PolarisContextProperties polarisContextProperties,
@Autowired(required = false) ConsulContextProperties consulContextProperties, @Autowired(required = false) ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties, SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties,
@Autowired(required = false) ServletWebServerApplicationContext servletWebServerApplicationContext, @Autowired(required = false) ServletWebServerApplicationContext servletWebServerApplicationContext,
@Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) { @Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) {
return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context, return new PolarisRegistration(polarisDiscoveryProperties, polarisContextProperties, consulContextProperties, context,
staticMetadataManager, nacosContextProperties, staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, reactiveWebServerApplicationContext); servletWebServerApplicationContext, reactiveWebServerApplicationContext);
} }

@ -22,6 +22,7 @@ import java.util.Map;
import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.StaticMetadataManager;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties; 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.consul.ConsulContextProperties;
import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties; import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.polaris.api.config.Configuration; import com.tencent.polaris.api.config.Configuration;
@ -62,6 +63,10 @@ public class PolarisRegistrationTest {
private PolarisRegistration polarisRegistration3; private PolarisRegistration polarisRegistration3;
private PolarisRegistration polarisRegistration4;
private static int testLocalPort = 10086;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
// mock PolarisDiscoveryProperties // mock PolarisDiscoveryProperties
@ -70,23 +75,27 @@ public class PolarisRegistrationTest {
doReturn("http").when(polarisDiscoveryProperties).getProtocol(); doReturn("http").when(polarisDiscoveryProperties).getProtocol();
doReturn(true).when(polarisDiscoveryProperties).isRegisterEnabled(); doReturn(true).when(polarisDiscoveryProperties).isRegisterEnabled();
// mock PolarisContextProperties
PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class);
doReturn(testLocalPort).when(polarisContextProperties).getLocalPort();
// mock ConsulContextProperties // mock ConsulContextProperties
ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class); ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class);
doReturn(true).when(consulContextProperties).isEnabled(); doReturn(true).when(consulContextProperties).isEnabled();
doReturn(true).when(consulContextProperties).isRegister(); doReturn(true).when(consulContextProperties).isRegister();
// mock NacosContextProperties // mock NacosContextProperties
nacosContextProperties = new NacosContextProperties(); // mock NacosContextProperties
nacosContextProperties.setEnabled(true); nacosContextProperties = mock(NacosContextProperties.class);
nacosContextProperties.setRegisterEnabled(true); doReturn(true).when(nacosContextProperties).isEnabled();
nacosContextProperties.setContextPath("/"); doReturn(true).when(nacosContextProperties).isRegisterEnabled();
nacosContextProperties.setClusterName("cluster"); doReturn("/").when(nacosContextProperties).getContextPath();
nacosContextProperties.setGroup(""); doReturn("cluster").when(nacosContextProperties).getClusterName();
nacosContextProperties.setDiscoveryEnabled(true); doReturn("").when(nacosContextProperties).getGroup();
nacosContextProperties.setPassword(""); doReturn(true).when(nacosContextProperties).isDiscoveryEnabled();
nacosContextProperties.setUsername(""); doReturn("").when(nacosContextProperties).getPassword();
nacosContextProperties.setServerAddr(""); doReturn("").when(nacosContextProperties).getUsername();
nacosContextProperties.toString(); doReturn("").when(nacosContextProperties).getServerAddr();
// mock SDKContext // mock SDKContext
APIConfig apiConfig = mock(APIConfig.class); APIConfig apiConfig = mock(APIConfig.class);
@ -114,15 +123,19 @@ public class PolarisRegistrationTest {
ReactiveWebServerApplicationContext reactiveWebServerApplicationContext = mock(ReactiveWebServerApplicationContext.class); ReactiveWebServerApplicationContext reactiveWebServerApplicationContext = mock(ReactiveWebServerApplicationContext.class);
doReturn(reactiveWebServer).when(reactiveWebServerApplicationContext).getWebServer(); doReturn(reactiveWebServer).when(reactiveWebServerApplicationContext).getWebServer();
polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties, polarisContext, staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, null); servletWebServerApplicationContext, null);
polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, null, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties, polarisContext, staticMetadataManager, nacosContextProperties,
null, reactiveWebServerApplicationContext); 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, polarisContext, staticMetadataManager, nacosContextProperties,
null, null); null, null);
} }
@ -147,6 +160,7 @@ public class PolarisRegistrationTest {
catch (RuntimeException e) { catch (RuntimeException e) {
assertThat(e.getMessage()).isEqualTo("Unsupported web type."); assertThat(e.getMessage()).isEqualTo("Unsupported web type.");
} }
assertThat(polarisRegistration4.getPort()).isEqualTo(testLocalPort);
} }
@Test @Test

@ -52,7 +52,8 @@ public class PolarisContextAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List<PolarisConfigModifier> modifierList) throws PolarisException { public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List<PolarisConfigModifier> modifierList) throws PolarisException {
return SDKContext.initContextByConfig(properties.configuration(modifierList, return SDKContext.initContextByConfig(properties.configuration(modifierList,
() -> environment.getProperty("spring.cloud.client.ip-address"))); () -> environment.getProperty("spring.cloud.client.ip-address"),
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
} }
@Bean @Bean

@ -52,6 +52,11 @@ public class PolarisContextProperties {
*/ */
private String localIpAddress; private String localIpAddress;
/**
* current server local port.
*/
private Integer localPort;
/** /**
* If polaris enabled. * If polaris enabled.
*/ */
@ -67,7 +72,7 @@ public class PolarisContextProperties {
*/ */
private String service; private String service;
public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier) { public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier, Supplier<Integer> portSupplier) {
// 1. Read user-defined polaris.yml configuration // 1. Read user-defined polaris.yml configuration
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(ConfigProvider.DEFAULT_CONFIG); .defaultConfig(ConfigProvider.DEFAULT_CONFIG);
@ -78,6 +83,9 @@ public class PolarisContextProperties {
defaultHost = ipAddressSupplier.get(); defaultHost = ipAddressSupplier.get();
this.localIpAddress = defaultHost; this.localIpAddress = defaultHost;
} }
if (this.localPort == null || this.localPort <= 0) {
this.localPort = portSupplier.get();
}
configuration.getGlobal().getAPI().setBindIP(defaultHost); configuration.getGlobal().getAPI().setBindIP(defaultHost);
@ -110,6 +118,14 @@ public class PolarisContextProperties {
this.localIpAddress = localIpAddress; this.localIpAddress = localIpAddress;
} }
public Integer getLocalPort() {
return localPort;
}
public void setLocalPort(Integer localPort) {
this.localPort = localPort;
}
public Boolean getEnabled() { public Boolean getEnabled() {
return enabled; return enabled;
} }

Loading…
Cancel
Save