1

一、打包

项目做分支,分为开发分支develop, 生产分支master。
file

在打包时,切换到生产分支,因为生产分支里边的配置和开发分支的配置不同,开发分支的数据库、日志路径都为本地的。

使用打包命令来打包,进入项目目录:

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

打包:

mvn clean package -Dmaven.test.skip=true

打包结果:

(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] ------------------------------------------------------------------------

二、部署

打完包就要部署了。因为spring boot有内置tomcat容器,如果系统有安装Tomcat则需停掉,这点比较方便,省去了tomcat的部署。我们直接把jar包扔到linux上。这里你可以通过FTP工具,也可以使用下面这个命令行的小工具,先安装,我们这里使用FileZilla进行上传。

上传的目录:

/var/www/web/

start.sh文件

#!/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.sh

#!/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 >/dev/null 2>&1 &
echo $! > tpid
echo 'Start Success!'

这个脚本的作用就是通过jar命令来执行jar包,前面是先通过grep命令看是否已有jar包在跑,有的话就杀掉再拉起,没有就直接跑。注意上面的JAR_NAME需要根据你的jar包名称赋值。另外最重要的一行就是通过nohup命令起一个后台线程跑该jar,并把生成的nohup.out指向一个黑洞当垃圾扔掉。对了,保存好脚本后还得给这个脚本加权限:

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.

三、前端部署

当项目开发完毕,只需要运行一行命令就可以打包你的应用

# 打包正式环境
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/


Corwien
6.3k 声望1.6k 粉丝

为者常成,行者常至。