From d5c6d6a0b0079d774b63afd8edd762f08119331b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=AD=8F=2C=E5=B0=8F=E9=AD=8F=2C=E6=88=91?= =?UTF-8?q?=E4=BB=AC=E8=A6=81=E5=8E=BB=E5=93=AA=E9=87=8C=E5=91=80?= <30821411+galaxy-sea@users.noreply.github.com> Date: Tue, 27 Sep 2022 11:02:49 +0800 Subject: [PATCH] Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry (#611) --- .../PolarisAutoServiceRegistration.java | 7 +++---- .../polaris/registry/PolarisRegistration.java | 15 ++++++++++++++- .../registry/PolarisServiceRegistry.java | 17 +++++++++-------- .../PolarisAutoServiceRegistrationTest.java | 5 ++--- .../common/pojo/PolarisServiceInstance.java | 4 ++-- 5 files changed, 30 insertions(+), 18 deletions(-) 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 591d4f177..e8e8cdc04 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 @@ -22,22 +22,21 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; -import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.util.StringUtils; /** * Auto service registration of Polaris. * - * @author Haotian Zhang, Andrew Shan, Jie Cheng + * @author Haotian Zhang, Andrew Shan, Jie Cheng, changjin wei(魏昌进) */ -public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistration { +public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistration { private static final Logger log = LoggerFactory.getLogger(PolarisAutoServiceRegistration.class); private final PolarisRegistration registration; - public PolarisAutoServiceRegistration(ServiceRegistry serviceRegistry, + public PolarisAutoServiceRegistration(ServiceRegistry serviceRegistry, AutoServiceRegistrationProperties autoServiceRegistrationProperties, PolarisRegistration registration) { super(serviceRegistry, autoServiceRegistrationProperties); this.registration = registration; 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 a127dc276..c74829a2d 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 @@ -36,7 +36,7 @@ import org.springframework.util.CollectionUtils; /** * Registration object of Polaris. * - * @author Haotian Zhang, Andrew Shan, Jie Cheng + * @author Haotian Zhang, Andrew Shan, Jie Cheng, changjin wei(魏昌进) */ public class PolarisRegistration implements Registration { @@ -55,6 +55,8 @@ public class PolarisRegistration implements Registration { private String host; + private String instanceId; + public PolarisRegistration( PolarisDiscoveryProperties polarisDiscoveryProperties, @Nullable ConsulContextProperties consulContextProperties, @@ -120,6 +122,15 @@ public class PolarisRegistration implements Registration { return polarisDiscoveryProperties; } + @Override + public String getInstanceId() { + return instanceId; + } + + protected void setInstanceId(String instanceId) { + this.instanceId = instanceId; + } + public boolean isRegisterEnabled() { boolean registerEnabled = false; @@ -141,6 +152,8 @@ public class PolarisRegistration implements Registration { ", polarisContext=" + polarisContext + ", staticMetadataManager=" + staticMetadataManager + ", metadata=" + metadata + + ", host='" + host + '\'' + + ", instanceId='" + instanceId + '\'' + '}'; } } 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 5a78e913b..e27bb27b2 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 @@ -31,6 +31,7 @@ import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.rpc.InstanceDeregisterRequest; import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; import com.tencent.polaris.api.rpc.InstanceRegisterRequest; +import com.tencent.polaris.api.rpc.InstanceRegisterResponse; import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.client.util.NamedThreadFactory; import org.apache.commons.lang.StringUtils; @@ -38,7 +39,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; -import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import static java.util.concurrent.TimeUnit.SECONDS; @@ -47,9 +47,9 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; /** * Service registry of Polaris. * - * @author Haotian Zhang, Andrew Shan, Jie Cheng + * @author Haotian Zhang, Andrew Shan, Jie Cheng, changjin wei(魏昌进) */ -public class PolarisServiceRegistry implements ServiceRegistry { +public class PolarisServiceRegistry implements ServiceRegistry { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class); @@ -78,7 +78,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { } @Override - public void register(Registration registration) { + public void register(PolarisRegistration registration) { if (StringUtils.isBlank(registration.getServiceId())) { LOGGER.warn("No service to register for polaris client..."); @@ -103,7 +103,8 @@ public class PolarisServiceRegistry implements ServiceRegistry { instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion()); try { ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI(); - providerClient.register(instanceRegisterRequest); + InstanceRegisterResponse instanceRegisterResponse = providerClient.register(instanceRegisterRequest); + registration.setInstanceId(instanceRegisterResponse.getInstanceId()); LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), registration.getServiceId(), registration.getHost(), registration.getPort(), staticMetadataManager.getMergedStaticMetadata()); @@ -122,7 +123,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { } @Override - public void deregister(Registration registration) { + public void deregister(PolarisRegistration registration) { LOGGER.info("De-registering from Polaris Server now..."); if (StringUtils.isEmpty(registration.getServiceId())) { @@ -158,12 +159,12 @@ public class PolarisServiceRegistry implements ServiceRegistry { } @Override - public void setStatus(Registration registration, String status) { + public void setStatus(PolarisRegistration registration, String status) { } @Override - public Object getStatus(Registration registration) { + public Object getStatus(PolarisRegistration registration) { String serviceName = registration.getServiceId(); InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName); Instance[] instances = instancesResponse.getInstances(); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java index 1414b3d27..9e4a19cb5 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistrationTest.java @@ -25,7 +25,6 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; -import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; @@ -48,7 +47,7 @@ import static org.mockito.Mockito.doReturn; public class PolarisAutoServiceRegistrationTest { @Mock - private ServiceRegistry serviceRegistry; + private ServiceRegistry serviceRegistry; @Mock private AutoServiceRegistrationProperties autoServiceRegistrationProperties; @@ -71,7 +70,7 @@ public class PolarisAutoServiceRegistrationTest { public void setUp() { doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties(); - doNothing().when(serviceRegistry).register(nullable(Registration.class)); + doNothing().when(serviceRegistry).register(nullable(PolarisRegistration.class)); polarisAutoServiceRegistration = new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServiceInstance.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServiceInstance.java index 787abe3bf..d6abcdf69 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServiceInstance.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/pojo/PolarisServiceInstance.java @@ -29,7 +29,7 @@ import org.springframework.cloud.client.ServiceInstance; /** * Polaris's implementation of {@link ServiceInstance}. * - * @author Haotian Zhang + * @author Haotian Zhang, changjin wei(魏昌进) */ public class PolarisServiceInstance implements ServiceInstance { @@ -56,7 +56,7 @@ public class PolarisServiceInstance implements ServiceInstance { @Override public String getInstanceId() { - return ServiceInstance.super.getInstanceId(); + return instance.getId(); } @Override