@ -49,7 +49,8 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
* /
public class PolarisServiceRegistry implements ServiceRegistry < Registration > {
private static final Logger log = LoggerFactory . getLogger ( PolarisServiceRegistry . class ) ;
private static final Logger log = LoggerFactory
. getLogger ( PolarisServiceRegistry . class ) ;
private static final int ttl = 5 ;
@ -61,14 +62,15 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
private final ScheduledExecutorService heartbeatExecutor ;
public PolarisServiceRegistry ( PolarisProperties polarisProperties , PolarisDiscoveryHandler polarisDiscoveryHandler ,
public PolarisServiceRegistry ( PolarisProperties polarisProperties ,
PolarisDiscoveryHandler polarisDiscoveryHandler ,
MetadataLocalProperties metadataLocalProperties ) {
this . polarisProperties = polarisProperties ;
this . polarisDiscoveryHandler = polarisDiscoveryHandler ;
this . metadataLocalProperties = metadataLocalProperties ;
if ( polarisProperties . isHeartbeatEnabled ( ) ) {
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor ( 0 ,
new NamedThreadFactory ( "spring-cloud-heartbeat" ) ) ;
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor (
0 , new NamedThreadFactory ( "spring-cloud-heartbeat" ) ) ;
heartbeatExecutor . setMaximumPoolSize ( 1 ) ;
this . heartbeatExecutor = heartbeatExecutor ;
}
@ -101,9 +103,9 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
ProviderAPI providerClient = polarisDiscoveryHandler . getProviderAPI ( ) ;
providerClient . register ( instanceRegisterRequest ) ;
log . info ( "polaris registry, {} {} {}:{} {} register finished" ,
polarisProperties . getNamespace ( ) ,
registration . get ServiceId( ) , registration . getHos t( ) ,
registration. getPort ( ) , metadataLocalProperties. getContent ( ) ) ;
polarisProperties . getNamespace ( ) , registration . getServiceId ( ) ,
registration . get Host( ) , registration . getPor t( ) ,
metadataLocalProperties. getContent ( ) ) ;
if ( null ! = heartbeatExecutor ) {
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest ( ) ;
@ -113,7 +115,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
}
}
catch ( Exception e ) {
log . error ( "polaris registry, {} register failed...{}," , registration . getServiceId ( ) , registration , e ) ;
log . error ( "polaris registry, {} register failed...{}," ,
registration . getServiceId ( ) , registration , e ) ;
rethrowRuntimeException ( e ) ;
}
}
@ -140,7 +143,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
providerClient . deRegister ( deRegisterRequest ) ;
}
catch ( Exception e ) {
log . error ( "ERR_POLARIS_DEREGISTER, de-register failed...{}," , registration , e ) ;
log . error ( "ERR_POLARIS_DEREGISTER, de-register failed...{}," , registration ,
e ) ;
}
finally {
if ( null ! = heartbeatExecutor ) {
@ -163,7 +167,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
@Override
public Object getStatus ( Registration registration ) {
String serviceName = registration . getServiceId ( ) ;
InstancesResponse instancesResponse = polarisDiscoveryHandler . getInstances ( serviceName ) ;
InstancesResponse instancesResponse = polarisDiscoveryHandler
. getInstances ( serviceName ) ;
Instance [ ] instances = instancesResponse . getInstances ( ) ;
if ( null = = instances | | instances . length = = 0 ) {
return null ;
@ -179,7 +184,6 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
/ * *
* Start the heartbeat thread .
*
* @param heartbeatRequest heartbeat request
* /
public void heartbeat ( InstanceHeartbeatRequest heartbeatRequest ) {
@ -192,10 +196,14 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
healthCheckEndpoint = "/" + healthCheckEndpoint ;
}
String healthCheckUrl = String . format ( "http://%s:%s%s" , heartbeatRequest . getHost ( ) , heartbeatRequest . getPort ( ) , healthCheckEndpoint ) ;
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 ) ;
log . error (
"backend service health check failed. health check endpoint = {}" ,
healthCheckEndpoint ) ;
return ;
}
}