在springcloud微服务架构中,如果我们想停止某个微服务实例,最好不用用kill -9 服务pid 的方法暴力杀死进程。

如果直接kill -9 Springcloud的服务,因为Eureka采用心跳的机制来上下线服务,会导致服务消费者调用此已经kill的服务提供者,然后出错。

springboot1.x 中微服务优雅停机的配置:

1、 在微服务pom.xml文件中,配置spring-boot-starter-actuator 监控组件

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

2 、application.yml中配置

endpoints.shutdown.enabled:  true  #启用shutdown端点,以便支持优雅停机

#endpoints.shutdown.sensitive:  false  #禁用密码验证(可选)

3、在任意一台服务器上利用curl发送shutdown命令
curl -X POST http://ip:端口/shutdown
或者
curl -d "" http://ip:端口/shutdown

 

================================

springboot2.x 中微服务优雅停机配置

 

1、 在微服务pom.xml文件中,配置spring-boot-starter-actuator 监控组件

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

2、application.yml配置

#管理端点
management: 
#  endpoints:
#    web:
#      base-path: /actuator  #默认为 /actuator
  endpoints: 
    web:
      exposure:
        include:
        - shutdown
        - info
        - health
  endpoint:
    shutdown:
      enabled: true

#配置management的自定义端口,可以与server.port不同, 
#management.server.port: 8081
#management.server.address: 127.0.0.1 # management只能在本机访问

3、在任意一台服务器上利用curl发送shutdown命令

curl  -X POST http://ip:端口/actuator/shutdown 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