Skip to content

部署文档

版本号修改修改时间备注
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-backend

3.1.2 安装依赖

bash
# 安装 protoc 工具
make init

# 更新依赖
make tidy

3.1.3 编译代码

bash
make build

3.1.4 配置说明

环境变量类型是否必填说明
APP_LOG_LEVELstring可选,默认info日志级别(trace、debug、info、warn、error、fatal)
SERVER_HOSTstring必填服务监听地址(如 0.0.0.0:8080)
METRICS_SWITCHstring必填metrics 开关,on:启用,off:关闭
METRICS_HOSTstring必填metrics 服务监听地址(如 0.0.0.0:18080)
MYSQL_READstring必填MySQL 读库 DSN,例如:user:password@tcp(host:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
MYSQL_WRITEstring必填MySQL 写库 DSN(同上)
REDIS_URIstring必填Redis 连接地址
REDIS_PDstring必填Redis 密码
REDIS_DBstring必填Redis 数据库编号
S3_REGIONstring必填S3 区域
S3_BUCKETstring必填S3 存储桶名称
S3_ACCESS_KEY_IDstring必填S3 Access Key
S3_ACCESS_SECRETstring必填S3 Secret Key
EVENTUS_S3_REGIONstring必填Eventus S3 区域
EVENTUS_S3_ACCESS_KEY_IDstring必填Eventus S3 Access Key
EVENTUS_S3_ACCESS_SECRETstring必填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 statistical

3.2 Docker部署(推荐生产环境)

3.2.1 下载代码

bash
git clone https://gitlab.atom8.io/hkbitex/exchange-statistical-service-backend.git
cd exchange-statistical-service-backend

3.2.2 编译代码

bash
make docker

3.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:latest

4. 数据库配置

  • 服务部署完成需要初始化试图,见views/exchange_statistical_ddl_view.sql

5. 日志配置

5.1 日志位置

  • 默认日志输出:/app/statistical/logs/
  • 日志文件:
    • output.log:标准输出日志
    • error.log:错误日志

5.2 日志级别

  • 可通过 .env 配置(如 info、debug、error等)
  • 建议生产环境使用 info 级别

6. 监控配置

7. 其他说明

🚀 构建现代化数字资产交易平台