modelRT/deploy/deploy.md

20 KiB
Raw Blame History

项目依赖服务部署指南

本项目依赖于 \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} 容器

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 状态检查

要确认容器是否正在运行,请执行

# 检查容器启动状态
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} 容器

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 状态检查

要确认容器是否正在运行,请执行

# 检查容器启动状态
docker ps -a grep redis
# 检查容器启动日志信息
docker logs redis

2.4 数据注入

测试数据注入

2.4.1 Postgres数据注入
insert into public.grid(id,tagname,name,description,op,ts) VALUES (1, 'grid1', '网格1', '测试网格1', -1,CURRENT_TIMESTAMP);

insert into public.zone(id,grid_id,tagname,name,description,op,ts) VALUES (1, 1,'zone1', '区域1_1', '测试区域1_1', -1,CURRENT_TIMESTAMP);

insert into public.station(id,zone_id,tagname,name,description,is_local,op,ts) VALUES (1, 1,'station1', '站1_1_1', '测试站1_1_1', true, -1,CURRENT_TIMESTAMP),
(2, 1, 'station2', '站1_1_2', '测试站1_1_2', false, -1, CURRENT_TIMESTAMP);

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, station_id, type, in_service, state, status, connection, label, context, op, ts) 
VALUES 
(
    '968dd6e6-faec-4f78-b58a-d6e68426b09e', 
    'ns1', 'tag1', 'component1', '', '', 
    'grid1', 'zone1', 'station1', 1,
    -1, 
    false, 
    -1, -1, 
    '{}', 
    '{}', 
    '{}', 
    -1, 
    CURRENT_TIMESTAMP
),
(
    '968dd6e6-faec-4f78-b58a-d6e68426b08e', 
    'ns2', 'tag2', 'component2', '', '', 
    'grid1', 'zone1', 'station1', 1,
    -1, 
    false, 
    -1, -1, 
    '{}', 
    '{}', 
    '{}', 
    -1, 
    CURRENT_TIMESTAMP
),
(
    '968dd6e6-faec-4f78-b58a-d6e88426b09e', 
    'ns3', 'tag3', 'component3', '', '', 
    'grid1', 'zone1', 'station2', 2,
    -1, 
    false, 
    -1, -1, 
    '{}', 
    '{}', 
    '{}', 
    -1, 
    CURRENT_TIMESTAMP
);

INSERT INTO public.measurement (id, tag, name, type, size, data_source, event_plan, bay_uuid, component_uuid, op, ts) 
VALUES 
(3, 'I11_C_rms', '45母甲侧互连电流C相1', -1, 200, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM1", "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),
(4, 'I11_B_rms', '45母甲侧互连电流B相1', -1, 300, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM2", "station": "001"}}', '{"cause": {"upup": 65, "downdown": 35}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流B相1"]}, "enable": true}', '18e71a24-694a-43fa-93a7-c4d02a27d1bc', '968dd6e6-faec-4f78-b58a-d6e68426b09e', -1, CURRENT_TIMESTAMP),
(5, 'I11_A_rms', '45母甲侧互连电流A相1', -1, 300, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM3", "station": "001"}}', '{"cause": {"up": 55, "down": 45, "upup": 65, "downdown": 35}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流A相1"]}, "enable": true}', '18e71a24-694a-43fa-93a7-c4d02a27d1bc', '968dd6e6-faec-4f78-b58a-d6e68426b09e', -1, CURRENT_TIMESTAMP);

