linux上部署spring-boot项目

sh脚本运行


下面几个脚本仅供参考,请根据自己需要做调整
start.sh

#!/bin/sh

rm -f tpid

APP_NAME=fm-eureka-client-1.0-SNAPSHOT
APP_JAR=$APP_NAME".jar"

##nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件。这里指定输出文件在为./fm-eureka-client-1.0-SNAPSHOT.log
nohup java -jar $APP_JAR > $APP_NAME".log" 2>&1 &

echo $! > $APP_NAME".tpid"

echo $APP_NAME Start Success!

stop.sh

#!/bin/sh
APP_NAME=fm-eureka-client-1.0-SNAPSHOT

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Stopping' $APP_NAME '...'
    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' $APP_NAME 'Process!'
    kill -9 $tpid
else
    echo $APP_NAME 'Stoped Success!'
fi

kill.sh

#!/bin/sh
APP_NAME=fm-eureka-client-1.0-SNAPSHOT

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 $APP_NAME 'is not running!'
fi

授予执行权限

chmod +x start.sh
chmod +x stop.sh
chmod +x kill.sh

只要修改变量 $APP_NAME 就可以用这种方式运行jar项目




使用Linux服务的方式运行


  1. 在项目pom.xml中配置插件

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

    需要注意一下:<executable>true<executable>

  2. 打包jar
    正常使用 mvn clean package -Dmaven.test.skip=true 将工程打成jar包

  3. 部署
    假设部署路径为 /var/apps/fm-eureka-client.jar ,使用命令做一个软连接到 /etc/init.d 目录,命令:

    ln -s /var/apps/fm-eureka-client.jar /etc/init.d/fm-eureka-client

    /etc/init.d/fm-eureka-client中的fm-eureka-client就是服务名。
    另删除软链的命令是:

    rm /etc/init.d/fm-eureka-client
  4. 授予jar文件可执行权限
    命令:

    chmod +x /var/apps/fm-eureka-client.jar
  5. 启动服务
    接下来,就可以使用 service fm-eureka-client start|stop|restart|status 来对应用进行启停了。
    执行命令后将得到形如 Started|Stopped [PID] 的结果反馈。
    默认PID文件路径:/var/run/{servicename}/{servicename}.pid
    默认服务日志文件路径:/var/log/{servicename}.log(可以通过下面.conf 的方式修改LOG_FOLDER)

  6. 自定义.conf文件来变更默认配置
    在jar包相同路径下创建一个.conf文件,名称应该与.jar的名称相同,如fm-eureka-client.conf(如果我们打包的文jar文件为 fm-eureka-client-1.0-SNAPSHOT.jar 那么这里的conf文件也应该是 fm-eureka-client-1.0-SNAPSHOT.conf),其内容配置可以如下:

    JAVA_HOME=/usr/local/jdk
    JAVA_OPTS=-Xmx1024M
    LOG_FOLDER=/data/logs/myapp

    注:LOG_FOLDER 对应的文件夹目录要必须存在,如果目录不存在,服务并不会自从创建目录。


- - - - - - - - - - - 分 - - - - - - -  割 - - - - - - -  线 - - - - - - - - - - - - - -

如果你是CentOS 7或红帽7以上,你还可以用下面的方法处理,为什么要用这样的方法(请自行研究),这里直接提供结果
编辑服务文件 vim /usr/lib/systemd/system/fm-eureka-client.service

[Unit]
[Unit]
Description=test

[Service]
WorkingDirectory=/servers/eureka_client
PrivateTmp=true
Restart=always
Type=simple
ExecStart=/usr/bin/java -jar /servers/eureka_client/fm-eureka-client-1.0-SNAPSHOT.jar
ExecStop=/usr/bin/kill -15  $MAINPID

[Install]
WantedBy=multi-user.target

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

sudo systemctl daemon-reload
sudo systemctl enable fm-eureka-client.service

启动服务

systemctl start fm-eureka-client

查看服务状态

systemctl status eureka-client.service

显示如下:

eureka-client.service - test
   Loaded: loaded (/usr/lib/systemd/system/eureka-client.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-10-25 21:32:24 CST; 16min ago
 Main PID: 17080 (java)
   Memory: 478.0M
   CGroup: /system.slice/eureka-client.service
           └─17080 /usr/bin/java -jar /servers/eureka_client/fm-eureka-client-1.0-SNAPSHOT.jar

Oct 25 21:47:03 localhost.localdomain java[17080]: gen usersig times:1
Oct 25 21:47:05 localhost.localdomain java[17080]: load times:0
Oct 25 21:47:05 localhost.localdomain java[17080]: gen usersig times:0
Oct 25 21:47:07 localhost.localdomain java[17080]: load times:0
Oct 25 21:47:07 localhost.localdomain java[17080]: gen usersig times:1
Oct 25 21:47:28 localhost.localdomain java[17080]: 2017-10-25 21:47:28.647  INFO 17080 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka...figuration
Oct 25 21:47:43 localhost.localdomain java[17080]: load times:0
Oct 25 21:47:43 localhost.localdomain java[17080]: gen usersig times:0
Oct 25 21:47:46 localhost.localdomain java[17080]: load times:0
Oct 25 21:47:46 localhost.localdomain java[17080]: gen usersig times:0
Warning: eureka-client.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Hint: Some lines were ellipsized, use -l to show in full.

查看日志(实时):

journalctl _PID=17080 -f
//或
journalctl -u eureka-client.service -f

使用Linux 7 以后服务新的启动方式,相关命令

//启动
systemctl start myapp
//停止
systemctl stop myapp
//重启
systemctl restart myapp
//查看日志
journalctl -u myapp
//关于更多 systemctl 命令的使用方法,度娘有很多。

转载:http://blog.csdn.net/catoop/article/details/50588851
journalctl 资料:http://blog.csdn.net/qq_31863071/article/details/52047593

猜你喜欢

转载自blog.csdn.net/mr_rain/article/details/78345822