SpringCloud服务如何在Eureka安全优雅的下线

原文:https://www.cnblogs.com/yangzhilong/p/7161941.html

如果直接KILL SpringCloud的服务,因为Eureka采用心跳的机制来上下线服务,会导致服务消费者调用此已经kill的服务提供者然后出错,处理这种情况有2中方案。

一、利用Spring Boot Actuato的管理端点(推荐

1、pom中引用Actuato

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、properties中添加如下内容

复制代码
#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
# 禁用actuator管理端鉴权
management.security.enabled=false

# 开启重启支持
endpoints.restart.enabled=true
复制代码

如果只允许本机访问,可以添加如下属性

#(只允许本机访问)
server.address=localhost

3、在服务器上用curl发送post请求到pause.

curl -X POST http://localhost:8080/pause

此时eurake上该服务被标记问下线,但该服务其实还是可以正常访问的,当client还未及时更新本地Instances缓存时,依然不会中断服务。当所有client都感知到该服务DOWN后就不会再往该服务发请求了。

4、在服务器上利用curl发送shutdown命令

curl -X POST http://localhost:8080/shutdown

或者

curl -d "" http://localhost:8080/shutdown

二、利用Eureka的rest管理端点下线服务

eureka界面注册的服务:

 发送DELETE的Restfull请求

对照关系看上面的2张图。

 注意:由于cloud服务是心跳检测,所有在eureka进行DELETE后要快速的停止服务,否则服务可能会被重新注册上。

原文地址:https://www.cnblogs.com/shihaiming/p/10136544.html