本文主要是介绍Eureka客户端续约及服务端过期租约清理源码...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Eureka客户端续约及服务端过期租约清理源码解析 在之前的文章:EurekaClient自动装配及启动流程解析中,我们提到了在构造DiscoveryClient时除了包含注册流程之外,还调度了一个心跳线程: scheduler.schedule( new TimedSupervisorTask( "heartbeat", scheduler, heartbeatExecutor, renewalIntervalInSecs, TimeUnit.SECONDS, expBackOffBound, new HeartbeatThread() ), renewalIntervalInSecs, TimeUnit.SECONDS); 其中HeartbeatThread线程如下: private class HeartbeatThread implements Runnable { public void run() { //续约 if (renew()) { //续约成功时间戳更新 lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis(); } } } boolean renew() { EurekaHttpResponse<InstanceInfo> httpResponse; try { //发送续约请求 httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null); logger.debug(PREFIX + "{} - Heartbeat status: {}", appPathIdentifier, httpResponse.getStatusCode()); if (httpResponse.getStatusCode() == 404) { REREGISTER_COUNTER.increment(); logger.info(PREFIX + "{} - Re-registering apps/{}", appPathIdentifier, instanceInfo.getAppName()); long timestamp = instanceInfo.setIsDirtyWithTime(); //重新注册 boolean success = register(); if (success) { instanceInfo.unsetIsDirty(timestamp); } return success; } return httpResponse.getStatusCode() == 200; } catch (Throwable e) { logger.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, e); return false; } } 这里直接发出了续约请求,如果续约请求失败则会尝试再次去注册 服务端接受续约请求 服务端接受续约请求的Controller在InstanceResource类中 @PUT public Respon |
|
这篇关于Eureka客户端续约及服务端过期租约清理源码...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!