一、上传Jar文件
1、将编译好的jar文件上传到服务8100和8200各一份。
8100和8200为目录名称可更改,示例如下:
/data/8100/tuha-ar-8100.jar
/data/8200/tuha-ar-8200.jar
二、项目中添生产环境配置文件
1、例如application-prod8100.yml和application-prod8200.yml 除端口不同,其余配置全部相同。
三、Nginx中添加配置
1、添加后台服务端口
# 轮训策略
upstream thygkq2 {
server 127.0.0.1:8100;
server 127.0.0.1:8200;
}
# 前端服务
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /admin-api {
proxy_pass http://thygkq2;
}
}
# 移动外网端口
server {
listen 9006;
server_name localhost;
location / {
proxy_pass http://thygkq2;
}
}
四、添加启动脚本 ht.sh
#!/bin/sh
# ./ht.sh start 启动 stop 停止 restart 重启 status 状态
AppName=tuha-ar-8100.jar
ENV=prod8100
# JVM参数
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=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 $AppName $JVM_OPTS --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo "Start $AppName success, environment is $ENV ..."
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
注意
1、AppName需改为jar包名称。
2、ENV修改指定的启动环境。
3、上传ht.sh到jar同级目录下(第一步jar目录)。
4、./ht.sh start 启动 ./ht.sh stop 停止 ./ht.sh restart 重启 ./ht.sh status 状态。