springboot配置成linuxservice
最近,要把公司以前的一个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里加点料,不然做不了的.
然后用
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
网站标题:springboot配置成linuxservice
文章路径:http://scjbc.cn/article/gcjspo.html