fundplatform/doc/部署运维文档.md

23 KiB
Raw Blame History

资金服务平台部署运维文档

版本: v1.0
更新日期: 2026-02-13
作者: zhangjf


一、本地开发环境搭建

1.1 环境要求

组件 版本要求 说明
JDK 21+ Java开发工具包
Maven 3.9+ 项目构建工具
MySQL 8.0+ 数据库
Redis 7.0+ 缓存服务
Nacos 3.0+ 服务注册与配置中心
Node.js 18+ 前端开发环境

1.2 安装步骤

1.2.1 安装 JDK 21

# 下载并解压 JDK 21
wget https://download.java.net/openjdk/jdk21/ri/openjdk-21+35_linux-x64_bin.tar.gz
tar -xzf openjdk-21+35_linux-x64_bin.tar.gz -C /opt/

# 配置环境变量
echo 'export JAVA_HOME=/opt/jdk-21' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

# 验证安装
java -version

1.2.2 安装 Maven 3.9

# 下载并解压 Maven
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
tar -xzf apache-maven-3.9.9-bin.tar.gz -C /opt/

# 配置环境变量
echo 'export MAVEN_HOME=/opt/apache-maven-3.9.9' >> ~/.bashrc
echo 'export PATH=$MAVEN_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

# 验证安装
mvn -version

1.2.3 安装 MySQL 8.0

# Ubuntu/Debian
sudo apt update
sudo apt install mysql-server-8.0

# 启动 MySQL
sudo systemctl start mysql
sudo systemctl enable mysql

# 配置 root 密码
sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'zjf@123456';"

# 创建数据库
mysql -u root -p'zjf@123456' -e "CREATE DATABASE fund_platform CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

1.2.4 安装 Redis

# Ubuntu/Debian
sudo apt update
sudo apt install redis-server

# 配置密码
sudo sed -i 's/# requirepass foobared/requirepass zjf@123456/' /etc/redis/redis.conf

# 启动 Redis
sudo systemctl restart redis
sudo systemctl enable redis

# 验证安装
redis-cli -a zjf@123456 ping

1.2.5 安装 Nacos 3.0

# 下载 Nacos
cd /opt
wget https://github.com/alibaba/nacos/releases/download/3.0.0/nacos-server-3.0.0.tar.gz
tar -xzf nacos-server-3.0.0.tar.gz

# 配置端口
cd nacos/conf
sed -i 's/server.port=8848/server.port=8048/' application.properties

# 启动 Nacos单机模式
cd /opt/nacos/bin
sh startup.sh -m standalone

# 访问控制台http://localhost:8048/nacos
# 默认账号密码nacos / nacos

1.2.6 安装 Node.js 18

# 使用 nvm 安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 18
nvm use 18

# 验证安装
node -v
npm -v

1.3 项目初始化

# 克隆项目
git clone <repository-url>
cd fundplatform

# 初始化数据库
mysql -u root -p'zjf@123456' < sql/fund_platform_schema.sql
mysql -u root -p'zjf@123456' < sql/fund_platform_init.sql

# 配置环境变量
cp .env.example .env
# 编辑 .env 文件配置数据库、Redis、Nacos 连接信息

1.4 启动服务

# 1. 确保 MySQL、Redis、Nacos 已启动

# 2. 启动后端服务(在各自服务目录执行)
cd fund-gateway
mvn spring-boot:run

cd fund-sys
mvn spring-boot:run

# 3. 启动前端Vue 3
cd fund-admin
npm install
npm run dev

# 4. 访问系统
# 管理后台http://localhost:5173
# 网关地址http://localhost:8080
# Nacos 控制台http://localhost:8048/nacos

二、测试环境部署

2.1 服务器配置

组件 配置 数量
应用服务器 4核8G 2台
数据库服务器 4核8G 1台
缓存服务器 2核4G 1台

2.2 部署架构

┌─────────────────────────────────────────────────────────────┐
│                         测试环境部署架构                       │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│   ┌──────────────┐                                          │
│   │   Nginx      │  ← 负载均衡 + 静态资源                    │
│   │  (80/443)    │                                          │
│   └──────┬───────┘                                          │
│          │                                                   │
│   ┌──────┴──────┐                                           │
│   │             │                                           │
│   ▼             ▼                                           │
│ ┌─────────┐  ┌─────────┐                                    │
│ │AppSrv-1 │  │AppSrv-2 │  ← 应用服务器Gateway + Services│
│ │:8080-8090│  │:8080-8090│                                    │
│ └────┬────┘  └────┬────┘                                    │
│      │            │                                         │
│      └─────┬──────┘                                         │
│            │                                                 │
│   ┌────────┴────────┐                                       │
│   │                 │                                       │
│   ▼                 ▼                                       │
│ ┌─────────┐     ┌─────────┐                                │
│ │  MySQL  │     │  Redis  │                                │
│ │ :3306   │     │ :6379   │                                │
│ └─────────┘     └─────────┘                                │
│                                                              │
│ ┌─────────┐                                                 │
│ │  Nacos  │  ← 服务注册与配置中心                            │
│ │ :8848   │                                                 │
│ └─────────┘                                                 │
│                                                              │
└─────────────────────────────────────────────────────────────┘

