Spring Boot 2.X优雅中止

本文章介绍了正常关闭Spring Boot 应用程序的过程。许多开发人员和架构师老是讨论SpringBoot的应用设计、流量负载、框架和应用模式,但不多有人讨论关闭阶段。生命周期意识能够说一个真正资深程序员应该具备的素质,对于重资源对象,包括线程池 链接池等都要善始善终,不能只顾建立。html

对于 SpringBoot 来讲,打包成一个简单的 Jar 包直接使用 java -jar便可启动,这是一种很是优雅的方式,但同时也带来了必定的问题,如:应用如何中止?在过去,应用程序是部署在特定的容器中的,使用容器提供的脚本能够优雅停服,但如今容器被内嵌了,脚本没有了,怎么办?直接 kill 是一种方式,但未免显得太过粗鲁,并且可能带来许多意想不到的问题。java


下面记录一种方案。程序员

使用 Endpoints

在 SpringBoot 官方文档的第5部分中介绍了为应用发布生产准备的各类特性,其中,经过 Actuator 的 HTTP Endpoint,开发人员能够方便地对应用的监控与管理。此方法配合winsw使用,适用于window服务器,有须要请参考:在windows服务器上使用winsw部署spring boot项目web

引入指定的SpringBoot starter包spring

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

在 application.properties 中打开以下两个配置,便可实现经过 Http 请求中止应用windows

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

操做命令以下:安全

curl -X POST http://host:port/actuator/shutdown

但这种方式有一个很是严重的问题,那就是任意人均可以控制应用的中止,为了保证系统的安全,须要给其加上一层 HTTP Basic Authbash

增长 Security 依赖:服务器

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

在 application.properties 中增长以下两个配置:架构

spring.security.user.name=actuator
spring.security.user.password=password
spring.security.user.roles=ACTUATOR

增长security过滤规则

@Configuration
public class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests().antMatchers("/").permitAll()
		.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
        .anyRequest().authenticated().and()
        //开启basic认证,若不添加此项,将不能经过curl的basic方式传递用户信息
        .httpBasic();
	}

}

配置完成后,最终的停服命令为:

curl -X POST -u actuator:password http://host:port/actuator/shutdown

执行返回:{"message":"Shutting down, bye..."},说明服务中止成功。

相关文章
相关标签/搜索