Spring Boot jar包 Centos7.5 部署的三种方式 sh、service、systemctl

方式一 sh 脚本启动

参考:https://blog.csdn.net/vakinge/article/details/78706679

启动脚本start.sh

#!/bin/sh

rm -f tpid
nohup java -Xms1536m -Xmx1536m -jar /appsystems/IFC/apps/ifc-0.0.1-SNAPSHOT.jar --spring.config.location=/appsystems/IFC/config/application.properties > /applogs/IFC/launch.log 2>&1 &
echo $! > tpid
echo Start Success!

关闭脚本stop.sh

#!/bin/sh
APP_NAME=service-wxcp-0.0.1-SNAPSHOT

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Stop Process...'
    kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Kill Process!'
    kill -9 $tpid
else
    echo 'Stop Success!'
fi

重启脚本:

#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh

集成版本:
 

#!/bin/bash
#
# chkconfig:   - 20 80
# description: Starts and stops the App.
# author:vakinge

ENV=dev
RUNNING_USER=vakinge
ADATE=`date +%Y%m%d%H%M%S`
APP_NAME=passport-server

APP_HOME=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
   APP_HOME=`dirname $0`
else
    dirname $0|grep "^\." >/dev/null
    retval=$?
    if [ $retval -eq 0 ];then
        APP_HOME=`dirname $0|sed "s#^.#$APP_HOME#"`
    else
        APP_HOME=`dirname $0|sed "s#^#$APP_HOME/#"`
    fi
fi

if [ ! -d "$APP_HOME/logs" ];then
  mkdir $APP_HOME/logs
fi

LOG_PATH=$APP_HOME/logs/$APP_NAME.out
GC_LOG_PATH=$APP_HOME/logs/gc-$APP_NAME-$ADATE.log
#JMX监控需用到
JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
#JVM参数
JVM_OPTS="-Dname=$APP_NAME -Djeesuite.configcenter.profile=$ENV -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -Xloggc:$GC_LOG_PATH -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"

JAR_FILE=$APP_NAME.jar
pid=0
start(){
  checkpid
  if [ ! -n "$pid" ]; then
    JAVA_CMD="nohup java -jar $JVM_OPTS $JAR_FILE > $LOG_PATH 2>&1 &"
    su - $RUNNING_USER -c "$JAVA_CMD"
    echo "---------------------------------"
    echo "启动完成,按CTRL+C退出日志界面即可>>>>>"
    echo "---------------------------------"
    sleep 2s
    tail -f $LOG_PATH
  else
      echo "$APP_NAME is runing PID: $pid"   
  fi

}


status(){
   checkpid
   if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
   else
     echo "$APP_NAME runing PID: $pid"
   fi 
}

checkpid(){
    pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'`
}

stop(){
    checkpid
    if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
    else
      echo "$APP_NAME stop..."
      kill -9 $pid
    fi 
}

restart(){
    stop 
    sleep 1s
    start
}

case $1 in  
          start) start;;  
          stop)  stop;; 
          restart)  restart;;  
          status)  status;;   
              *)  echo "require start|stop|restart|status"  ;;  
esac 

 

方式二 service 命令启动

参考:https://www.cnblogs.com/zhengshiqiang47/p/8119944.html

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

首先在spring boot工程的pom.xml 文件中添加上述修改

mavn 命令打包

clean package -Dmaven.test.skip=true

sftp 上传到 linux 服务器上

在linux环境中,可以为该jar包添加执行权限,类似chmod a+x xxx.jar

使用ln -s 命令添加软链接:
ln -s /path/to/your.jar /etc/init.d/yourappname

添加完成后,即可直接使用service命令:
service yourappname start/stop/status

遇到问题

[root@instance-lzteipxa ~]# service mylayui-0.0.1-SNAPSHOT start
Unable to find Java

原因系统中安装的是 tar.gz 版本 JDK是解压安装,路径安装在了/home/jun/jdk/jdk/jdk1.8.0_192,虽然配置了环境变量,但是 service 程序在/usr/bin下去找JDK的安装信息,需要将JDK指定为系统默认的JDK。

update-alternatives是 linux 系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本,比如,我们在系统中同时安装了open jdk和sun jdk两个版本,而我们又希望系统默认使用的是sun jdk,那怎么办呢?通过update-alternatives就可以很方便的实现了。
执行下面三行代码

sudo update-alternatives --install "/usr/bin/java" "java" "/home/leon/lib/jdk/jdk1.7.0_79/bin/java" 300
sudo update-alternatives --install "/usr/bin/javac" "javac" "/home/leon/lib/jdk/jdk1.7.0_79/bin/javac" 300
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/home/leon/lib/jdk/jdk1.7.0_79/bin/javaws" 300
update-alternatives --config java
 
update-alternatives --config javac
 
update-alternatives --config javaws

方法三 systemctl 命令启动

参考:https://blog.csdn.net/zeroctu/article/details/76538416

一. 关于systemctl的基本命令参考:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

二. 写service配置,两种方式:设定working目录,springboot会在working目录下寻找application.yml加载;不设定working目录,指定springboot配置文件 
2.1 设定working目录:
 

vim /usr/lib/systemd/system/wxcp.service
[Unit]
Description=test 

[Service]
WorkingDirectory=/usr/local/test
PrivateTmp=true
Restart=always
Type=simple
ExecStart=/usr/local/java/jdk1.8.0_101/bin/java -jar /usr/local/test/test.jar --spring.config.location=/usr/local/test/application.yml
ExecStop=/usr/bin/kill -15  $MAINPID

[Install]
WantedBy=multi-user.target

三. systemctl 管理,让配置生效。若是修改配置文件,需要reload:

sudo systemctl daemon-reload

 

sudo systemctl enable test.service

启动服务

systemctl start test.service

四. 在CentOS 7 / RHEL 7的系统中,使用Systemd替代了之前的SysV,因此 /etc/security/limits.conf 文件的配置作用域缩小了一些。limits.conf的配置,只适用于通过PAM认证登录用户的资源限制,对systemd的service的资源限制不生效,所以这里需要在[Service]中配置limits:

[Service]
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000

运行命令使其生效:

systemctl daemon-reload
systemctl restart test.service

查看该进程的limits:

cat /proc/PID/limits

systemd官方文档: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Services.html

转载于:https://my.oschina.net/webcreazy/blog/2254167

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值