Spring Boot应用的启动和停止(Spring Boot应用通过start命令启动)

Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。

下面主要有两种方式进行Spring Boot的关闭:通过HTTP发送shutdown信号,或者通过service stop的方式。

一、通过HTTP发送shutdown信号关闭应用

该方式主要依赖Spring Boot Actuatorendpoint特性,具体步骤如下:

1、在pom.xml中引入actuator依赖

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

2、开启shutdown endpoint

Spring Boot Actuatorshutdown endpoint默认是关闭的,因此在application.properties中开启shutdown endpoint

#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false

指定路径、IP、端口

#指定shutdown endpoint的路径
endpoints.shutdown.path=/custompath
#也可以统一指定所有endpoints的路径`management.context-path=/manage`
#指定管理端口和IP
management.port=8081
management.address=127.0.0.1

3、发送shutdown信号

shutdown的默认urlhost:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:

curl -X POST host:port/shutdown

将得到形如{"message":"Shutting down, bye..."}的响应

4、安全设置

可以看出,使用该方法可以非常方便的进行远程操作,但是需要注意的是,正式使用时,必须对该请求进行必要的安全设置,比如借助spring-boot-starter-security进行身份认证:

pom.xml添加security依赖

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

开启安全验证
application.properties中变更配置

#开启shutdown的安全验证
endpoints.shutdown.sensitive=true
#验证用户名
security.user.name=admin
#验证密码
security.user.password=secret
#角色
management.security.role=SUPERUSER

注意:如果引入了security框架后,按照上面的配置,那么全部请求都会要求输入账号密码才能访问。

二、部署为Unix/Linux Service

该方式主要借助官方的spring-boot-maven-plugin创建"Fully executable" jar ,这中jar包内置一个shell脚本,可以方便的将该应用设置为Unix/Linux的系统服务(init.d service),官方对该功能在CentOS和Ubuntu进行了测试,对于OS X和FreeBSD,可能需要自定义。具体步骤如下:

1、在pom.xml中引入插件:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin> 

注意:标红部分的意思是是否是可以执行的。

2、赋予可执行权限:

chmod u+x app.jar 

说明:到了这一步之后基本可以在命令行运行,先打包出JAR包,然后启动,比如./app.jar start即可启动。

3、设置为系统服务

将你的应用打成JAR包,部署到服务器,假设部署路径为/var/app,包名为app.jar,通过如下方式将应该设置为一个系统服务:

sudo ln -s /var/app/app.jar /etc/init.d/app

4. 以系统服务的方式管理

接下来,就可以使用我们熟悉的service foo start|stop|restart来对应用进行启停等管理了

sudo service app start|stop

命令将得到形如Started|Stopped [PID]的结果反馈

默认PID文件路径:

/var/run/appname/appname.pid

默认日志文件路径:

/var/log/appname.log 

这可能是我们更熟悉也更常用的管理方式。

提示:上面的的日志和存放PID的文件根据不同的系统可能出现的位置不一样。

5、自定义参数

在这种方式下,我们还可以使用自定义的.conf文件来变更默认配置,方法如下:

1)在jar包相同路径下创建一个.conf文件,名称应该与.jar的名称相同,如appname.conf

2)在其中配置相关变量,如:

JAVA_HOME=/usr/local/jdk 
JAVA_OPTS=-Xmx1024M 
LOG_FOLDER=/custom/log

6、安全设置

作为应用服务,安全性是一个不能忽略的问题,如下一些操作可以作为部分基础设置参考:

  • 为服务创建一个独立的用户,同时最好将该用户的shell绑定为/usr/sbin/nologin
  • 赋予最小范围权限:chmod 500 app.jar
  • 阻止修改:sudo chattr +i app.jar
  • 对.conf文件做类似的工作:chmod 400 app.conf,sudo chown root:root app.conf

 

参考:

http://www.cnblogs.com/lobo/p/5657684.html(以上内容转自此篇文章)

Installing Spring Boot applications

Endpoints

Securing sensitive endpoints

原文地址:https://www.cnblogs.com/EasonJim/p/7753371.html