温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring boot 配置成 linux service

发布时间:2020-07-18 15:02:32 来源:网络 阅读:399 作者:沙漏半杯 栏目:编程语言

最近,要把公司以前的一个java application 同步程序修改一下,让它变成一个容易部署而且运行稳健的服务。


网上找了一下资料,决定动手把它变成一个spring boot项目,理由有3:


(1)易维护;


(2)易部署;


(3)较稳健;




做了以下步骤:


1.把它从传统项目变为maven项目,好处是大家都知的.


2.把它从一般的java application 变为spring boot application.


3.把它部署成Linux的系统服务.


听起来是容易的,实际也是容易,只要不做错就行,呵呵。在这里,重点说一下第三步.


以下为ubuntu环境:


(1)先生成spring boot 项目的jar包,往pom.xml里加点料,不然做不了的.


<build>

<plug×××>

  <plugin>

  <groupId>org.apache.maven.plug×××</groupId>

  <artifactId>maven-surefire-plugin</artifactId>

  <version>2.19.1</version>

</plugin>

<plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

            <version>1.5.9.RELEASE</version>

            <executions>

                <execution>

                    <goals>

                        <goal>repackage</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

  </plug×××>

    </build>

然后用

mvn clean compile //重新编译


mvn package skipTests //生成包啦


(2)把生成的jar包 oee-sync-tool-0.1.jar copy 到/home/page/soft目录下


(3)在/etc/systemd/system下创建文件oee-sync-tool.service如下内容:


[Unit]

Description=oee-sync-tool Service

After=syslog.target

 

[Service]

ExecStart=/opt/java/jdk1.8.0_131/bin/java -jar /home/page/soft/oee-sync-tool-0.1.jar --spring.profiles.active=dev

SuccessExitStatus=143

 

[Install]

WantedBy=multi-user.target


然后输入启动服务 :   service oee-sync-tool restart

查看服务:service oee-sync-tool status


停止服务:service oee-sync-tool stop


日志嘛,是放在/logs下面的sync-error.log,sync-info.log


(4)大功告成




以下是Centos环境:


(1)在/etc/init.d下新建一个oee-sync-tool文件,内容如下:




#!/bin/bash

#

# chkconfig: - 57 75

# description: test service

 

ROOT_PATH=/opt/oee/

OEE_SYNC_PID=/opt/oee/oee-sync-tool.pid

start() {

 

     if [ -f /opt/oee/oee-sync-tool.pid ];then

                    SPID=`cat /opt/oee/oee-sync-tool.pid`

                      if [ "$SPID" != "" ];then

                         echo "oee-sync-tool is running.....please check again!"

                         echo  $OEE_SYNC_PID

                      else

                        echo -n $"Starting $prog: "

                        nohup java -jar /opt/oee/oee-sync-tool-0.1.jar >/dev/null 2>&1 & new_agent_pid=$!

                        echo "$new_agent_pid" > $OEE_SYNC_PID

 

                      fi

     else

         echo -n $"Starting $prog: "

         nohup java -jar /opt/oee/oee-sync-tool-0.1.jar >/dev/null 2>&1 & new_agent_pid=$!

         echo "$new_agent_pid" > $OEE_SYNC_PID

     fi

 

 

 

 

}

 

stop() {

 

     if [ -f /opt/oee/oee-sync-tool.pid ];then  

                    SPID=`cat /opt/oee/oee-sync-tool.pid`  

                      if [ "$SPID" != "" ];then  

                         kill -9  $SPID  

  

                         echo  > $OEE_SYNC_PID  

                         echo "stop success"  

                      fi  

     fi          

}

 

  

CheckProcessStata()  

{  

    CPS_PID=$1  

    if [ "$CPS_PID" != "" ] ;then  

        CPS_PIDLIST=`ps -ef|grep $CPS_PID|grep -v grep|awk -F" " '{print $2}'`  

    else  

        CPS_PIDLIST=`ps -ef|grep "$CPS_PNAME"|grep -v grep|awk -F" " '{print $2}'`  

    fi  

  

    for CPS_i in `echo $CPS_PIDLIST`  

    do  

        if [ "$CPS_PID" = "" ] ;then  

            CPS_i1="$CPS_PID"  

        else  

            CPS_i1="$CPS_i"  

        fi  

  

        if [ "$CPS_i1" = "$CPS_PID" ] ;then  

            #kill -s 0 $CPS_i  

            kill -0 $CPS_i >/dev/null 2>&1  

            if [ $? != 0 ] ;then  

                echo "[`date`] MC-10500: Process $i have Dead"   

                kill -9 $CPS_i >/dev/null 2>&1  

                 

                return 1  

            else  

                #echo "[`date`] MC-10501: Process is alive"   

                return 0  

            fi  

        fi  

    done  

    echo "[`date`] MC-10502: Process $CPS_i is not exists"   

    return 1  

}  

  

status()  

{  

  SPID=`cat /opt/oee/oee-sync-tool.pid`   

  CheckProcessStata $SPID >/dev/null  

                             if [ $? != 0 ];then  

                                echo "oee-sync-tool:{$SPID}  Stopped ...."  

                              else  

                                echo "oee-sync-tool:{$SPID} Running Normal."  

                             fi  

  

}  

   

restart()  

{  

    echo "stoping ... "  

    stop  

    echo "staring ..."  

    start  

}  

   

case "$1" in  

    start)  

        start  

        ;;  

    stop)  

        stop  

        ;;  

    status)  

         status  

        ;;  

    restart)  

        restart  

        ;;  

    *)  

        echo $"Usage: $0 {start|stop|restart}"  

        RETVAL=1  

 

echo $"Usage: $0 {start|stop|restart|force-reload}"

exit 2

esac

 

(2)使用命令chkconfig设置开机启动

        

        chkconfig --add oee-sync-tool


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI