feat:update getPort method of PolarisRegistration. (#804)

pull/819/head
Haotian Zhang 2 years ago committed by GitHub
parent a15e2ceaa4
commit c750c206d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,3 +23,4 @@
- [set reporter.enabled default to true](https://github.com/Tencent/spring-cloud-tencent/pull/786)
- [fix:fix nacos enabled condition bugs.](https://github.com/Tencent/spring-cloud-tencent/pull/794)
- [fix:fix instance's weight hard code to 100 bug.](https://github.com/Tencent/spring-cloud-tencent/pull/801)
- [feat:update getPort method of PolarisRegistration.](https://github.com/Tencent/spring-cloud-tencent/pull/804)

@ -67,12 +67,6 @@ public class PolarisDiscoveryProperties {
@Value("${spring.cloud.polaris.discovery.protocol:http}")
private String protocol;
/**
* Port of instance.
*/
@Value("${server.port:8080}")
private int port;
/**
* Enable polaris discovery or not.
*/
@ -130,18 +124,10 @@ public class PolarisDiscoveryProperties {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public boolean isRegisterEnabled() {
return registerEnabled;
}
public void setRegisterEnabled(boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
public String getToken() {
return token;
}
@ -166,14 +152,6 @@ public class PolarisDiscoveryProperties {
this.protocol = protocol;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHealthCheckUrl() {
return healthCheckUrl;
}
@ -205,10 +183,18 @@ public class PolarisDiscoveryProperties {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Boolean getRegisterEnabled() {
return registerEnabled;
}
public void setRegisterEnabled(boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
public void setRegisterEnabled(Boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
@ -222,7 +208,6 @@ public class PolarisDiscoveryProperties {
", weight=" + weight +
", version='" + version + '\'' +
", protocol='" + protocol + '\'' +
", port=" + port +
", enabled=" + enabled +
", registerEnabled=" + registerEnabled +
", heartbeatInterval=" + heartbeatInterval +

@ -44,9 +44,6 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
@Override
protected PolarisRegistration getRegistration() {
if (this.registration.getPort() <= 0) {
this.registration.setPort(this.getPort().get());
}
return this.registration;
}
@ -61,9 +58,6 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
log.debug("Registration disabled.");
return;
}
if (this.registration.getPort() <= 0) {
this.registration.setPort(getPort().get());
}
super.register();
}

@ -30,6 +30,8 @@ import com.tencent.cloud.polaris.extend.nacos.NacosContextProperties;
import com.tencent.polaris.client.api.SDKContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.lang.Nullable;
@ -37,6 +39,7 @@ import org.springframework.util.CollectionUtils;
import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFAULT_CLUSTER;
import static com.tencent.cloud.polaris.extend.nacos.NacosContextProperties.DEFAULT_GROUP;
/**
* Registration object of Polaris.
*
@ -53,29 +56,38 @@ public class PolarisRegistration implements Registration {
private final ConsulContextProperties consulContextProperties;
private final NacosContextProperties nacosContextProperties;
private final SDKContext polarisContext;
private final StaticMetadataManager staticMetadataManager;
private final NacosContextProperties nacosContextProperties;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext;
private Map<String, String> metadata;
private String host;
private int port;
private String instanceId;
public PolarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager,
@Nullable NacosContextProperties nacosContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager) {
@Nullable ServletWebServerApplicationContext servletWebServerApplicationContext,
@Nullable ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.consulContextProperties = consulContextProperties;
this.nacosContextProperties = nacosContextProperties;
this.polarisContext = context;
this.staticMetadataManager = staticMetadataManager;
this.nacosContextProperties = nacosContextProperties;
this.servletWebServerApplicationContext = servletWebServerApplicationContext;
this.reactiveWebServerApplicationContext = reactiveWebServerApplicationContext;
host = polarisContext.getConfig().getGlobal().getAPI().getBindIP();
}
@ -100,17 +112,24 @@ public class PolarisRegistration implements Registration {
return host;
}
public void setHost(String host) {
this.host = host;
}
@Override
public int getPort() {
return polarisDiscoveryProperties.getPort();
if (port <= 0) {
if (servletWebServerApplicationContext != null) {
port = servletWebServerApplicationContext.getWebServer().getPort();
}
else if (reactiveWebServerApplicationContext != null) {
port = reactiveWebServerApplicationContext.getWebServer().getPort();
}
else {
throw new RuntimeException("Unsupported web type.");
}
}
return port;
}
public void setPort(int port) {
this.polarisDiscoveryProperties.setPort(port);
protected void setPort(int port) {
this.port = port;
}
@Override
@ -130,7 +149,7 @@ public class PolarisRegistration implements Registration {
// put internal metadata
instanceMetadata.put(METADATA_KEY_IP, host);
instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + polarisDiscoveryProperties.getPort());
instanceMetadata.put(METADATA_KEY_ADDRESS, host + ":" + port);
// put internal-nacos-cluster if necessary
String clusterName = nacosContextProperties.getClusterName();

@ -174,7 +174,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
}
for (Instance instance : instances) {
if (instance.getHost().equalsIgnoreCase(registration.getHost())
&& instance.getPort() == polarisDiscoveryProperties.getPort()) {
&& instance.getPort() == registration.getPort()) {
return instance.isHealthy() ? "UP" : "DOWN";
}
}

@ -31,6 +31,8 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
@ -62,9 +64,12 @@ public class PolarisServiceRegistryAutoConfiguration {
public PolarisRegistration polarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties,
@Autowired(required = false) ConsulContextProperties consulContextProperties,
SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties) {
return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, nacosContextProperties, context,
staticMetadataManager);
SDKContext context, StaticMetadataManager staticMetadataManager, NacosContextProperties nacosContextProperties,
@Autowired(required = false) ServletWebServerApplicationContext servletWebServerApplicationContext,
@Autowired(required = false) ReactiveWebServerApplicationContext reactiveWebServerApplicationContext) {
return new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties, context,
staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, reactiveWebServerApplicationContext);
}
@Bean

@ -21,7 +21,6 @@ package com.tencent.cloud.polaris;
import org.junit.Test;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.PROVIDER_TOKEN;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
@ -77,10 +76,6 @@ public class PolarisDiscoveryPropertiesTest {
polarisDiscoveryProperties.setProtocol("HTTP");
assertThat(polarisDiscoveryProperties.getProtocol()).isEqualTo("HTTP");
// Port
polarisDiscoveryProperties.setPort(PORT);
assertThat(polarisDiscoveryProperties.getPort()).isEqualTo(PORT);
// HealthCheckUrl
polarisDiscoveryProperties.setHealthCheckUrl("/health");
assertThat(polarisDiscoveryProperties.getHealthCheckUrl()).isEqualTo("/health");
@ -96,7 +91,6 @@ public class PolarisDiscoveryPropertiesTest {
+ ", token='19485a7674294e3c88dba293373c1534'"
+ ", weight=10, version='1.0.0'"
+ ", protocol='HTTP'"
+ ", port=9091"
+ ", enabled=true"
+ ", registerEnabled=true"
+ ", heartbeatInterval=20"

@ -29,7 +29,6 @@ import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@ -90,15 +89,6 @@ public class PolarisAutoServiceRegistrationTest {
}
doReturn(true).when(registration).isRegisterEnabled();
doReturn(-1).when(registration).getPort();
try {
polarisAutoServiceRegistration.register();
}
catch (Exception e) {
fail();
}
doReturn(PORT).when(registration).getPort();
try {
polarisAutoServiceRegistration.register();
}

@ -33,6 +33,10 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import static com.tencent.polaris.test.common.Consts.HOST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
@ -49,21 +53,29 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class PolarisRegistrationTest {
private PolarisRegistration polarisRegistration;
private NacosContextProperties nacosContextProperties;
private PolarisRegistration polarisRegistration1;
private PolarisRegistration polarisRegistration2;
NacosContextProperties nacosContextProperties = mock(NacosContextProperties.class);
private PolarisRegistration polarisRegistration3;
@Before
public void setUp() {
// mock PolarisDiscoveryProperties
PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
doReturn(PORT).when(polarisDiscoveryProperties).getPort();
doReturn("http").when(polarisDiscoveryProperties).getProtocol();
doReturn(true).when(polarisDiscoveryProperties).isRegisterEnabled();
// mock
// mock ConsulContextProperties
ConsulContextProperties consulContextProperties = mock(ConsulContextProperties.class);
doReturn(true).when(consulContextProperties).isEnabled();
doReturn(true).when(consulContextProperties).isRegister();
// mock NacosContextProperties
nacosContextProperties = mock(NacosContextProperties.class);
doReturn(true).when(nacosContextProperties).isEnabled();
doReturn(true).when(nacosContextProperties).isRegisterEnabled();
// mock SDKContext
APIConfig apiConfig = mock(APIConfig.class);
@ -79,38 +91,72 @@ public class PolarisRegistrationTest {
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata();
polarisRegistration = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties,
nacosContextProperties, polarisContext, staticMetadataManager);
// mock ServletWebServerApplicationContext
WebServer servletwebServer = mock(WebServer.class);
doReturn(PORT).when(servletwebServer).getPort();
ServletWebServerApplicationContext servletWebServerApplicationContext = mock(ServletWebServerApplicationContext.class);
doReturn(servletwebServer).when(servletWebServerApplicationContext).getWebServer();
// mock ReactiveWebServerApplicationContext
WebServer reactiveWebServer = mock(WebServer.class);
doReturn(PORT + 1).when(reactiveWebServer).getPort();
ReactiveWebServerApplicationContext reactiveWebServerApplicationContext = mock(ReactiveWebServerApplicationContext.class);
doReturn(reactiveWebServer).when(reactiveWebServerApplicationContext).getWebServer();
polarisRegistration1 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
servletWebServerApplicationContext, null);
polarisRegistration2 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
null, reactiveWebServerApplicationContext);
polarisRegistration3 = new PolarisRegistration(polarisDiscoveryProperties, consulContextProperties,
polarisContext, staticMetadataManager, nacosContextProperties,
null, null);
}
@Test
public void testGetServiceId() {
assertThat(polarisRegistration.getServiceId()).isEqualTo(SERVICE_PROVIDER);
assertThat(polarisRegistration1.getServiceId()).isEqualTo(SERVICE_PROVIDER);
}
@Test
public void testGetHost() {
assertThat(polarisRegistration.getHost()).isEqualTo(HOST);
assertThat(polarisRegistration1.getHost()).isEqualTo(HOST);
}
@Test
public void testGetPort() {
assertThat(polarisRegistration.getPort()).isEqualTo(PORT);
assertThat(polarisRegistration1.getPort()).isEqualTo(PORT);
assertThat(polarisRegistration2.getPort()).isEqualTo(PORT + 1);
try {
polarisRegistration3.getPort();
}
catch (RuntimeException e) {
assertThat(e.getMessage()).isEqualTo("Unsupported web type.");
}
}
@Test
public void testIsSecure() {
assertThat(polarisRegistration.isSecure()).isFalse();
assertThat(polarisRegistration1.isSecure()).isFalse();
}
@Test
public void testGetUri() {
assertThat(polarisRegistration.getUri().toString()).isEqualTo("http://" + HOST + ":" + PORT);
assertThat(polarisRegistration1.getUri().toString()).isEqualTo("http://" + HOST + ":" + PORT);
}
@Test
public void testInstanceId() {
polarisRegistration1.setInstanceId("TEST");
assertThat(polarisRegistration1.getInstanceId()).isEqualTo("TEST");
}
@Test
public void testGetMetadata() {
Map<String, String> metadata = polarisRegistration.getMetadata();
Map<String, String> metadata = polarisRegistration1.getMetadata();
assertThat(metadata).isNotNull();
assertThat(metadata).isNotEmpty();
assertThat(metadata.size()).isEqualTo(3);
@ -119,17 +165,17 @@ public class PolarisRegistrationTest {
@Test
public void testGetPolarisProperties() {
assertThat(polarisRegistration.getPolarisProperties()).isNotNull();
assertThat(polarisRegistration1.getPolarisProperties()).isNotNull();
}
@Test
public void testIsRegisterEnabled() {
assertThat(polarisRegistration.isRegisterEnabled()).isTrue();
assertThat(polarisRegistration1.isRegisterEnabled()).isTrue();
}
@Test
public void testToString() {
System.out.println(polarisRegistration);
System.out.println(polarisRegistration1);
}
@Test
@ -137,7 +183,7 @@ public class PolarisRegistrationTest {
String groupName = "group";
String format = "%s__%s";
when(nacosContextProperties.getGroup()).thenReturn(groupName);
String serviceId = polarisRegistration.getServiceId();
String serviceId = polarisRegistration1.getServiceId();
assertThat(String.format(format, groupName, SERVICE_PROVIDER).equals(serviceId));
}
@ -145,7 +191,7 @@ public class PolarisRegistrationTest {
public void testGetNacosMetadata() {
String clusterName = "cluster";
when(nacosContextProperties.getClusterName()).thenReturn(clusterName);
Map<String, String> metadata = polarisRegistration.getMetadata();
Map<String, String> metadata = polarisRegistration1.getMetadata();
assertThat(metadata).isNotNull();
assertThat(metadata).isNotEmpty();
assertThat(metadata.size()).isEqualTo(4);

@ -14,6 +14,16 @@
<name>Polaris Discovery Callee Service</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>

@ -1,5 +1,5 @@
server:
port: 48084
port: ${random.int(7003,7005)}
spring:
application:
name: DiscoveryCalleeService

@ -18,6 +18,11 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>

@ -20,15 +20,10 @@
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>

@ -54,7 +54,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link EnhancedRestTemplateReporter}
* Test for {@link EnhancedRestTemplateReporter}.
*
* @author lepdou 2022-09-06
*/
@RunWith(MockitoJUnitRunner.class)

Loading…
Cancel
Save