modelRT/deploy/deploy.md

265 lines
9.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 项目依赖服务部署指南
本项目依赖于 $\text{PostgreSQL}$ 数据库和 $\text{Redis Stack Server}$(包含 $\text{Redisearch}$ 等模块)部署文档将使用 $\text{Docker}$ 容器化技术部署这两个依赖服务
## 前提条件
1. 已安装 $\text{Docker}$
2. 下载相关容器镜像
3. 确保主机的 $\text{5432}$ 端口($\text{Postgres}$)和 $\text{6379}$ 端口($\text{Redis}$)未被占用
### 1\. 部署 PostgreSQL 数据库
使用官方的 `postgres:13.16` 镜像,并设置默认的用户、密码和端口
#### 1.1 部署命令
运行以下命令启动 $\text{PostgreSQL}$ 容器
```bash
docker run --name postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=coslight \
-p 5432:5432 \
-d postgres:13.16
```
#### 1.2 连接信息
| 参数 | 值 | 说明 |
| :--- | :--- | :--- |
| **容器名称** | `postgres` | 容器名 |
| **镜像版本** | `postgres:13.16` | 镜像名 |
| **主机端口** | `5432` | 外部应用连接使用的端口 |
| **用户名** | `postgres` | 默认超级用户 |
| **密码** | `coslight` | 配置的密码 |
#### 1.3 状态检查
要确认容器是否正在运行,请执行
```bash
# 检查容器启动状态
docker ps -a grep postgres
# 检查容器启动日志信息
docker logs postgres
```
### 2\. 部署 Redis Stack Server
我们将使用 `redis/redis-stack-server:latest` 镜像该镜像内置了 $\text{Redisearch}$ 模块,用于 $\text{ModelRT}$ 项目中补全功能
#### 2.1 部署命令
运行以下命令启动 $\text{Redis Stack Server}$ 容器
```bash
docker run --name redis -p 6379:6379 \
-d redis/redis-stack-server:latest
```
#### 2.2 连接信息
| 参数 | 值 | 说明 |
| :--- | :--- | :--- |
| **容器名称** | `redis` | 容器名 |
| **镜像版本** | `redis/redis-stack-server:latest` | 镜像名 |
| **主机端口** | `6379` | 外部应用连接使用的端口 |
| **地址** | `localhost:6379` | |
| **密码** | **无** | 默认未设置密码 |
> **注意:** 生产环境中建议使用 `-e REDIS_PASSWORD=<your_secure_password>` 参数来设置 $\text{Redis}$ 访问密码
#### 2.3 状态检查
要确认容器是否正在运行,请执行
```bash
# 检查容器启动状态
docker ps -a grep redis
# 检查容器启动日志信息
docker logs redis
```
#### 2.4 数据注入
测试数据注入
##### 2.4.1 Postgres数据注入
```SQL
INSERT INTO public.topologic(flag, uuid_from, uuid_to, context, description, op, ts)
VALUES
(1, '00000000-0000-0000-0000-000000000000', '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '10f155cf-bd27-4557-85b2-d126b6e2657f', '{}', '', 1, CURRENT_TIMESTAMP),
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '{}', '', 1, CURRENT_TIMESTAMP),
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '70c190f2-8a75-42a9-b166-ec5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '70c200f2-8a75-42a9-c166-bf5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '968dd6e6-faec-4f78-b58a-d6e68426b09e', '{}', '', 1, CURRENT_TIMESTAMP),
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '968dd6e6-faec-4f78-b58a-d6e68426b08e', '{}', '', 1, CURRENT_TIMESTAMP);
INSERT INTO public.bay (bay_uuid, name, tag, type, unom, fla, capacity, description, in_service, state, grid, zone, station, business, context, from_uuids, to_uuids, dev_protect, dev_fault_record, dev_status, dev_dyn_sense, dev_instruct, dev_etc, components, op, ts)
VALUES (
'18e71a24-694a-43fa-93a7-c4d02a27d1bc',
'', '', '',
-1, -1, -1,
'',
false,
-1,
'', '', '',
'{}',
'{}',
'[]',
'[]',
'[]',
'[]',
'[]',
'[]',
'[]',
'[]',
ARRAY['968dd6e6-faec-4f78-b58a-d6e68426b09e', '968dd6e6-faec-4f78-b58a-d6e68426b08e']::uuid[],
-1,
CURRENT_TIMESTAMP
);
INSERT INTO public.component (global_uuid, nspath, tag, name, model_name, description, grid, zone, station, type, in_service, state, status, connection, label, context, op, ts)
VALUES
(
'968dd6e6-faec-4f78-b58a-d6e68426b09e',
'ns1', 'tag1', 'component1', '', '',
'grid1', 'zone1', 'station1',
-1,
false,
-1, -1,
'{}',
'{}',
'{}',
-1,
CURRENT_TIMESTAMP
),
(
'968dd6e6-faec-4f78-b58a-d6e68426b08e',
'ns1', 'tag2', 'component2', '', '',
'grid1', 'zone1', 'station1',
-1,
false,
-1, -1,
'{}',
'{}',
'{}',
-1,
CURRENT_TIMESTAMP
);
INSERT INTO public.measurement (tag, name, type, size, data_source, event_plan, bay_uuid, component_uuid, op, ts)
VALUES (
'I11_C_rms',
'45母甲侧互连电流C相1',
-1,
200,
'{"type": 1, "io_address": {"device": "ssu001", "channel": "Telemetry1", "station": "001"}}',
'{"cause": {"up": 55.0, "down": 45.0}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流C相1"]}, "enable": true}',
'18e71a24-694a-43fa-93a7-c4d02a27d1bc',
'968dd6e6-faec-4f78-b58a-d6e68426b09e',
-1,
CURRENT_TIMESTAMP
);
```
##### 2.4.2 Redis数据注入
Redis数据脚本
```shell
deploy/redis-test-data/measurments-recommend/measurement_injection.go
```
运行脚本向 Reids 导入数据
```shell
go run deploy/redis-test-data/measurments-recommend/measurement_injection.go
```
### 3\. 启动 ModelRT 服务
#### 3.1 配置服务配置文件
以下表格为配置文件参数说明表
| 类别 | 参数名 | 作用描述 | 示例值 |
| :--- | :--- | :--- | :--- |
| **Postgres** | `host` | PostgreSQL 数据库服务器的 $\text{IP}$ 地址或域名。 | `"192.168.1.101"` |
| | `port` | PostgreSQL 数据库服务器的端口号。 | `5432` |
| | `database` | 连接的数据库名称。 | `"demo"` |
| | `user` | 连接数据库所使用的用户名。 | `"postgres"` |
| | `password` | 连接数据库所使用的密码。 | `"coslight"` |
| **Kafka** | `servers` | Kafka 集群的 $\text{Bootstrap Server}$ 地址列表(通常是 $\text{host:port}$ 形式,多个地址用逗号分隔)。 | `"localhost:9092"` |
| | `port` | Kafka 服务器的端口号。 | `9092` |
| | `group_id` | 消费者组 $\text{ID}$,用于标识和管理一组相关的消费者。 | `"modelRT"` |
| | `topic` | Kafka 消息的主题名称。 | `""` |
| | `auto_offset_reset` | 消费者首次启动或 $\text{Offset}$ 无效时,从哪个位置开始消费(如 `earliest``latest`)。 | `"earliest"` |
| | `enable_auto_commit` | 是否自动提交 $\text{Offset}$。设为 $\text{false}$ 通常用于手动控制 $\text{Offset}$ 提交。 | `"false"` |
| | `read_message_time_duration` | 读取消息时的超时或等待时间。 | `”0.5s"` |
| **Logger (Zap)** | `mode` | 日志模式,通常为 `development`(开发)或 `production`(生产)。影响日志格式。 | `"development"` |
| | `level` | 最低日志级别(如 $\text{debug, info, warn, error}$)。 | `"debug"` |
| | `filepath` | 日志文件的输出路径和名称格式(`%s` 会被替换为日期等)。 | `"/Users/douxu/Workspace/coslight/modelRT/modelRT-%s.log"` |
| | `maxsize` | 单个日志文件最大大小(单位:$\text{MB}$)。 | `1` |
| | `maxbackups` | 保留旧日志文件的最大个数。 | `5` |
| | `maxage` | 保留旧日志文件的最大天数。 | `30` |
| | `compress` | 是否压缩备份的日志文件。 | `false` |
| **Ants Pool** | `parse_concurrent_quantity` | 用于解析任务的协程池最大并发数量。 | `10` |
| | `rtd_receive_concurrent_quantity` | 用于实时数据接收任务的协程池最大并发数量。 | `10` |
| **Locker Redis** | `addr` | 分布式锁服务所使用的 $\text{Redis}$ 地址。 | `"127.0.0.1:6379"` |
| | `password` | $\text{Locker Redis}$ 的密码。 | `""` |
| | `db` | $\text{Locker Redis}$ 使用的数据库编号。 | `1` |
| | `poolsize` | $\text{Locker Redis}$ 连接池的最大连接数。 | `50` |
| | `timeout` | $\text{Locker Redis}$ 连接操作的超时时间(单位:毫秒)。 | `10` |
| **Storage Redis** | `addr` | 数据存储服务所使用的 $\text{Redis}$ 地址(例如 $\text{Redisearch}$)。 | `"127.0.0.1:6379"` |
| | `password` | $\text{Storage Redis}$ 的密码。 | `""` |
| | `db` | $\text{Storage Redis}$ 使用的数据库编号。 | `0` |
| | `poolsize` | $\text{Storage Redis}$ 连接池的最大连接数。 | `50` |
| | `timeout` | $\text{Storage Redis}$ 连接操作的超时时间(单位:毫秒)。 | `10` |
| **Base Config** | `grid_id` | 项目所操作的默认电网 $\text{ID}$。 | `1` |
| | `zone_id` | 项目所操作的默认区域 $\text{ID}$。 | `1` |
| | `station_id` | 项目所操作的默认变电站 $\text{ID}$。 | `1` |
| **Service Config** | `service_name` | 服务名称,用于日志、监控等标识。 | `"modelRT"` |
| | `secret_key` | 服务内部使用的秘钥,用于签名或认证。 | `"modelrt_key"` |
| **DataRT API** | `host` | 外部 $\text{DataRT}$ 服务的主机地址。 | `"http://127.0.0.1"` |
| | `port` | $\text{DataRT}$ 服务的端口号。 | `8888` |
| | `polling_api` | 轮询数据的 $\text{API}$ 路径。 | `"datart/getPointData"` |
| | `polling_api_method` | 调用该 $\text{API}$ 使用的 $\text{HTTP}$ 方法。 | `"GET"` |
#### 3.2 编译 ModelRT 服务
```bash
go build -o model-rt main.go
```
#### 3.3 启动服务
使用编译好的二进制文件进行启动
```bash
./model-rt
```
#### 3.4 检测服务启动日志
在发现控制台输出如下信息`starting ModelRT server`
后即代表服务启动成功
### 4\. 后续操作(停止与清理)
#### 4.1 停止容器
```bash
docker stop postgres redis
```
#### 4.2 删除容器(删除后数据将丢失)
```bash
docker rm postgres redis
```