从单个 tomcat 上运行的多个应用程序关闭 spring boot 应用程序

我想以编程方式从单个 tomcat 服务器上运行的多个应用程序关闭 spring boot 应用程序,而不停止 tomcat。

我在谷歌上搜索发现了几个解决方案,例如

System.exit(0)

SpringbootApplication.exit()

导致关闭tomcat。我不想关闭tomcat。只是特定的应用程序。

我该怎么做......以编程方式有什么办法可以做到这一点。

请帮忙!


慕尼黑的夜晚无繁华
浏览 41回答 3
3回答

呼唤远方

一种方法是使用执行器。在您的 pom 中添加此依赖项<dependency>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; <artifactId>spring-boot-starter-actuator</artifactId></dependency>将这些属性添加到您的 yml / 属性文件中management.endpoint.shutdown.enabled=trueendpoints.shutdown.enabled=truemanagement.endpoints.web.exposure.include=*完成此操作后,您可以点击此休息端点来关闭应用程序http://host:port/actuator/shutdown这是一个POST调用。如果您在应用程序中使用 Spring Security,那么您可能需要进行一些调整以允许此端点通过。您可以使用curl来调用post调用,例如curl -X POST http://host:port/actuator/shutdown

噜噜哒

您可以通过注册一个端点(尽管高度安全)来做到这一点,您的应用程序可以向该端点发送关闭请求,并且您可以对端点进行编码,如下所示:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConfigurableApplicationContext ctx = SpringApplication.run(YourApplicationClassName.class, args);&nbsp; &nbsp; &nbsp; &nbsp; int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int getExitCode() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no errors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });安全性 - 我建议,如果您想通过其他应用程序向应用程序发送终止信号,您可以使用应用程序令牌来唯一标识有权关闭您的应用程序的应用程序。

芜湖不芜

一种方法是终止应用程序进程。首先,应用程序必须将其 PID 写入文件 (shutdown.pid):SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class)&nbsp; .web(WebApplicationType.NONE);app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));app.run();然后您可以创建一个文件(shutdown.bat)并添加以下行:kill $(cat ./bin/shutdown.pid)shutdown.bat 的执行从 shutdown.pid 文件中提取进程 ID,并使用kill 命令终止启动应用程序。ps:从这里偷来的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java