put请求, 有请求体的为服务下线, 称为服务的平滑下线。请求体为status
和value
delete请求没有请求体为服务下架。
/**
* Shuts down Eureka Client. Also sends a deregistration request to the
* eureka server.
*/
@PreDestroy
@Override
public synchronized void shutdown() {
if (isShutdown.compareAndSet(false, true)) {
logger.info("Shutting down DiscoveryClient ...");
if (statusChangeListener != null && applicationInfoManager != null) {
applicationInfoManager.unregisterStatusChangeListener(statusChangeListener.getId());
}
// 取消定时任务
cancelScheduledTasks();
// 未注册情况
if (applicationInfoManager != null
&& clientConfig.shouldRegisterWithEureka()
&& clientConfig.shouldUnregisterOnShutdown()) {
applicationInfoManager.setInstanceStatus(InstanceStatus.DOWN);
// 未注册情况
unregister();
}
if (eurekaTransport != null) {
eurekaTransport.shutdown();
}
heartbeatStalenessMonitor.shutdown();
registryStalenessMonitor.shutdown();
logger.info("Completed shut down of DiscoveryClient");
}
}
首先把定时器去掉了, 然后 cancelScheduledTasks, 然后
取消定时任务
private void cancelScheduledTasks() {
// 定时更新Client信息给Server
if (instanceInfoReplicator != null) {
instanceInfoReplicator.stop();
}
// 定时续约, 更新心跳
if (heartbeatExecutor != null) {
heartbeatExecutor.shutdownNow();
}
// 定时更新客户端注册表
if (cacheRefreshExecutor != null) {
cacheRefreshExecutor.shutdownNow();
}
if (scheduler != null) {
scheduler.shutdownNow();
}
}
这里有定时更新Client信息给Server, 定时续约, 定时更新客户端注册表, 定时器, 和两个任务都取消了
未注册的情况
void unregister() {
// It can be null if shouldRegisterWithEureka == false
if(eurekaTransport != null && eurekaTransport.registrationClient != null) {
try {
logger.info("Unregistering ...");
EurekaHttpResponse<Void> httpResponse = eurekaTransport.registrationClient.cancel(instanceInfo.getAppName(), instanceInfo.getId());
logger.info(PREFIX + "{} - deregister status: {}", appPathIdentifier, httpResponse.getStatusCode());
} catch (Exception e) {
logger.error(PREFIX + "{} - de-registration failed{}", appPathIdentifier, e.getMessage(), e);
}
}
}
cancal() -> delete()
服务下线是调用的是spring-cloud-commons包中的ServiceRegistryAutoConfiguration
Status 为 UP或OUT_OF_SERVICE
spring.factories:
有设置状态和得到状态的到注册中心。
@Override
public void setStatus(EurekaRegistration registration, String status) {
InstanceInfo info = registration.getApplicationInfoManager().getInfo();
// TODO: howto deal with delete properly?
if ("CANCEL_OVERRIDE".equalsIgnoreCase(status)) {
registration.getEurekaClient().cancelOverrideStatus(info);
return;
}
// TODO: howto deal with status types across discovery systems?
InstanceInfo.InstanceStatus newStatus = InstanceInfo.InstanceStatus
.toEnum(status);
registration.getEurekaClient().setStatus(newStatus, info);
}
CANCEL_OVERRIDE
Client会通过jersey向server提交delete请求, 用于在Server将InstanceInfo的overrideStatus
修改为UNKNOWN, 此时Client发送的心跳server不接受, 返回为404。
setStatus() -> statusUpdate() -> put()
携带Value
和 lastDirtyTimestamp
服务下架的话, 如果没有服务发现的功能的话会500