Skip to content

Latest commit

 

History

History
272 lines (244 loc) · 6.78 KB

File metadata and controls

272 lines (244 loc) · 6.78 KB

DistributedDetectionNode

分布式节点健康监控平台。

设计方案

  1. 提供 HTTP/Websocket 服务,可以用 Websocket 是否连接作为是否在线的判断依据。
  2. 使用 MongoDB 数据库存储客户端推送的指标数据,支持水平扩展的集群服务。
  3. 提供一些接口可以为 Prometheus 提供数据。
  4. 提供一些管理接口或功能,如数据清理、持久化等。

备注: MongoDB 5.0 版本太低,时间序列功能不全,还有 Bug,需要使用最新的 7.0 以上版本。

docker pull mongodb/mongodb-community-server:7.0.12-ubuntu2204
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:7.0.12-ubuntu2204

Build

go build -ldflags "-X main.version=v0.0.1" -o ddn ./app/ddn/ddn.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=v0.0.1" -o ddn ./app/ddn/ddn.go

Run

创建一个 JSON 配置文件,示例如下:

{
  "Addr": "0.0.0.0:9521",
  "LogLevel": "info",
  "LogFile": "./test.log",
  "MongoDB": {
    "URI": "mongodb://127.0.0.1:27017/",
    "Database": "health_monitoring",
    "ExpireTime": 86400
  },
  "IP2LocationDB": {
    "DatabasePath": "./IP2LOCATION-LITE-DB5.BIN/IP2LOCATION-LITE-DB5.BIN"
  },
  "Prometheus": {
    "JobName": "test"
  },
  "Chain": {
    "Rpc": "https://rpc-testnet.dbcwallet.io",
    "ChainId": 19850818,
    "PrivateKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "ReportContract": {
      "AbiFile": "./dbc/ai_abi.json",
      "ContractAddress": "0xb616A0dad9af4cA23234b65D27176be2c09c720c"
    },
    "MachineInfoContract": {
      "AbiFile": "./dbc/0xE676096cA8B957e914094c5e044Fcf99f5dbf3C0.json",
      "ContractAddress": "0xE676096cA8B957e914094c5e044Fcf99f5dbf3C0"
    }
  },
  "NotifyThirdParty": {
    "OfflineNotify": "https://nodeapi.deeplink.cloud/api/cyc/notifyOffline"
  }
}

使用命令 ddn -config ./config.json 运行即可。

程序会启动一个 WebSocket 服务,可以使用 ws://192.168.1.159:9521/websocket 连接。

WebSocket

WebSocket 设置了心跳服务,即 client 发送 ping 消息,服务回复 pong 消息。 如果 30s 内没有任何 ping 消息,连接将被服务端断开。 请及时发送 ping 消息,既是一种心跳,又能保证长连接的稳定可靠。

WebSocket 消息采用 UTF-8 文本格式,主要使用 JSON 形式。具体示例请看 测试用例

client 向 server 发送的请求消息主要由 Header 和 Body 两部分组成。

字段 描述 类型 备注
Header version 协议版本,暂时用 0 uint32
timestamp 时间戳 int64
id 消息序号,一对请求与应答的序号相同 uint64
type 消息体的类型,0 - 保留, 1 - online,2 - 机器信息 uint32
pub_key 公钥,验证消息安全完整,暂时不需要 []byte
sign 签名,验证消息安全完整,暂时不需要 []byte
Body body 消息体,真正的消息通过 JSON 编码,加密后的字节数组 []byte

消息体暂时有以下几种:

  • 0 - 没有意义
  • 1 - Online,表示 WebSocket 连接属于那个设备或者节点。
{
  "machine_id": "123456789",
  "project": "deeplink",
  "staking_type": 0
}
  • 2 - DeepLink 短租设备信息,定时发送显卡等机器信息。
{
  "cpu_type": "13th Gen Intel(R) Core(TM) i5-13400F",
  "cpu_rate": 2500,
  "gpu_names": [
    "NVIDIA GeForce RTX 4060"
  ],
  "gpu_memory_total": [
    8
  ],
  "memory_total": 16,
  "wallet": "xxxxxx"
}
  • 3 - 通知消息。
{
  "unregister": {
    "message": "machine unregistered, notify from node server"
  }
}
  • 4 - DeepLink 带宽挖矿设备信息。
{
  "cpu_cores": 1,
  "memory_total": 2,
  "hdd": 50,
  "bandwidth": 10,
  "wallet": "xxxxxx"
}

server 向 client 返回的应答消息体格式结构相似,只比请求多了 Code 和 Message 两个字段。

字段 描述 类型 备注
Header version 协议版本,暂时用 0 uint32
timestamp 时间戳 int64
id 消息序号,一对请求与应答的序号相同 uint64
type 消息体的类型,与请求的类型相同 uint32
pub_key 公钥,验证消息安全完整,暂时不需要 []byte
sign 签名,验证消息安全完整,暂时不需要 []byte
Code code 错误码,0 表示正常 uint32
Message message 错误信息 string
Body body 消息体,真正的消息通过 JSON 编码,加密后的字节数组 []byte

Prometheus

假设本服务的 HTTP 地址设置为 192.168.1.159:9527,当需要为 Prometheus 提供监控数据时,只需要在 Prometheus 的配置中增加如下的 scrape_config:

# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    metrics_path: "/metrics/prometheus"
    static_configs:
      - targets: ["192.168.1.159:9527"]

参考文档

  1. 本项目使用 IP2Location LITE 资料库于 IP地理定位.