Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry (#612)

pull/623/head
小魏,小魏,我们要去哪里呀 2 years ago committed by GitHub
parent fd0502439d
commit 30c9e495a1
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.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<Registration> {
public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistration<PolarisRegistration> {
private static final Logger LOG = LoggerFactory.getLogger(PolarisAutoServiceRegistration.class);
private final PolarisRegistration registration;
public PolarisAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
public PolarisAutoServiceRegistration(ServiceRegistry<PolarisRegistration> serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
PolarisRegistration registration) {
super(serviceRegistry, autoServiceRegistrationProperties);

@ -56,6 +56,8 @@ public class PolarisRegistration implements Registration {
private String host;
private String instanceId;
public PolarisRegistration(
PolarisDiscoveryProperties polarisDiscoveryProperties,
@Nullable ConsulContextProperties consulContextProperties,
@ -121,6 +123,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;
@ -142,6 +153,8 @@ public class PolarisRegistration implements Registration {
", polarisContext=" + polarisContext +
", staticMetadataManager=" + staticMetadataManager +
", 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.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<Registration> {
public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistration> {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class);
@ -78,7 +78,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
}
@Override
public void register(Registration registration) {
public void register(PolarisRegistration registration) {
if (StringUtils.isEmpty(registration.getServiceId())) {
LOGGER.warn("No service to register for polaris client...");
return;
@ -102,7 +102,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
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(),
@ -122,7 +123,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
}
@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<Registration> {
}
@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();

@ -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<Registration> serviceRegistry;
private ServiceRegistry<PolarisRegistration> 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);

@ -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 {
@ -52,7 +52,7 @@ public class PolarisServiceInstance implements ServiceInstance {
@Override
public String getInstanceId() {
return ServiceInstance.super.getInstanceId();
return instance.getId();
}
@Override

Loading…
Cancel
Save