2.3 部署步骤

2.3.1 部署 Nacos

# 在服务器上安装 Nacos
cd /opt
wget https://github.com/alibaba/nacos/releases/download/3.0.0/nacos-server-3.0.0.tar.gz
tar -xzf nacos-server-3.0.0.tar.gz
cd nacos/conf

# 配置 MySQL 持久化
cat >> application.properties << EOF
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://mysql-server:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
db.user.0=nacos
db.password.0=nacos_password
EOF

# 启动 Nacos集群模式
cd /opt/nacos/bin
sh startup.sh

2.3.2 部署 MySQL

# 创建数据库和用户
mysql -u root -p << EOF
CREATE DATABASE IF NOT EXISTS fund_platform CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'fund_user'@'%' IDENTIFIED BY 'Fund@123456';
GRANT ALL PRIVILEGES ON fund_platform.* TO 'fund_user'@'%';
FLUSH PRIVILEGES;
EOF

# 导入表结构和数据
mysql -u fund_user -p'Fund@123456' fund_platform < fund_platform_schema.sql
mysql -u fund_user -p'Fund@123456' fund_platform < fund_platform_init.sql

2.3.3 部署 Redis

# 配置 Redis
sudo tee /etc/redis/redis.conf > /dev/null << EOF
bind 0.0.0.0
port 6379
requirepass Redis@123456
maxmemory 2gb
maxmemory-policy allkeys-lru
appendonly yes
EOF

# 启动 Redis
sudo systemctl restart redis

2.3.4 部署应用服务

# 1. 打包应用
cd fundplatform
mvn clean package -DskipTests

# 2. 上传 jar 包到服务器
scp fund-gateway/target/fund-gateway-1.0.0.jar user@app-server-1:/opt/fundplatform/
scp fund-sys/target/fund-sys-1.0.0.jar user@app-server-1:/opt/fundplatform/

# 3. 创建启动脚本
cat > /opt/fundplatform/start.sh << 'EOF'
#!/bin/bash
APP_NAME=$1
JAR_FILE=$2

nohup java -Xms512m -Xmx1024m \
  -Dspring.profiles.active=test \
  -Dfile.encoding=UTF-8 \
  -jar $JAR_FILE > logs/$APP_NAME.log 2>&1 &

echo "$APP_NAME started, PID: $!"
EOF

chmod +x /opt/fundplatform/start.sh

# 4. 启动服务
./start.sh gateway fund-gateway-1.0.0.jar
./start.sh sys fund-sys-1.0.0.jar

2.3.5 部署 Nginx

# 安装 Nginx
sudo apt install nginx

# 配置反向代理
sudo tee /etc/nginx/sites-available/fundplatform << 'EOF'
upstream gateway {
    server app-server-1:8080 weight=5;
    server app-server-2:8080 weight=5;
}

