cnblogs/dcrenl/centos 运行springboot 项目.html

61 lines
4.9 KiB
HTML
Raw Permalink Normal View History

2024-09-24 12:43:01 +08:00
<p>jar文件发布</p>
<p>准备工作:</p>
<p>发布在springboot项目中的pom.xml文件添加如下</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">build</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">plugins</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">plugin</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>org.springframework.boot<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>spring-boot-maven-plugin<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">configuration</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">executable</span><span style="color: #0000ff;">&gt;</span>true<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">executable</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">configuration</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">plugin</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">plugins</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">build</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>一、正常启动:</p>
<p>后台运行关闭远程后不会自动退出nohup java -jar demo-1.0.1.jar &gt;msg.log &amp;</p>
<p>后面msg.log为运行日志</p>
<p>查看8080端口是否已使用netstat -lanp|grep 8080</p>
<p>查看java进程&nbsp;ps -ef | grep java&nbsp;</p>
<p>停止java进程 kill -9&nbsp;进程ID</p>
<p>&nbsp;</p>
<p>二、添加软连接以service方式启动</p>
<p>打开ssh运行ln -sb 原文件 /etc/init.d/服务名</p>
<p>日志可到&nbsp;<span class="cnblogs_code">/var/log/服务名</span>&nbsp;查看</p>
<p>设置权限chmod 777&nbsp;服务名</p>
<p>服务启动停止查看状态命令service 服务名 start|stop|status|restart</p>
<p>使用自定义的.conf文件来变更默认配置,在jar包相同路径下创建一个.conf文件名称应该与.jar的名称相同</p>
<div class="cnblogs_code">
<pre>JAVA_HOME=<span style="color: #000000;">/usr/local/jdk
JAVA_OPTS</span>=<span style="color: #000000;">-Xmx1024M
LOG_FOLDER</span>=/data/logs/myapp</pre>
</div>
<p>&nbsp;</p>
<p>三、添加系统服务,开机自动启动</p>
<p>切换目录cd /ets/systemd/system</p>
<p>添加文件:服务名.service</p>
<p>文件内容如下:</p>
<div class="cnblogs_code">
<pre><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">Unit</span><span style="color: #800000; font-weight: bold;">]</span><span style="color: #000000;">
Description</span>=描述<span style="color: #000000;">
After</span>=<span style="color: #000000;">network.target
</span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">Service</span><span style="color: #800000; font-weight: bold;">]</span><span style="color: #000000;">
WorkingDirectory</span>=<span style="color: #000000;">/root/api
ExecStart</span>=/usr/bin/java <span style="color: #000000;">-jar /root/api/ROOT.jar
ExecStop</span>=<span style="color: #000000;">kill $MAINPID
Restart</span>=<span style="color: #000000;">always
</span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;">Install</span><span style="color: #800000; font-weight: bold;">]</span><span style="color: #000000;">
WantedBy</span>=multi-user.target</pre>
</div>
<p>如不知道java安装目录可运行which java</p>
<p>重新加载服务systemctl daemon-reload</p>
<p>运行服务systemctl&nbsp;start&nbsp;服务名</p>
<p>查看日志journalctl -u 服务名</p>
<p>查看、停止服务状态:&nbsp;&nbsp;<span class="cnblogs_code">systemctl stop 服务名</span>&nbsp;&nbsp;<span class="cnblogs_code">systemctl status 服务名</span>&nbsp;&nbsp;<span class="cnblogs_code">systemctl restart 服务名</span>&nbsp;</p>
<p><span><span>设置开机启动和禁止开机启动:&nbsp;<span class="cnblogs_code">systemctl enable 服务名</span>&nbsp;&nbsp;<span class="cnblogs_code">systemctl disable 服务名</span>&nbsp;</span></span></p>
<p>&nbsp;</p>