springboot 打包(jar)部署在 Linux 環境

1、打包

項目作分支,分爲開發分支develop, 生產分支master。
filephp

在打包時,切換到生產分支,由於生產分支裏邊的配置和開發分支的配置不一樣,開發分支的數據庫、日誌路徑都爲本地的。css

使用打包命令來打包,進入項目目錄:html

source ~/.bash_profile
 cd /Users/kaiyiwang/Code/java/quantsmart/ruoyi/

打包:前端

mvn clean package -Dmaven.test.skip=true

打包結果:vue

(base) ➜  ruoyi git:(master) ✗ mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building ruoyi 2.2.0
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ ruoyi ---
[INFO] Deleting /Users/kaiyiwang/Code/java/quantsmart/ruoyi/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ ruoyi ---
...
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ ruoyi ---
[INFO] Building jar: /Users/kaiyiwang/Code/java/quantsmart/ruoyi/target/ruoyi.jar
[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) @ ruoyi ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 40.379 s
[INFO] Finished at: 2020-05-18T22:16:22+08:00
[INFO] Final Memory: 47M/297M
[INFO] ------------------------------------------------------------------------

2、部署

打完包就要部署了。由於spring boot有內置tomcat容器,若是系統有安裝Tomcat則需停掉,這點比較方便,省去了tomcat的部署。咱們直接把jar包扔到linux上。這裏你能夠經過FTP工具,也可使用下面這個命令行的小工具,先安裝,咱們這裏使用FileZilla進行上傳。java

上傳的目錄:linux

/var/www/web/

start.sh文件nginx

#!/bin/bash

AppName=ruoyi.jar

#JVM參數
JVM_OPTS="-Dname=$AppName  -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps  -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
APP_HOME=`pwd`
LOG_PATH=$APP_HOME/logs/$AppName.log

if [ "$1" = "" ];
then
    echo -e "\033[0;31m 未輸入操做名 \033[0m  \033[0;34m {start|stop|restart|status} \033[0m"
    exit 1
fi

if [ "$AppName" = "" ];
then
    echo -e "\033[0;31m 未輸入應用名 \033[0m"
    exit 1
fi

function start()
{
    PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`

    if [ x"$PID" != x"" ]; then
        echo "$AppName is running..."
    else
        nohup java -jar  $JVM_OPTS target/$AppName > /dev/null 2>&1 &
        echo "Start $AppName success..."
    fi
}

function stop()
{
    echo "Stop $AppName"
    
    PID=""
    query(){
        PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
    }

    query
    if [ x"$PID" != x"" ]; then
        kill -TERM $PID
        echo "$AppName (pid:$PID) exiting..."
        while [ x"$PID" != x"" ]
        do
            sleep 1
            query
        done
        echo "$AppName exited."
    else
        echo "$AppName already stopped."
    fi
}

function restart()
{
    stop
    sleep 2
    start
}

function status()
{
    PID=`ps -ef |grep java|grep $AppName|grep -v grep|wc -l`
    if [ $PID != 0 ];then
        echo "$AppName is running..."
    else
        echo "$AppName is not running..."
    fi
}

case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    *)

esac

另外一版本:start_init.shgit

#!/bin/sh
JAR_NAME=ruoyi.jar

tpid=`ps -ef|grep $JAR_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
fi
sleep 5
tpid=`ps -ef|grep $JAR_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi

tpid=`ps -ef|grep $JAR_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
        echo 'App is running.'
else
        echo 'App is NOT running.'
fi

rm -f tpid
nohup java -jar ./$JAR_NAME --spring.profiles.active=test >/dev/null 2>&1 &
echo $! > tpid
echo 'Start Success!'

這個腳本的做用就是經過jar命令來執行jar包,前面是先經過grep命令看是否已有jar包在跑,有的話就殺掉再拉起,沒有就直接跑。注意上面的JAR_NAME須要根據你的jar包名稱賦值。另外最重要的一行就是經過nohup命令起一個後臺線程跑該jar,並把生成的nohup.out指向一個黑洞當垃圾扔掉。對了,保存好腳本後還得給這個腳本加權限:web

chmod +x ry.sh

啓動:

[root@css web]# ./start_init.sh                                                                        
Stop Success!
App is NOT running.
Start Success!

能夠直接使用命令啓動,若是報錯好排查,啓動沒問題以後能夠再使用shell啓動。

cd /var/www/web
java -jar ./ruoyi.jar
#./start_init.sh

咱們能夠看到已經啓動成功,若是啓動不成功,請查看具體報錯信息,如MySQL帳號權限設置是否OK,Redis是否有開啓等。

(♥◠‿◠)ノ゙  啓動成功   ლ(´ڡ`ლ)゙  
 .-------.       ____     __        
 |  _ _   \      \   \   /  /    
 | ( ' )  |       \  _. /  '       
 |(_ o _) /        _( )_ .'         
 | (_,_).' __  ___(_ o _)'          
 |  |\ \  |  ||   |(_,_)'         
 |  | \ `'   /|   `-'  /           
 |  |  \    /  \      /           
 ''-'   `'-'    `-..-'              
01:57:41.471 [Quartz Scheduler [RuoyiScheduler]] INFO  o.q.c.QuartzScheduler - [start,547] - Scheduler RuoyiScheduler
_$_cssbjqnffcsvic1589824655161 started.

3、前端部署

當項目開發完畢,只須要運行一行命令就能夠打包你的應用

# 打包正式環境
npm run build:prod

# 打包預發佈環境
npm run build:stage

構建打包成功以後,會在根目錄生成 dist 文件夾,裏面就是構建打包好的文件,一般是 .js 、.css、index.html 等靜態文件。

一般狀況下 dist 文件夾的靜態文件發佈到你的 nginx 或者靜態服務器便可,其中的 index.html 是後臺服務的入口頁面。

Nginx配置

cd /usr/local/nginx/conf
vim nginx.conf

nginx.conf

#user  nobody;
worker_processes  1;

error_log  /var/log/nginx_error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx_access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  /var/log/nginx_access.log  main;

        location / {
            root   /var/www/web/ui/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /prod-api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

修改完Nginx配置後重啓:

/usr/local/nginx/sbin/nginx -s reload # 重啓
/usr/local/nginx/sbin/nginx -s stop 或:nginx -s quit  # 中止

開啓redis:

cd /developer/redis-5.0.6/src
./redis-server

# 後臺執行
nohup /developer/redis-5.0.6/src/redis-server >/dev/null 2>&1 &

OK,至此,先後端項目都已經部署OK,咱們經過瀏覽器訪問一下在想項目。


相關文章:
springboot打包(jar)部署在linux環境上(idea,maven)
https://www.cnblogs.com/wuxun...
https://segmentfault.com/a/11...
https://vuepress.vuejs.org/

相關文章
相關標籤/搜索