Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry (#611)

pull/625/head
小魏,小魏,我们要去哪里呀 2 years ago committed by GitHub
parent 337b357176
commit d5c6d6a0b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,22 +22,21 @@ import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Auto service registration of Polaris. * 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<Registration> { public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistration<PolarisRegistration> {
private static final Logger log = LoggerFactory.getLogger(PolarisAutoServiceRegistration.class); private static final Logger log = LoggerFactory.getLogger(PolarisAutoServiceRegistration.class);
private final PolarisRegistration registration; private final PolarisRegistration registration;
public PolarisAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry, public PolarisAutoServiceRegistration(ServiceRegistry<PolarisRegistration> serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties, PolarisRegistration registration) { AutoServiceRegistrationProperties autoServiceRegistrationProperties, PolarisRegistration registration) {
super(serviceRegistry, autoServiceRegistrationProperties); super(serviceRegistry, autoServiceRegistrationProperties);
this.registration = registration; this.registration = registration;

@ -36,7 +36,7 @@ import org.springframework.util.CollectionUtils;
/** /**
* Registration object of Polaris. * 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 { public class PolarisRegistration implements Registration {
@ -55,6 +55,8 @@ public class PolarisRegistration implements Registration {
private String host; private String host;
private String instanceId;
public PolarisRegistration( public PolarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties, PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable ConsulContextProperties consulContextProperties, @Nullable ConsulContextProperties consulContextProperties,
@ -120,6 +122,15 @@ public class PolarisRegistration implements Registration {
return polarisDiscoveryProperties; return polarisDiscoveryProperties;
} }
@Override
public String getInstanceId() {
return instanceId;
}
protected void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public boolean isRegisterEnabled() { public boolean isRegisterEnabled() {
boolean registerEnabled = false; boolean registerEnabled = false;
@ -141,6 +152,8 @@ public class PolarisRegistration implements Registration {
", polarisContext=" + polarisContext + ", polarisContext=" + polarisContext +
", staticMetadataManager=" + staticMetadataManager + ", staticMetadataManager=" + staticMetadataManager +
", metadata=" + metadata + ", metadata=" + metadata +
", host='" + host + '\'' +
", instanceId='" + instanceId + '\'' +
'}'; '}';
} }
} }

@ -31,6 +31,7 @@ import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.rpc.InstanceDeregisterRequest; import com.tencent.polaris.api.rpc.InstanceDeregisterRequest;
import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest; 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.api.rpc.InstancesResponse;
import com.tencent.polaris.client.util.NamedThreadFactory; import com.tencent.polaris.client.util.NamedThreadFactory;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -38,7 +39,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
@ -47,9 +47,9 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/** /**
* Service registry of Polaris. * 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<Registration> { public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistration> {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class); private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class);
@ -78,7 +78,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
@Override @Override
public void register(Registration registration) { public void register(PolarisRegistration registration) {
if (StringUtils.isBlank(registration.getServiceId())) { if (StringUtils.isBlank(registration.getServiceId())) {
LOGGER.warn("No service to register for polaris client..."); LOGGER.warn("No service to register for polaris client...");
@ -103,7 +103,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion()); instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion());
try { try {
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI(); ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
providerClient.register(instanceRegisterRequest); InstanceRegisterResponse instanceRegisterResponse = providerClient.register(instanceRegisterRequest);
registration.setInstanceId(instanceRegisterResponse.getInstanceId());
LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), LOGGER.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(),
registration.getServiceId(), registration.getHost(), registration.getPort(), registration.getServiceId(), registration.getHost(), registration.getPort(),
staticMetadataManager.getMergedStaticMetadata()); staticMetadataManager.getMergedStaticMetadata());
@ -122,7 +123,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
@Override @Override
public void deregister(Registration registration) { public void deregister(PolarisRegistration registration) {
LOGGER.info("De-registering from Polaris Server now..."); LOGGER.info("De-registering from Polaris Server now...");
if (StringUtils.isEmpty(registration.getServiceId())) { if (StringUtils.isEmpty(registration.getServiceId())) {
@ -158,12 +159,12 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
@Override @Override
public void setStatus(Registration registration, String status) { public void setStatus(PolarisRegistration registration, String status) {
} }
@Override @Override
public Object getStatus(Registration registration) { public Object getStatus(PolarisRegistration registration) {
String serviceName = registration.getServiceId(); String serviceName = registration.getServiceId();
InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName); InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName);
Instance[] instances = instancesResponse.getInstances(); Instance[] instances = instancesResponse.getInstances();

@ -25,7 +25,6 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -48,7 +47,7 @@ import static org.mockito.Mockito.doReturn;
public class PolarisAutoServiceRegistrationTest { public class PolarisAutoServiceRegistrationTest {
@Mock @Mock
private ServiceRegistry<Registration> serviceRegistry; private ServiceRegistry<PolarisRegistration> serviceRegistry;
@Mock @Mock
private AutoServiceRegistrationProperties autoServiceRegistrationProperties; private AutoServiceRegistrationProperties autoServiceRegistrationProperties;
@ -71,7 +70,7 @@ public class PolarisAutoServiceRegistrationTest {
public void setUp() { public void setUp() {
doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties(); doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties();
doNothing().when(serviceRegistry).register(nullable(Registration.class)); doNothing().when(serviceRegistry).register(nullable(PolarisRegistration.class));
polarisAutoServiceRegistration = polarisAutoServiceRegistration =
new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration); new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration);

@ -29,7 +29,7 @@ import org.springframework.cloud.client.ServiceInstance;
/** /**
* Polaris's implementation of {@link ServiceInstance}. * Polaris's implementation of {@link ServiceInstance}.
* *
* @author Haotian Zhang * @author Haotian Zhang, changjin wei()
*/ */
public class PolarisServiceInstance implements ServiceInstance { public class PolarisServiceInstance implements ServiceInstance {
@ -56,7 +56,7 @@ public class PolarisServiceInstance implements ServiceInstance {
@Override @Override
public String getInstanceId() { public String getInstanceId() {
return ServiceInstance.super.getInstanceId(); return instance.getId();
} }
@Override @Override

Loading…
Cancel
Save