server {
    listen 80;
    server_name test.fundplatform.com;
    
    # 前端静态资源
    location / {
        root /opt/fundplatform/admin;
        try_files $uri $uri/ /index.html;
    }
    
    # API 代理
    location /api/ {
        proxy_pass http://gateway/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
EOF

sudo ln -s /etc/nginx/sites-available/fundplatform /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

三、生产环境部署

3.1 服务器配置

组件 配置 数量 说明
应用服务器 8核16G 3台 运行 Gateway 和微服务
数据库服务器 8核16G 2台 MySQL 主从
缓存服务器 4核8G 3台 Redis 集群
Nacos 服务器 4核8G 3台 Nacos 集群
Nginx 服务器 4核8G 2台 负载均衡

3.2 部署架构

┌─────────────────────────────────────────────────────────────────────────────┐
│                              生产环境部署架构                                 │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                              │
│   ┌──────────────┐              ┌──────────────┐                            │
│   │   Nginx-1    │◄────────────►│   Nginx-2    │  ← 负载均衡Keepalived   │
│   │  (VIP:80/443)│              │              │                            │
│   └──────┬───────┘              └──────┬───────┘                            │
│          │                              │                                   │
│          └──────────────┬───────────────┘                                   │
│                         │                                                   │
│   ┌─────────────────────┼─────────────────────┐                             │
│   │                     │                     │                             │
│   ▼                     ▼                     ▼                             │
│ ┌─────────┐         ┌─────────┐         ┌─────────┐                        │
│ │AppSrv-1 │         │AppSrv-2 │         │AppSrv-3 │  ← 应用服务集群          │
│ │:8080-8090│         │:8080-8090│         │:8080-8090│                        │
│ └────┬────┘         └────┬────┘         └────┬────┘                        │
│      │                   │                   │                             │
│      └───────────────────┼───────────────────┘                             │
│                          │                                                  │
│   ┌──────────────────────┼──────────────────────┐                          │
│   │                      │                      │                          │
│   ▼                      ▼                      ▼                          │
│ ┌─────────┐          ┌─────────┐          ┌─────────┐                     │
│ │  MySQL  │◄────────►│  MySQL  │          │  Redis  │  ← 数据层            │
│ │ Master  │   主从   │  Slave  │          │ Cluster │                     │
│ │ :3306   │   复制   │  :3306  │          │ :6379   │                     │
│ └─────────┘          └─────────┘          └────┬────┘                     │
│                                                 │                          │
│   ┌─────────────────────────────────────────────┘                          │
│   │                                                                         │
│   ▼                                                                         │
│ ┌─────────┐  ┌─────────┐  ┌─────────┐                                     │
│ │ Nacos-1 │◄►│ Nacos-2 │◄►│ Nacos-3 │  ← Nacos 集群(外置 MySQL          │
│ │ :8848   │  │ :8848   │  │ :8848   │                                     │
│ └─────────┘  └─────────┘  └─────────┘                                     │
│                                                                              │
└─────────────────────────────────────────────────────────────────────────────┘

3.3 高可用配置

3.3.1 MySQL 主从配置

# 主库配置
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=fund_platform

# 从库配置
[mysqld]
server-id=2
relay-log=mysql-relay-bin
replicate-do-db=fund_platform

# 创建复制用户
CREATE USER 'repl'@'%' IDENTIFIED BY 'Repl@123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

# 从库执行
CHANGE MASTER TO
  MASTER_HOST='master-host',
  MASTER_USER='repl',
  MASTER_PASSWORD='Repl@123456',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=154;
START SLAVE;

3.3.2 Redis 集群配置

# 创建 6 个节点3主3从
redis-server --port 7001 --cluster-enabled yes --cluster-config-file nodes-7001.conf
redis-server --port 7002 --cluster-enabled yes --cluster-config-file nodes-7002.conf
redis-server --port 7003 --cluster-enabled yes --cluster-config-file nodes-7003.conf
redis-server --port 7004 --cluster-enabled yes --cluster-config-file nodes-7004.conf
redis-server --port 7005 --cluster-enabled yes --cluster-config-file nodes-7005.conf
redis-server --port 7006 --cluster-enabled yes --cluster-config-file nodes-7006.conf

# 创建集群
redis-cli --cluster create \
  node1:7001 node1:7002 node2:7003 node2:7004 node3:7005 node3:7006 \
  --cluster-replicas 1

3.3.3 Nacos 集群配置

# 配置 cluster.conf
cat > /opt/nacos/conf/cluster.conf << EOF
nacos-1:8848
nacos-2:8848
nacos-3:8848
EOF

# 配置 MySQL 持久化
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://mysql-cluster:3306/nacos?...
db.user.0=nacos
db.password.0=nacos_password

# 启动 Nacos
sh startup.sh

3.3.4 Nginx + Keepalived

# 安装 Keepalived
sudo apt install keepalived

# 配置 Keepalived主节点
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
    router_id NGINX_MASTER
}

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    virtual_ipaddress {
        192.168.1.100/24
    }
    
    track_script {
        check_nginx
    }
}
EOF

# 健康检查脚本
cat > /etc/keepalived/check_nginx.sh << 'EOF'
#!/bin/bash
if ! pgrep nginx > /dev/null; then
    systemctl start nginx
    sleep 2
    if ! pgrep nginx > /dev/null; then
        systemctl stop keepalived
    fi
fi
EOF

chmod +x /etc/keepalived/check_nginx.sh

四、运维手册

4.1 监控体系

4.1.1 应用监控Prometheus + Grafana

# prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'fund-gateway'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['app-server-1:8080', 'app-server-2:8080', 'app-server-3:8080']
  
  - job_name: 'fund-sys'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['app-server-1:8081', 'app-server-2:8081', 'app-server-3:8081']

4.1.2 关键监控指标

指标 告警阈值 说明
JVM 内存使用率 > 80% JVM 堆内存使用百分比
CPU 使用率 > 80% 服务器 CPU 使用率
磁盘使用率 > 85% 磁盘空间使用百分比
接口响应时间 > 2s API 平均响应时间
错误率 > 5% HTTP 5xx 错误率
MySQL 连接数 > 80% 数据库连接数使用率
Redis 内存使用率 > 80% Redis 内存使用百分比

4.2 日志管理

4.2.1 日志配置

<!-- logback-spring.xml -->
<configuration>
    <!-- 控制台输出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{traceId}] [%X{tid}] [%X{uid}] [%X{uname}] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <!-- 文件输出 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/fundplatform/app.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/var/log/fundplatform/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <maxFileSize>100MB</maxFileSize>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{traceId}] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

