若依框架下项目,通过Nginx进行双服务部署
双服务部署可以避免后续系统更新时,系统停机造成数据缺失。
nginx前端设置:
upstream xxl {
#双服务后端启动路径
server 127.0.0.1:9081;
server 127.0.0.1:9082;
}
#提供外部调用后端接口,不需要登录验证
server {
listen 8080;
server_name 10.0.0.244;
charset utf-8;
gzip_static on;
location / {
proxy_set_header Access-Control-Allow-Origin *;
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://xxl/;
}
}
#前端调用
server {
listen 8081;
server_name 10.0.0.244;
charset utf-8;
gzip_static on;
location / {
root /data/program/program-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/{
proxy_set_header Access-Control-Allow-Origin *;
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://xxl/;
# 设置 NGINX 的代理连接超时为 600 秒
proxy_connect_timeout 600;
# 设置 NGINX 的代理发送超时为 600 秒
proxy_send_timeout 600;
# 设置 NGINX 的代理读取超时为 600 秒
proxy_read_timeout 600;
# 设置 NGINX 的发送超时为 600 秒
send_timeout 600;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
其中一个服务启动设置,另一个设置基本相同,需要修改9081为9082,
start.sh文件内容如下:
#!/bin/sh
# ./ry.sh start 启动 stop 停止 restart 重启 status 状态
AppName=program-admin.jar
# JVM参数
JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms8g -Xmx8g -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m -XX:-OmitStackTraceInFastThrow -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 "port=9081 $AppName"|grep -v grep|awk '{print $2}'`
if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
nohup java $JVM_OPTS -jar -Dspring.profiles.active=prod -Dserver.port=9081 $AppName > /dev/null 2>&1 &
echo "Start $AppName success..."
fi
}
function stop()
{
echo "Stop $AppName"
PID=""
query(){
PID=`ps -ef |grep java|grep "port=9081 $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 "port=9081 $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
linux可以通过shell中输入“sh start.sh start”进行启动服务,
“sh start.sh stop”进行停止服务,
“sh start.sh restart”进行重启服务。
另外系统其他服务启动设置,例如xxljob的启动,
启动文件xxl-job-start.sh如下:
#!/bin/sh
nohup java -Dspring.profiles.active=prod -jar xxl-job-admin.jar > /dev/null 2>&1 &