一般停止服务方式都是先查找端口占用端口,然后kill pid,但是eureka在90s(默认)内不会立即踢出服务
actuator提供了接口可进行停止服务操作

pom.xml

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

支持退出接口

curl -X POST localhost:port/actuator/shutdown

properties

management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=shutdown

退出时触发动作,创建DestroyTask,主要注解@PreDestroy

@Slf4j
@Component
public class DestroyTask {
    @PreDestroy
    public void destroy() {
        log.info("start destroy action... ");
        //action
        log.info("over destroy action");
    }
}

调用

curl -X POST localhost:port/actuator/shutdown

会在eureka维护的服务列表中踢出当前服务,并停止服务,停止服务前执行方法destroy()