4.2.2 ELK 日志收集

# filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/fundplatform/*.log
  fields:
    service: fundplatform
  multiline.pattern: '^\d{4}-\d{2}-\d{2}'
  multiline.negate: true
  multiline.match: after

output.elasticsearch:
  hosts: ["elasticsearch:9200"]
  index: "fundplatform-%{+yyyy.MM.dd}"

4.3 备份策略

4.3.1 数据库备份

#!/bin/bash
# 数据库备份脚本

BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y%m%d_%H%M%S)
DB_NAME="fund_platform"
DB_USER="backup_user"
DB_PASS="Backup@123456"

# 全量备份
mysqldump -u$DB_USER -p$DB_PASS --single-transaction --routines --triggers $DB_NAME > $BACKUP_DIR/${DB_NAME}_${DATE}.sql

# 压缩
gzip $BACKUP_DIR/${DB_NAME}_${DATE}.sql

# 删除 7 天前的备份
find $BACKUP_DIR -name "${DB_NAME}_*.sql.gz" -mtime +7 -delete

# 上传到对象存储
aws s3 cp $BACKUP_DIR/${DB_NAME}_${DATE}.sql.gz s3://fundplatform-backup/mysql/

4.3.2 备份计划

备份类型 频率 保留时间
全量备份 每天 02:00 30 天
增量备份 每小时 7 天
二进制日志 实时 7 天

4.4 故障处理

4.4.1 常见故障及处理

故障现象 可能原因 处理方案
服务无法启动 端口冲突 检查端口占用,修改配置
数据库连接失败 连接池耗尽 增加连接池大小,检查慢查询
内存溢出 内存泄漏 分析堆转储,重启服务
接口超时 下游服务慢 检查依赖服务,增加熔断
Redis 连接失败 连接数超限 增加连接池,检查连接泄露

4.4.2 应急处理流程

1. 故障发现
   ↓
2. 故障确认(查看监控、日志)
   ↓
3. 故障定级P0-核心功能不可用P1-部分功能受影响P2-轻微影响)
   ↓
4. 启动应急预案
   - P0立即回滚或重启
   - P1限流降级保留核心功能
   - P2记录问题计划修复
   ↓
5. 故障恢复验证
   ↓
6. 故障复盘

4.5 性能优化

4.5.1 JVM 调优

# 生产环境 JVM 参数
java -server \
  -Xms4g -Xmx4g \
  -XX:MetaspaceSize=256m \
  -XX:MaxMetaspaceSize=512m \
  -XX:+UseG1GC \
  -XX:MaxGCPauseMillis=200 \
  -XX:+HeapDumpOnOutOfMemoryError \
  -XX:HeapDumpPath=/var/log/fundplatform/heapdump.hprof \
  -jar app.jar

4.5.2 MySQL 调优

# my.cnf
[mysqld]
# 连接配置
max_connections = 500
max_connect_errors = 1000
wait_timeout = 600
interactive_timeout = 600

# InnoDB 配置
innodb_buffer_pool_size = 4G
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT

# 查询缓存
query_cache_type = 1
query_cache_size = 256M

# 慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2

4.5.3 Redis 调优

# redis.conf
maxmemory 4gb
maxmemory-policy allkeys-lru
tcp-keepalive 300
timeout 0

五、附录

5.1 常用命令

# 查看服务状态
systemctl status mysql
systemctl status redis
systemctl status nginx

# 查看日志
tail -f /var/log/fundplatform/app.log
tail -f /var/log/nginx/access.log

# 查看进程
ps -ef | grep java
netstat -tlnp | grep 8080

# 数据库操作
mysql -u root -p
redis-cli -a password

# Nacos 管理
curl http://localhost:8848/nacos/v1/ns/instance/list?serviceName=fund-sys

5.2 端口清单

服务 端口 说明
Nginx 80/443 Web 服务
Gateway 8080 API 网关
fund-sys 8081 系统服务
fund-cust 8082 客户中心
fund-proj 8083 项目中心
fund-req 8084 需求中心
fund-exp 8085 支出中心
fund-receipt 8086 收款中心
MySQL 3306 数据库
Redis 6379 缓存
Nacos 8848 服务注册
Nacos 控制台 8048 配置中心

文档结束