|
|
@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
import com.tencent.cloud.metadata.config.MetadataLocalProperties;
|
|
|
|
import com.tencent.cloud.metadata.config.MetadataLocalProperties;
|
|
|
|
import com.tencent.cloud.polaris.PolarisProperties;
|
|
|
|
import com.tencent.cloud.polaris.PolarisProperties;
|
|
|
|
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
|
|
|
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
|
|
|
|
|
|
|
import com.tencent.cloud.polaris.util.OkHttpUtil;
|
|
|
|
import com.tencent.polaris.api.core.ProviderAPI;
|
|
|
|
import com.tencent.polaris.api.core.ProviderAPI;
|
|
|
|
import com.tencent.polaris.api.exception.PolarisException;
|
|
|
|
import com.tencent.polaris.api.exception.PolarisException;
|
|
|
|
import com.tencent.polaris.api.pojo.Instance;
|
|
|
|
import com.tencent.polaris.api.pojo.Instance;
|
|
|
@ -32,6 +33,7 @@ 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.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.logging.log4j.util.Strings;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
@ -47,8 +49,7 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(PolarisServiceRegistry.class);
|
|
|
|
.getLogger(PolarisServiceRegistry.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int ttl = 5;
|
|
|
|
private static final int ttl = 5;
|
|
|
|
|
|
|
|
|
|
|
@ -60,15 +61,14 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
|
|
|
|
|
|
|
|
private final ScheduledExecutorService heartbeatExecutor;
|
|
|
|
private final ScheduledExecutorService heartbeatExecutor;
|
|
|
|
|
|
|
|
|
|
|
|
public PolarisServiceRegistry(PolarisProperties polarisProperties,
|
|
|
|
public PolarisServiceRegistry(PolarisProperties polarisProperties, PolarisDiscoveryHandler polarisDiscoveryHandler,
|
|
|
|
PolarisDiscoveryHandler polarisDiscoveryHandler,
|
|
|
|
|
|
|
|
MetadataLocalProperties metadataLocalProperties) {
|
|
|
|
MetadataLocalProperties metadataLocalProperties) {
|
|
|
|
this.polarisProperties = polarisProperties;
|
|
|
|
this.polarisProperties = polarisProperties;
|
|
|
|
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
|
|
|
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
|
|
|
this.metadataLocalProperties = metadataLocalProperties;
|
|
|
|
this.metadataLocalProperties = metadataLocalProperties;
|
|
|
|
if (polarisProperties.isHeartbeatEnabled()) {
|
|
|
|
if (polarisProperties.isHeartbeatEnabled()) {
|
|
|
|
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor(
|
|
|
|
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor(0,
|
|
|
|
0, new NamedThreadFactory("spring-cloud-heartbeat"));
|
|
|
|
new NamedThreadFactory("spring-cloud-heartbeat"));
|
|
|
|
heartbeatExecutor.setMaximumPoolSize(1);
|
|
|
|
heartbeatExecutor.setMaximumPoolSize(1);
|
|
|
|
this.heartbeatExecutor = heartbeatExecutor;
|
|
|
|
this.heartbeatExecutor = heartbeatExecutor;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -101,9 +101,9 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
|
|
|
|
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
|
|
|
|
providerClient.register(instanceRegisterRequest);
|
|
|
|
providerClient.register(instanceRegisterRequest);
|
|
|
|
log.info("polaris registry, {} {} {}:{} {} register finished",
|
|
|
|
log.info("polaris registry, {} {} {}:{} {} register finished",
|
|
|
|
polarisProperties.getNamespace(), registration.getServiceId(),
|
|
|
|
polarisProperties.getNamespace(),
|
|
|
|
registration.getHost(), registration.getPort(),
|
|
|
|
registration.getServiceId(), registration.getHost(),
|
|
|
|
metadataLocalProperties.getContent());
|
|
|
|
registration.getPort(), metadataLocalProperties.getContent());
|
|
|
|
|
|
|
|
|
|
|
|
if (null != heartbeatExecutor) {
|
|
|
|
if (null != heartbeatExecutor) {
|
|
|
|
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
|
|
|
|
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
|
|
|
@ -113,8 +113,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
catch (Exception e) {
|
|
|
|
log.error("polaris registry, {} register failed...{},",
|
|
|
|
log.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e);
|
|
|
|
registration.getServiceId(), registration, e);
|
|
|
|
|
|
|
|
rethrowRuntimeException(e);
|
|
|
|
rethrowRuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -141,8 +140,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
providerClient.deRegister(deRegisterRequest);
|
|
|
|
providerClient.deRegister(deRegisterRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
catch (Exception e) {
|
|
|
|
log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration,
|
|
|
|
log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration, e);
|
|
|
|
e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
finally {
|
|
|
|
if (null != heartbeatExecutor) {
|
|
|
|
if (null != heartbeatExecutor) {
|
|
|
@ -165,8 +163,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Object getStatus(Registration registration) {
|
|
|
|
public Object getStatus(Registration registration) {
|
|
|
|
String serviceName = registration.getServiceId();
|
|
|
|
String serviceName = registration.getServiceId();
|
|
|
|
InstancesResponse instancesResponse = polarisDiscoveryHandler
|
|
|
|
InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName);
|
|
|
|
.getInstances(serviceName);
|
|
|
|
|
|
|
|
Instance[] instances = instancesResponse.getInstances();
|
|
|
|
Instance[] instances = instancesResponse.getInstances();
|
|
|
|
if (null == instances || instances.length == 0) {
|
|
|
|
if (null == instances || instances.length == 0) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
@ -182,22 +179,27 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Start the heartbeat thread.
|
|
|
|
* Start the heartbeat thread.
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param heartbeatRequest heartbeat request
|
|
|
|
* @param heartbeatRequest heartbeat request
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) {
|
|
|
|
public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) {
|
|
|
|
heartbeatExecutor.scheduleWithFixedDelay(new Runnable() {
|
|
|
|
heartbeatExecutor.scheduleWithFixedDelay(() -> {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// String healthCheckUrl = String.format("http://%s:%s%s",
|
|
|
|
String healthCheckEndpoint = polarisProperties.getHealthCheckUrl();
|
|
|
|
// heartbeatRequest.getHost(), heartbeatRequest.getPort(),
|
|
|
|
//先判断是否配置了health-check-url,如果配置了,需要先进行服务实例健康检查,如果健康检查通过,则进行心跳上报,如果不通过,则不上报心跳
|
|
|
|
// polarisProperties.getHealthCheckUrl());
|
|
|
|
if (Strings.isNotEmpty(healthCheckEndpoint)) {
|
|
|
|
// //先判断是否配置了health-check-url,如果配置了,需要先进行服务实例健康检查,如果健康检查通过,则进行心跳上报,如果不通过,则不上报心跳
|
|
|
|
if (!healthCheckEndpoint.startsWith("/")) {
|
|
|
|
// if (Strings.isNotEmpty(healthCheckUrl) &&
|
|
|
|
healthCheckEndpoint = "/" + healthCheckEndpoint;
|
|
|
|
// !OkHttpUtil.get(healthCheckUrl, null)) {
|
|
|
|
}
|
|
|
|
// log.error("polaris health check failed");
|
|
|
|
|
|
|
|
// return;
|
|
|
|
String healthCheckUrl = String.format("http://%s:%s%s", heartbeatRequest.getHost(), heartbeatRequest.getPort(), healthCheckEndpoint);
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
if (!OkHttpUtil.get(healthCheckUrl, null)) {
|
|
|
|
|
|
|
|
log.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest);
|
|
|
|
polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (PolarisException e) {
|
|
|
|
catch (PolarisException e) {
|
|
|
@ -206,7 +208,6 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
|
|
|
|
catch (Exception e) {
|
|
|
|
catch (Exception e) {
|
|
|
|
log.error("polaris heartbeat runtime error", e);
|
|
|
|
log.error("polaris heartbeat runtime error", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 0, ttl, TimeUnit.SECONDS);
|
|
|
|
}, 0, ttl, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|