部署文档
| 版本号 | 修改 | 修改时间 | 备注 |
|---|---|---|---|
| v1.0.0 | 王小琱 | 2024.12.20 | 首版 |
1. 项目简介
exchange-statistical-service-backend 是一个基于 Go 语言开发的微服务,主要用于交易所统计报表、财务结算、用户资金等多维度数据的自动化统计与归档。系统支持 MySQL 数据库,集成 S3 存储,适用于金融、交易所等对数据统计有高要求的业务场景。
2. 环境要求
- Go 版本:1.21 及以上
- Docker:20.10 及以上
- 数据库:MySQL 8.x(或兼容版本)
- S3:兼容 AWS S3 的对象存储
3. 部署方式
3.1 本地部署
3.1.1 下载代码
bash
git clone https://gitlab.atom8.io/hkbitex/exchange-statistical-service-backend.git
cd exchange-statistical-service-backend3.1.2 安装依赖
bash
# 安装 protoc 工具
make init
# 更新依赖
make tidy3.1.3 编译代码
bash
make build3.1.4 配置说明
| 环境变量 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| APP_LOG_LEVEL | string | 可选,默认info | 日志级别(trace、debug、info、warn、error、fatal) |
| SERVER_HOST | string | 必填 | 服务监听地址(如 0.0.0.0:8080) |
| METRICS_SWITCH | string | 必填 | metrics 开关,on:启用,off:关闭 |
| METRICS_HOST | string | 必填 | metrics 服务监听地址(如 0.0.0.0:18080) |
| MYSQL_READ | string | 必填 | MySQL 读库 DSN,例如:user:password@tcp(host:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local |
| MYSQL_WRITE | string | 必填 | MySQL 写库 DSN(同上) |
| REDIS_URI | string | 必填 | Redis 连接地址 |
| REDIS_PD | string | 必填 | Redis 密码 |
| REDIS_DB | string | 必填 | Redis 数据库编号 |
| S3_REGION | string | 必填 | S3 区域 |
| S3_BUCKET | string | 必填 | S3 存储桶名称 |
| S3_ACCESS_KEY_ID | string | 必填 | S3 Access Key |
| S3_ACCESS_SECRET | string | 必填 | S3 Secret Key |
| EVENTUS_S3_REGION | string | 必填 | Eventus S3 区域 |
| EVENTUS_S3_ACCESS_KEY_ID | string | 必填 | Eventus S3 Access Key |
| EVENTUS_S3_ACCESS_SECRET | string | 必填 | Eventus S3 Secret Key |
3.1.5 启动服务
创建 Systemd 文件 /etc/systemd/system/statistical.service:
bash
[Unit]
Description=statistical-service
After=network.target
[Service]
WorkingDirectory=/app/statistical
Type=simple
User=root
ExecStart=/app/statistical/statistical
StandardOutput=append:/app/statistical/logs/output.log
StandardError=append:/app/statistical/logs/error.log
Restart=on-failure
EnvironmentFile=/app/statistical/.env
[Install]
WantedBy=multi-user.target重载并启动服务:
bash
# 创建必要的目录
mkdir -p /app/statistical/logs
# 复制配置文件
cp .env.default /app/statistical/.env
cp statistical /app/statistical/
# 启动服务
systemctl daemon-reload
systemctl start statistical
systemctl enable statistical
# 检查服务状态
systemctl status statistical3.2 Docker部署(推荐生产环境)
3.2.1 下载代码
bash
git clone https://gitlab.atom8.io/hkbitex/exchange-statistical-service-backend.git
cd exchange-statistical-service-backend3.2.2 编译代码
bash
make docker3.2.3 环境变量
在项目根目录创建 .env 文件(内容同上),或直接通过 Docker -e 方式传入。
3.2.4 启动服务
bash
docker run -d \
--name exchange-statistical-service-backend \
--env-file .env \
-p 8080:8080 \
registry.example.com/exchange-statistical-service-backend:latest4. 数据库配置
- 服务部署完成需要初始化试图,见
views/exchange_statistical_ddl_view.sql。
5. 日志配置
5.1 日志位置
- 默认日志输出:
/app/statistical/logs/ - 日志文件:
output.log:标准输出日志error.log:错误日志
5.2 日志级别
- 可通过 .env 配置(如 info、debug、error等)
- 建议生产环境使用 info 级别
6. 监控配置
Metrics 端点(Prometheus 可用): http://127.0.0.1:18080/metrics
建议接入 Grafana 进行可视化监控
7. 其他说明
- 无
