diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a4bf4a74..8e3ba59fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java index cf1b9dfff..ea480d406 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java @@ -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 + diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java index e8e8cdc04..e4e74cf31 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java @@ -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(); } 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 0f1e1096b..7a5fb4609 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 @@ -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 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(); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 4a2afd880..4a185902b 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -174,7 +174,7 @@ public class PolarisServiceRegistry implements ServiceRegistry metadata = polarisRegistration.getMetadata(); + Map 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 metadata = polarisRegistration.getMetadata(); + Map metadata = polarisRegistration1.getMetadata(); assertThat(metadata).isNotNull(); assertThat(metadata).isNotEmpty(); assertThat(metadata.size()).isEqualTo(4); diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml index 7c72ba9d7..f57ab7ac0 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml @@ -14,6 +14,16 @@ Polaris Discovery Callee Service + + org.springframework.boot + spring-boot-starter-webflux + + + + + + + com.tencent.cloud spring-cloud-starter-tencent-polaris-discovery diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 9c57e95cb..7268c7fc4 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -1,5 +1,5 @@ server: - port: 48084 + port: ${random.int(7003,7005)} spring: application: name: DiscoveryCalleeService diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml index 27d228ee1..a61de58f6 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml @@ -18,6 +18,11 @@ + + org.springframework.boot + spring-boot-starter-web + + com.tencent.cloud spring-cloud-starter-tencent-polaris-discovery diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml index 865038c7f..2ce2a3e3d 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml @@ -20,15 +20,10 @@ - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-actuator - + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.cloud diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java index d84d7d335..437c53ba8 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java @@ -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)