SpringBoot之部署 http://my.oschina.net/wangnian/blog/714719
前言:之前一直用的 Java-jar 运行的,但是部署的时候得停止服务 通过端口kill 掉的,最近在推酷上发现一个安全关闭springboot的博客 ,所以自己整理(搬运)一下。主要是英语有点差,最近也比较忙 就没有关注springboot更新的文档。
kill方式这里就不说了。我的项目都是通过jenkins执行的git maven打包并执行sh命令启动的。
主要有两种方式:通过 HTTP
发送 shutdown
信号,或者通过 service stop
的方式
方式一:通过 HTTP
发送 shutdown
信号
该方式主要依赖 Spring Boot Actuator
的 endpoint
特性,具体步骤如下:
1. 在 pom.xml
中引入 actuator
依赖
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
2. 开启 shutdown endpoint
Spring Boot Actuator
的 shutdown endpoin
t默认是关闭的,因此在 application.properties
中开启 shutdown endpoint
:
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
3. 发送 shutdown
信号
shutdown
的默认 url
为 host:port/shutdown
,当需要停止服务时,向服务器post
该请求即可,如:
1 |
curl -X POST host:port/shutdown |
将得到形如 {"message":"Shutting down, bye..."}
的响应
4. 安全设置
可以看出,使用该方法可以非常方便的进行远程操作,但是需要注意的是,正式使用时,必须对该请求进行必要的安全设置,比如借助 spring-boot-starter-security
进行身份认证:
- pom.xml添加security依赖
-
1234<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
- 开启安全验证
在
application.properties
中变更配置,并12345678#开启shutdown的安全验证endpoints.shutdown.sensitive=true#验证用户名security.user.name=admin#验证密码security.user.password=secret#角色management.security.role=SUPERUSER
方式二:部署为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
中引入插件:
1 2 3 4 5 6 7 |
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> |
2. 设置为系统服务
将你的应用打成jar包,部署到服务器,假设部署路径为/var/app,包名为app.jar,通过如下方式将应该设置为一个系统服务:
1 |
sudo ln -s /var/app/app.jar /etc/init.d/app |
3. 赋予可执行权限:
1 |
chmod u+x app.jar |
4. 以系统服务的方式管理
接下来,就可以使用我们熟悉的service foo start|stop|restart来对应用进行启停等管理了
1 |
sudo service app start|stop |
命令将得到形如 Started|Stopped [PID]
的结果反馈
默认PID文件路径:/var/run/appname/appname.pid默认日志文件路径:/var/log/appname.log
这可能是我们更熟悉也更常用的管理方式。
自定义参数
在这种方式下,我们还可以使用自定义的.conf文件来变更默认配置,方法如下:
- 在jar包相同路径下创建一个.conf文件,名称应该与.jar的名称相同,如appname.conf
- 在其中配置相关变量,如:
123JAVA_HOME=/usr/local/jdkJAVA_OPTS=-Xmx1024MLOG_FOLDER=/custom/log
安全设置
作为应用服务,安全性是一个不能忽略的问题,如下一些操作可以作为部分基础设置参考:
- 为服务创建一个独立的用户,同时最好将该用户的shell绑定为/usr/sbin/nologin
- 赋予最小范围权限:
chmod 500 app.jar
- 阻止修改:
sudo chattr +i app.jar
- 对.conf文件做类似的工作:
chmod 400 app.conf
,sudo chown root:root a
两种方式,我都弄了一下,第一种HTTP方式我发现在jenkins中想批处理停掉服务并启动有点麻烦,如果直接执行curl -X POST host:port/shutdown命令的话安全性就有问题,所以得集成 security,需要验证通过才能执行。每次部署前手动停掉感觉会多此一举,所以我选择了第二种方式。第二种方式的话需要设置 <configuration><executable>true</executable></configuration>,在这个环节居然与我集成的mybatis有关系(我之前xml只需要写相对的类名就行了,不需要写完整的路径,方便之后的改实体类的包路径),所以花了好大一会时间修改所有的xml的包路径。
以上的官方文档地址:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#deployment-install
通过maven打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<profiles> <profile> <id>dev</id> <!--<activation>--> <!--<activeByDefault>true</activeByDefault>--> <!--</activation>--> <properties> <environment>dev</environment> </properties> </profile> <profile> <id>test</id> <properties> <environment>test</environment> </properties> </profile> <profile> <id>prod</id> <properties> <environment>prod</environment> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <excludes> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-prod.properties</exclude> <exclude>application.properties</exclude> </excludes> </resource> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>application-${environment}.properties</include> <include>application.properties</include> </includes> </resource> </resources> </build> |
在application.properties配置
1 2 |
## dev | prod | test spring.profiles.active=@environment@ |