INSERT INTO public.project_manager (id, name, tag, meta_model, group_name, link_type, check_state, is_public, op, ts
) VALUES 
(1, 'component', 'component', '', 'component', 0, 
'{"checkState": [{"name": "global_uuid", "type": "UUID", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": -1}, {"name": "nspath", "type": "VARCHAR(32)", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": 32}, {"name": "tag", "type": "VARCHAR(32)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 32}, {"name": "name", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "description", "type": "VARCHAR(512)", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": 512}, {"name": "station", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "zone", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "grid", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "type", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "0", "lengthPrecision": -1}, {"name": "in_service", "type": "SMALLINT", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "state", "type": "INTEGER", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "connection", "type": "JSONB", "checked": 1, "isVisible": 1, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "label", "type": "JSONB", "checked": 1, "isVisible": 1, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "context", "type": "JSONB", "checked": 1, "isVisible": 0, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "op", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "-1", "lengthPrecision": -1}, {"name": "ts", "type": "TIMESTAMP", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": -1}, {"name": "model_name", "type": "VARCHAR(64)", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": 64}, {"name": "status", "type": "SMALLINT", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": -1}]}', TRUE, -1, CURRENT_TIMESTAMP
),
(2, 'bus_bus_1_base_extend', 'bus_1', 'bus', 'base_extend', 0, 
'{"checkState": [{"name": "bus_num", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "1", "lengthPrecision": -1}, {"name": "unom_kv", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP
),
(3, 'bus_bus_1_model', 'bus_1', 'bus', 'model', 0, 
'{"checkState": [{"name": "ui_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "ui_kv", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "35", "lengthPrecision": -1}, {"name": "ui_pa", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "stability_rated_current", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "1000", "lengthPrecision": -1}, {"name": "stability_dynamic_steady_current", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "40", "lengthPrecision": -1}, {"name": "load_adjustment_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "load_adjustment_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "bus_type", "type": "VARCHAR(10)", "checked": 1, "isVisible": 1, "defaultValue": "PQ母线", "lengthPrecision": 10}, {"name": "csc_s3_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_s3_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i3_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i3_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_z3s_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.05", "lengthPrecision": -1}, {"name": "csc_z3s_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.1", "lengthPrecision": -1}, {"name": "csc_s1_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_s1_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i1_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i1_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_z1s_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.05", "lengthPrecision": -1}, {"name": "csc_z1s_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.1", "lengthPrecision": -1}, {"name": "csc_base_voltage", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "37", "lengthPrecision": -1}, {"name": "csc_base_capacity", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP
),
(4, 'bus_bus_1_stable', 'bus_1', 'bus', 'stable', 0, 
'{"checkState": [{"name": "uvpw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "95", "lengthPrecision": -1}, {"name": "uvpw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "uvw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "90", "lengthPrecision": -1}, {"name": "uvw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "ovpw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "105", "lengthPrecision": -1}, {"name": "ovpw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "60", "lengthPrecision": -1}, {"name": "ovw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "110", "lengthPrecision": -1}, {"name": "ovw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "umargin_pmax", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "umargin_qmax", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "umargin_ulim", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "90", "lengthPrecision": -1}, {"name": "umargin_plim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}, {"name": "umargin_qlim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}, {"name": "umargin_ulim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP);

INSERT INTO public.bus_bus_1_stable (id, global_uuid, attribute_group, uvpw_threshold_percent, uvpw_runtime, uvw_threshold_percent, uvw_runtime, ovpw_threshold_percent, ovpw_runtime, ovw_threshold_percent, ovw_runtime, 
umargin_pmax, umargin_qmax, umargin_ulim, umargin_plim_percent, umargin_qlim_percent, umargin_ulim_percent
) VALUES (
    1, 
    '968dd6e6-faec-4f78-b58a-d6e68426b08e', 
    'stable', 
    95, 
    10, 
    90, 
    10, 
    105, 
    60, 
    110, 
    10, 
    0, 
    0, 
    90, 
    15, 
    15, 
    15
);

INSERT INTO public.bus_bus_1_model (id, global_uuid, attribute_group, 
ui_percent, ui_kv, ui_pa, stability_rated_current, stability_dynamic_steady_current, load_adjustment_min, load_adjustment_max, bus_type, csc_s3_max, csc_s3_min, csc_i3_max, csc_i3_min, csc_z3s_max, csc_z3s_min, csc_s1_max, csc_s1_min, csc_i1_max, csc_i1_min, csc_z1s_max, csc_z1s_min, csc_base_voltage, csc_base_capacity
) VALUES (
    1, 
    '968dd6e6-faec-4f78-b58a-d6e68426b08e', 
    'model', 
    100, 
    35, 
    0, 
    1000, 
    40, 
    100, 
    100, 
    'PQ母线', 
    0, 
    0, 
    0, 
    0, 
    0.05, 
    0.1, 
    0, 
    0, 
    0, 
    0, 
    0.05, 
    0.1, 
    37, 
    100
);

INSERT INTO public.bus_bus_1_base_extend (id, global_uuid, attribute_group, 
bus_num, unom_kv
) VALUES (
    1, 
    '968dd6e6-faec-4f78-b58a-d6e68426b08e', 
    'base_extend', 
    1, 
    NULL
);
2.4.2 Redis数据注入

Redis数据脚本

deploy/redis-test-data/measurments-recommend/measurement_injection.go

运行脚本向 Reids 导入数据

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} 无效时,从哪个位置开始消费(如 earliestlatest)。 "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 服务

go build -o model-rt main.go

3.3 启动服务

使用编译好的二进制文件进行启动

./model-rt

3.4 检测服务启动日志

在发现控制台输出如下信息starting ModelRT server 后即代表服务启动成功

4. 后续操作(停止与清理)

4.1 停止容器

docker stop postgres redis

4.2 删除容器(删除后数据将丢失)

docker rm postgres redis