服务续约
服务续约的线程和 Eureka – 服务发现中的一样,这里不再解说,咱们间接通过 HeartbeatThread 的 run 办法看 renew 办法。
boolean renew() {
EurekaHttpResponse<InstanceInfo> httpResponse;
try {
// 调用 EurekaServer 地址 +"apps/" + appName + '/' + id
httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null);
logger.debug(PREFIX + "{} - Heartbeat status: {}", appPathIdentifier, httpResponse.getStatusCode());
if (httpResponse.getStatusCode() == Status.NOT_FOUND.getStatusCode()) {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() == Status.OK.getStatusCode();
} catch (Throwable e) {logger.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, e);
return false;
}
}
服务注册
在 Eureka – Client 服务启动中提到 initScheduledTasks 中会调用 InstanceInfoReplicator#start 办法。
public void start(int initialDelayMs) {if (started.compareAndSet(false, true)) {instanceInfo.setIsDirty(); // for initial register
Future next = scheduler.schedule(this, initialDelayMs, TimeUnit.SECONDS);
scheduledPeriodicRef.set(next);
}
}
start 办法会启动一个定时工作,就会调用 InstanceInfoReplicator#run 办法。refreshInstanceInfo 会刷新数据中心信息和租约信息。
public void run() {
try {
// 刷新数据中心信息和租约信息。discoveryClient.refreshInstanceInfo();
Long dirtyTimestamp = instanceInfo.isDirtyWithTime();
// 如果信息有变动,则注册
if (dirtyTimestamp != null) {// 注册,"apps/" + info.getAppName()
discoveryClient.register();
instanceInfo.unsetIsDirty(dirtyTimestamp);
}
} catch (Throwable t) {logger.warn("There was a problem with the instance info replicator", t);
} finally {
// 线程用于刷新信息以及从新注册
Future next = scheduler.schedule(this, replicationIntervalSeconds, TimeUnit.SECONDS);
scheduledPeriodicRef.set(next);
}
}
完结
直至,服务注册、服务发现、服务续约,client 局部曾经讲完了。