【问题标题】:management.server.port not shutdown when stopping spring application context停止 spring 应用程序上下文时 management.server.port 不关闭
【发布时间】:2018-10-14 15:04:55
【问题描述】:

我在使用以下方法停止 Spring 应用程序上下文时遇到问题: ConfigurableApplicationContext.close();

http 端口已停止,但弹簧执行器管理端口management.server.port 9001 未关闭。

所以当我尝试重新启动应用程序时,spring 给出以下异常: org.springframework.boot.web.server.PortInUseException: Port 9001 is already in use

谢谢。


使用 application.properties 的 sn-p 更新:

management.server.port=9001
management.server.ssl.enabled=false
management.endpoints.web.exposure.include=info,health

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    我不确定我是否会提供任何帮助,但首先我会尝试手动杀死 9001 上运行的任何东西。

    我使用命令:lsof -i :9001 获取 PID,然后使用kill -9 <PID>

    然后尝试再次运行应用程序,它应该可以正常运行。等待关机并重新开始。

    我在本地设置了一个非常基本的 Spring 应用程序,但无法复制该问题。

    package com.example.demo;
    
    import org.springframework.beans.BeansException;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class DemoApplication implements ApplicationContextAware {
    
      private static ApplicationContext appContext;
    
      public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    
        ((ConfigurableApplicationContext)appContext).close();
      }
    
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
      }
    }
    

    我的 yaml 文件如下所示:

    management:
      server:
        port: 9001
    
    server:
      port: 8080
    

    您能否提供一个配置文件示例以及您在哪里调用ConfigurableApplicationContext.close();

    【讨论】:

    • 我用一段 application.properties 更新了这个问题。但我发现即使我删除它们问题仍然发生在下一个端口,例如http 端口。关闭真的关闭了所有端口吗?
    • @user3675334 我在我的应用程序中放置了一个 Thread.sleep() 5 秒,以查看应用程序是否真的在 8080 上运行,然后在 5 秒后再次检查。一切都关闭了。我在想的是你的 Spring 上下文正在关闭,但你的服务器连接没有。我在 IntelliJ 中运行我的应用程序,您可以设置一个 tomcat 服务器配置。 Run/Debug Configuration -> + -> Tomcat Server -> On update action: Restart Server。这里的一些答案也可能会有所帮助stackoverflow.com/questions/29169916/…
    • 谢谢。最近太忙了。目前不在这,但我会在回到这个时尝试找出更多。
    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多