133 lines
3.9 KiB
Bash
133 lines
3.9 KiB
Bash
#!/bin/bash
|
||
# ==============================================================================
|
||
# 铁路培训实训教学平台 - 全自动平滑部署与自检脚本
|
||
# 路径规范:/data2/wzb/deploy
|
||
# ==============================================================================
|
||
|
||
DEPLOY_ROOT="/data2/wzb/deploy"
|
||
BACKEND_DIR="$DEPLOY_ROOT/backend"
|
||
FRONTEND_DIR="$DEPLOY_ROOT/frontend"
|
||
LOG_DIR="$DEPLOY_ROOT/logs"
|
||
PID_FILE="$BACKEND_DIR/app.pid"
|
||
JAR_PATH="$BACKEND_DIR/ruoyi-admin.jar"
|
||
LOG_PATH="$LOG_DIR/backend.log"
|
||
JAVA_BIN="/data2/wzb/tools/jdk8/bin/java"
|
||
|
||
# 服务运行端口与数据库参数
|
||
SERVER_PORT=8092
|
||
DB_URL="jdbc:kingbase8://192.168.139.218:4321/training?currentSchema=public,sys_catalog&useUnicode=true&characterEncoding=utf8"
|
||
|
||
mkdir -p "$BACKEND_DIR" "$FRONTEND_DIR" "$LOG_DIR"
|
||
|
||
echo "=== 1. 检查并停止当前用户的旧后端进程 ==="
|
||
TARGET_PID=""
|
||
if [ -f "$PID_FILE" ]; then
|
||
SAVED_PID=$(cat "$PID_FILE")
|
||
if kill -0 "$SAVED_PID" 2>/dev/null; then
|
||
TARGET_PID="$SAVED_PID"
|
||
fi
|
||
fi
|
||
|
||
if [ -z "$TARGET_PID" ]; then
|
||
TARGET_PID=$(pgrep -u $(whoami) -f "$JAR_PATH" | head -n 1)
|
||
fi
|
||
|
||
if [ -n "$TARGET_PID" ]; then
|
||
echo "正在停止旧进程 PID: $TARGET_PID ..."
|
||
kill -15 $TARGET_PID 2>/dev/null
|
||
sleep 3
|
||
if kill -0 $TARGET_PID 2>/dev/null; then
|
||
echo "强制终止 PID: $TARGET_PID"
|
||
kill -9 $TARGET_PID 2>/dev/null
|
||
fi
|
||
fi
|
||
rm -f "$PID_FILE"
|
||
|
||
echo "=== 2. 启动新的后端服务(端口: $SERVER_PORT) ==="
|
||
if [ ! -f "$JAR_PATH" ]; then
|
||
echo "错误:未找到 $JAR_PATH"
|
||
exit 1
|
||
fi
|
||
|
||
nohup $JAVA_BIN -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m \
|
||
-Dfile.encoding=UTF-8 \
|
||
-jar "$JAR_PATH" \
|
||
--server.port=$SERVER_PORT \
|
||
--spring.datasource.druid.master.url="$DB_URL" > "$LOG_PATH" 2>&1 &
|
||
|
||
NEW_PID=$!
|
||
echo $NEW_PID > "$PID_FILE"
|
||
echo "后端已在后台启动,PID: $NEW_PID"
|
||
echo "实时日志: $LOG_PATH"
|
||
|
||
echo "等待服务启动与健康自检..."
|
||
for i in {1..35}; do
|
||
RES=$(curl -s "http://127.0.0.1:${SERVER_PORT}/captchaImage" 2>/dev/null)
|
||
if echo "$RES" | grep -q '"code":200'; then
|
||
echo "后端健康检查通过!验证码接口返回正常!服务已在端口 $SERVER_PORT 成功运行!"
|
||
break
|
||
fi
|
||
sleep 2
|
||
done
|
||
|
||
echo "=== 3. 检查并启动/重载前端 Web 服务 (Nginx 8082) ==="
|
||
NGINX_DIR="$DEPLOY_ROOT/nginx"
|
||
NGINX_CONF="$NGINX_DIR/nginx.conf"
|
||
mkdir -p "$NGINX_DIR/temp"
|
||
|
||
if [ ! -f "$NGINX_CONF" ]; then
|
||
cat << 'NGINX_EOF' > "$NGINX_CONF"
|
||
pid /data2/wzb/deploy/nginx/nginx.pid;
|
||
error_log /data2/wzb/deploy/nginx/error.log;
|
||
|
||
events {
|
||
worker_connections 1024;
|
||
}
|
||
|
||
http {
|
||
include /etc/nginx/mime.types;
|
||
default_type application/octet-stream;
|
||
|
||
client_body_temp_path /data2/wzb/deploy/nginx/temp/client_body;
|
||
proxy_temp_path /data2/wzb/deploy/nginx/temp/proxy;
|
||
fastcgi_temp_path /data2/wzb/deploy/nginx/temp/fastcgi;
|
||
uwsgi_temp_path /data2/wzb/deploy/nginx/temp/uwsgi;
|
||
scgi_temp_path /data2/wzb/deploy/nginx/temp/scgi;
|
||
|
||
access_log /data2/wzb/deploy/nginx/access.log;
|
||
|
||
sendfile on;
|
||
keepalive_timeout 65;
|
||
|
||
server {
|
||
listen 8082;
|
||
server_name localhost;
|
||
|
||
location / {
|
||
root /data2/wzb/deploy/frontend;
|
||
try_files $uri $uri/ /index.html;
|
||
index index.html;
|
||
}
|
||
|
||
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://127.0.0.1:8092/;
|
||
}
|
||
}
|
||
}
|
||
NGINX_EOF
|
||
fi
|
||
|
||
if pgrep -u $(whoami) -f "nginx: master" >/dev/null; then
|
||
echo "平滑重载前端 Nginx 服务..."
|
||
/usr/sbin/nginx -c "$NGINX_CONF" -s reload
|
||
else
|
||
echo "启动前端 Nginx 服务..."
|
||
/usr/sbin/nginx -c "$NGINX_CONF"
|
||
fi
|
||
|
||
echo "=== 部署全部就绪!前端访问地址: http://192.168.139.230:8082 ==="
|