培森的Blog Java SpringBoot之部署以及Maven打包切换环境

SpringBoot之部署以及Maven打包切换环境

前言:之前一直用的  Java-jar 运行的,但是部署的时候得停止服务 通过端口kill 掉的,最…

前言:之前一直用的  Java-jar 运行的,但是部署的时候得停止服务 通过端口kill 掉的,最近在推酷上发现一个安全关闭springboot的博客 ,所以自己整理(搬运)一下。主要是英语有点差,最近也比较忙就没有关注springboot更新的文档。、

方式一:通过shell命令启动 关闭

1.1 后台运行

BUILD_ID=dontKillMe nohup java -jar 文件名.jar  --spring.profiles.active=prod >output 2>&1  &

1.2 关闭服务

#!/bin/bash
PID=$(ps -ef | grep 文件名.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo 服务已关闭
else
    echo 关闭服务中 $PID
    kill $PID
fi

目前我们公司项目都是通过jenkins执行的git maven打包并执行sh命令启动的。

 

方式二:通过 HTTP 发送 shutdown 信号

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

2.1  在 pom.xml 中引入 actuator 依赖

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

2.2  开启 shutdown endpoint

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

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

2.3  发送 shutdown 信号

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

curl -X POST host:port/shutdown

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

2.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

方式三:部署为Unix/Linux Service

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

3.1  在 pom.xml 中引入插件

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

3.2  设置为系统服务

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

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

3.3 赋予可执行权限:

chmod u+x app.jar

3.4  以系统服务的方式管理

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

 sudo service app start|stop

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

默认PID文件路径:/var/run/appname/appname.pid默认日志文件路径:/var/log/appname.log

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

自定义参数

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

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

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

JAVA_HOME=/usr/local/jdk
JAVA_OPTS=-Xmx1024M
LOG_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打包

   

<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配置

## dev | prod | test

spring.profiles.active=@environment@

本文来自网络,不代表培森的Blog立场,转载请注明出处:https://blog.xupeisen.com/archives/133

作者: 培森

联系我们

联系我们

13262951234

在线咨询: QQ交谈

邮箱: admin@xupeisen.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部