1. Overview
1.概述
Managing the lifecycle of Spring Boot Application is very important for a production-ready system. The Spring container handles the creation, initialization, and destruction of all the Beans with the help of the ApplicationContext.
管理Spring Boot应用程序的生命周期对于一个可以投入生产的系统来说是非常重要的。Spring容器在ApplicationContext.的帮助下处理所有Bean的创建、初始化和销毁。
The emphasize of this write-up is the destruction phase of the lifecycle. More specifically, we’ll have a look at different ways to shut down a Spring Boot Application.
本篇文章的重点是生命周期的销毁阶段。更具体地说,我们将看看关闭Spring Boot应用程序的不同方法。
To learn more about how to set up a project using Spring Boot, check out the Spring Boot Starter article, or go over the Spring Boot Configuration.
要了解有关如何使用Spring Boot设置项目的更多信息,请查看Spring Boot Starter文章,或翻阅Spring Boot配置。
2. Shutdown Endpoint
2.关闭端点
By default, all the endpoints are enabled in Spring Boot Application except /shutdown; this is, naturally, part of the Actuator endpoints.
默认情况下,除了/shutdown,所有的端点都在Spring Boot Application中被启用;这自然是Actuator端点的一部分。
Here’s the Maven dependency to set up these up:
下面是设置这些的Maven依赖性。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
And, if we want to also set up security support, we need:
而且,如果我们想同时设置安全支持,我们需要。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Lastly, we enable the shutdown endpoint in application.properties file:
最后,我们在application.properties文件中启用关闭端点。
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
Note that we also have to expose any actuator endpoints that we want to use. In the example above, we’ve exposed all the actuator endpoints which will include the /shutdown endpoint.
请注意,我们还必须公开我们想要使用的任何执行器端点。在上面的例子中,我们已经暴露了所有的执行器端点,其中包括/shutdown端点。
To shut down the Spring Boot application, we simply call a POST method like this:
为了关闭Spring Boot应用程序,我们只需像这样调用一个POST方法。
curl -X POST localhost:port/actuator/shutdown
In this call, the port represents the actuator port.
在这个调用中,port代表执行器端口。
3. Close Application Context
3.关闭应用程序上下文
We can also call the close() method directly using the application context.
我们也可以使用应用程序上下文直接调用close()方法。
Let’s start with an example of creating a context and closing it:
让我们从创建一个上下文并关闭它的例子开始。
ConfigurableApplicationContext ctx = new
SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run();
System.out.println("Spring Boot application started");
ctx.getBean(TerminateBean.class);
ctx.close();
This destroys all the beans, releases the locks, then closes the bean factory. To verify the application shutdown, we use the Spring’s standard lifecycle callback with @PreDestroy annotation:
这将销毁所有的Bean,释放锁,然后关闭Bean工厂。为了验证应用程序的关闭,我们使用带有@PreDestroy注解的Spring的标准生命周期回调。
public class TerminateBean {
@PreDestroy
public void onDestroy() throws Exception {
System.out.println("Spring Container is destroyed!");
}
}
We also have to add a bean of this type:
我们还必须添加一个这种类型的Bean。
@Configuration
public class ShutdownConfig {
@Bean
public TerminateBean getTerminateBean() {
return new TerminateBean();
}
}
Here’s the output after running this example:
下面是运行这个例子后的输出。
Spring Boot application started
Closing AnnotationConfigApplicationContext@39b43d60
DefaultLifecycleProcessor - Stopping beans in phase 0
Unregistering JMX-exposed beans on shutdown
Spring Container is destroyed!
The important thing here to keep in mind: while closing the application context, the parent context isn’t affected due to separate lifecycles.
这里需要记住的重要一点是。在关闭应用程序上下文时,由于独立的生命周期,父级上下文不受影响。
3.1. Close the Current Application Context
3.1.关闭当前应用程序上下文
In the example above, we created a child application context, then used the close() method to destroy it.
在上面的例子中,我们创建了一个子应用程序上下文,然后使用close()方法来销毁它。
If we want to close the current context, one solution is to simply call the actuator /shutdown endpoint.
如果我们想关闭当前的上下文,一个解决方案是简单地调用执行器/shutdown端点。
However, we can also create our own custom endpoint:
然而,我们也可以创建我们自己的自定义端点。
@RestController
public class ShutdownController implements ApplicationContextAware {
private ApplicationContext context;
@PostMapping("/shutdownContext")
public void shutdownContext() {
((ConfigurableApplicationContext) context).close();
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.context = ctx;
}
}
Here, we’ve added a controller that implements the ApplicationContextAware interface and overrides the setter method to obtain the current application context. Then, in a mapping method, we’re simply calling the close() method.
在这里,我们添加了一个控制器,它实现了ApplicationContextAware接口,并重写了setter方法以获得当前的应用上下文。然后,在一个映射方法中,我们简单地调用close()方法。
We can then call our new endpoint to shut down the current context:
然后我们可以调用我们的新端点来关闭当前的上下文。
curl -X POST localhost:port/shutdownContext
Of course, if you add an endpoint like this in a real-life application, you’ll want to secure it as well.
当然,如果你在现实生活中的应用中添加了这样一个端点,你也会想要保护它。
4. Exit SpringApplication
4.退出SpringApplication
SpringApplication registers a shutdown hook with the JVM to make sure the application exits appropriately.
SpringApplication向JVM注册了一个shutdown钩,以确保应用程序适当地退出。
Beans may implement the ExitCodeGenerator interface to return a specific error code:
Bean可以实现ExitCodeGenerator接口来返回一个特定的错误代码。
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE).run();
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
// return the error code
return 0;
}
});
System.exit(exitCode);
The same code with the application of Java 8 lambdas:
同样的代码,应用了Java 8的lambdas。
SpringApplication.exit(ctx, () -> 0);
After calling the System.exit(exitCode), the program terminates with a 0 return code:
在调用System.exit(exitCode)后,程序以0的返回代码终止。
Process finished with exit code 0
5. Kill the App Process
5.关闭应用程序进程
Finally, we can also shut down a Spring Boot Application from outside the application by using a bash script. Our first step for this option is to have the application context write it’s PID into a file:
最后,我们还可以通过使用bash脚本从应用程序外部关闭Spring Boot应用程序。这个选项的第一步是让应用程序的上下文把它的PID写进一个文件。
SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE);
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();
Next, create a shutdown.bat file with the following content:
接下来,创建一个shutdown.bat文件,内容如下。
kill $(cat ./bin/shutdown.pid)
The execution of shutdown.bat extracts the Process ID from the shutdown.pid file and uses the kill command to terminate the Boot application.
执行shutdown.bat从shutdown.pid文件中提取进程ID,并使用kill命令来终止Boot应用程序。
6. Conclusion
6.结论
In this quick write-up, we’ve covered few simple methods that can be used to shut down a running Spring Boot Application.
在这篇简短的文章中,我们已经介绍了一些简单的方法,可以用来关闭正在运行的Spring Boot应用程序。
While it’s up to the developer to choose an appropriate a method; all of these methods should be used by design and on purpose.
虽然要由开发者来选择适当的方法,但所有这些方法都应该在设计上有目的地使用。
For example, .exit() is preferred when we need to pass an error code to another environment, say JVM for further actions. Using Application PID gives more flexibility, as we can also start or restart the application with the use of bash script.
例如,当我们需要将错误代码传递给另一个环境时,例如JVM,以便采取进一步行动,.exit()是首选。使用 Application PID可以提供更多的灵活性,因为我们也可以通过使用bash脚本来启动或重新启动应用程序。
Finally, /shutdown is here to make it possible to terminate the applications externally via HTTP. For all the other cases .close() will work perfectly.
最后,/shutdown是为了使通过HTTP从外部终止应用程序成为可能。对于所有其他情况,.close()将完美工作。
As usual, the complete code for this article is available over on the GitHub project.
像往常一样,本文的完整代码可以在GitHub项目上找到。