You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Eureka客户端注册后立即注销问题排查求助

Spring Eureka Client注册后立即注销问题排查

我尝试将Spring Eureka客户端注册到Eureka Server,但客户端完成注册后随即注销,相关日志信息如下:

Eureka Server日志

2018-05-13 16:02:47.290 INFO 25557 --- [io-9091-exec-10] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-CLIENT/192.168.43.96:hello-client:8072 with status UP (replication=false) 
2018-05-13 16:02:47.438 INFO 25557 --- [nio-9091-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-CLIENT/192.168.43.96:hello-client:8072 with status DOWN (replication=false) 
2018-05-13 16:02:47.457 INFO 25557 --- [nio-9091-exec-2] c.n.e.registry.AbstractInstanceRegistry : Cancelled instance HELLO-CLIENT/192.168.43.96:hello-client:8072 (replication=false) 
2018-05-13 16:02:47.950 INFO 25557 --- [nio-9091-exec-5] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-CLIENT/192.168.43.96:hello-client:8072 with status DOWN (replication=true) 
2018-05-13 16:02:47.951 INFO 25557 --- [nio-9091-exec-5] c.n.e.registry.AbstractInstanceRegistry : Cancelled instance HELLO-CLIENT/192.168.43.96:hello-client:8072 (replication=true) 
2018-05-13 16:03:25.747 INFO 25557 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 4ms

Eureka Client日志

2018-05-13 16:02:47.163 INFO 25676 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLO-CLIENT/192.168.43.96:hello-client:8072: registering service... 
2018-05-13 16:02:47.212 INFO 25676 --- [main] c.a.helloclient.HelloClientApplication : Started HelloClientApplication in 7.62 seconds (JVM running for 8.573) 
2018-05-13 16:02:47.224 INFO 25676 --- [ Thread-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f7923a5: startup date [Sun May 13 16:02:42 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5c30a9b0 
2018-05-13 16:02:47.226 INFO 25676 --- [ Thread-5] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application hello-client with eureka with status DOWN 
2018-05-13 16:02:47.227 WARN 25676 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1526207567227, current=DOWN, previous=UP] 
2018-05-13 16:02:47.232 INFO 25676 --- [ Thread-5] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0 
2018-05-13 16:02:47.235 INFO 25676 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ... 
2018-05-13 16:02:47.292 INFO 25676 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLO-CLIENT/192.168.43.96:hello-client:8072 - registration status: 204 
2018-05-13 16:02:47.423 INFO 25676 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLO-CLIENT/192.168.43.96:hello-client:8072: registering service... 
2018-05-13 16:02:47.440 INFO 25676 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLO-CLIENT/192.168.43.96:hello-client:8072 - registration status: 204 
2018-05-13 16:02:47.442 INFO 25676 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Unregistering ... 
2018-05-13 16:02:47.460 INFO 25676 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLO-CLIENT/192.168.43.96:hello-client:8072 - deregister status: 200 
2018-05-13 16:02:47.494 INFO 25676 --- [ Thread-5] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient 
2018-05-13 16:02:47.495 INFO 25676 --- [ Thread-5] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 
2018-05-13 16:02:47.498 INFO 25676 --- [ Thread-5] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans

问题原因分析

从客户端日志里的这两行关键信息就能定位根源:

2018-05-13 16:02:47.224 INFO 25676 --- [ Thread-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f7923a5...
2018-05-13 16:02:47.226 INFO 25676 --- [ Thread-5] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application hello-client with eureka with status DOWN

这说明你的Eureka客户端应用启动完成后立刻关闭了Spring上下文,整个应用进程直接终止了。Eureka客户端在应用关闭时会主动向Server发送注销请求,所以就出现了注册后马上注销的情况。

最常见的触发场景:

  • 客户端项目是普通Spring Boot项目,没有引入spring-boot-starter-web这类Web依赖,导致Spring容器启动后没有需要保持进程存活的组件(比如内嵌Tomcat),容器完成初始化后就直接退出了。
  • 项目中没有任何需要持续运行的业务逻辑,比如没有Web接口、定时任务、消息消费者等,没有东西让JVM保持运行状态。

解决办法

根据你的应用类型选择对应的方案:

  1. 如果是Web服务:
    确保项目中引入了Spring Web依赖。

    • Maven项目,在pom.xml中添加:
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
    • Gradle项目,在build.gradle中添加:
      implementation 'org.springframework.boot:spring-boot-starter-web'
      

    引入后,Spring Boot会启动内嵌Tomcat,保持应用进程持续运行,Eureka客户端也就不会主动注销了。

  2. 如果是非Web服务(比如后台任务):
    需要手动让Spring容器保持运行,可以通过以下方式:
    添加一个ApplicationRunner实现类,在run方法中阻塞线程:

    @Component
    public class KeepAliveRunner implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            Thread.sleep(Long.MAX_VALUE);
        }
    }
    

    这样应用启动后会一直保持运行状态,直到手动停止。

内容的提问来源于stack exchange,提问作者user9584853

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:33:07