1. 程式人生 > >spring boot 配置成 linux service

spring boot 配置成 linux service

最近,要把公司以前的一個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>
	 <plugins>
 		<plugin>
		  <groupId>org.apache.maven.plugins</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>
		
	  </plugins>
    </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