服务续约
服务续约的线程和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局部曾经讲完了。
发表回复