一、概述
Core Data微服务主要为手机的设备数据进行持久性存储,默认采用RedisDB数据库进行存储,其他的数据库也可支持。
其他服务和系统(包括 EdgeX 特定服务和外部服务系统)通过核心数据服务访问传感器数据。 Core Data 还为数据位于边缘收集的数据提供一定程度的安全性和保护。
Core Data微服务是可选的,如果Device服务数据通过消息总线直接传递给Application服务,不需要本地存储,该服务可不搭建。
Core Data获取数据的两种方式:
- 消息总线:默认数据传输方式,采用的是MQTT
- rest api:采用此方式时,core data需要重新发布到消息总线,便与其他服务订阅,不如第一种方式简便,作为可选方式。
Core Data服务的数据默认通过MQTT传输给Application服务,也可以选择NATS,具体详见消息总线章节。
二、配置
通用配置字段:Core Data - Configuration - EdgeX Foundry Documentation
特定配置字段:Core Data - Configuration - EdgeX Foundry Documentation
// curl http://localhost:59880/api/v3/config
{
"apiVersion": "v3",
"config": {
"Database": {
"Host": "edgex-redis",
"Name": "coredata",
"Port": 6379,
"Timeout": "5s",
"Type": "redisdb"
},
"MaxEventSize": 25000,
"MessageBus": {
"AuthMode": "usernamepassword",
"BaseTopicPrefix": "edgex",
"Disabled": false,
"Host": "edgex-redis",
"Optional": {
"AutoProvision": "true",
"AutoReconnect": "true",
"ClientId": "core-data",
"ConnectTimeout": "5",
"DefaultPubRetryAttempts": "2",
"Deliver": "new",
"Durable": "",
"Format": "nats",
"KeepAlive": "10",
"Qos": "0",
"QueueGroup": "",
"Retained": "false",
"RetryOnFailedConnect": "true",
"SkipCertVerify": "false",
"Subject": "edgex/#"
},
"Port": 6379,
"Protocol": "redis",
"SecretName": "redisdb",
"Type": "redis"
},
"Registry": {
"Host": "edgex-core-consul",
"Port": 8500,
"Type": "consul"
},
"Retention": {
"Enabled": false,
"Interval": "30s",
"MaxCap": 10000,
"MinCap": 8000
},
"Service": {
"CORSConfiguration": {
"CORSAllowCredentials": false,
"CORSAllowedHeaders": "Authorization, Accept, Accept-Language, Content-Language, Content-Type, X-Correlation-ID",
"CORSAllowedMethods": "GET, POST, PUT, PATCH, DELETE",
"CORSAllowedOrigin": "https://localhost",
"CORSExposeHeaders": "Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma, X-Correlation-ID",
"CORSMaxAge": 3600,
"EnableCORS": false
},
"EnableNameFieldEscape": false,
"HealthCheckInterval": "10s",
"Host": "edgex-core-data",
"MaxRequestSize": 0,
"MaxResultCount": 1024,
"Port": 59880,
"RequestTimeout": "5s",
"ServerBindAddr": "",
"StartupMsg": "This is the Core Data Microservice"
},
"Writable": {
"InsecureSecrets": {
"DB": {
"SecretName": "redisdb",
"SecretData": {
"password": "",
"username": ""
}
}
},
"LogLevel": "INFO",
"PersistData": true,
"Telemetry": {
"Interval": "30s",
"Metrics": {
"EventsPersisted": false,
"ReadingsPersisted": false,
"SecurityConsulTokenDuration": false,
"SecurityConsulTokensRequested": false,
"SecurityGetSecretDuration": false,
"SecurityRuntimeSecretTokenDuration": false,
"SecuritySecretsRequested": false,
"SecuritySecretsStored": false
},
"Tags": null
}
}
},
"serviceName": "core-data"
}
三、相关API
Core Data - API Reference - EdgeX Foundry Documentation
数据形式
Event:对应一个设备的所有属性度量和读数,至少有一个属性度量和读数对;
Reading:对应一个属性度量和读数对;
比如:
Event代表motor123设备的所有度量和读数;
pressure:1300是其中一个reading;
四、代码解析
4.1 入口
edgex-go/cmd/core-data/main.go
edgex-go/cmd/core-data/main.go at napa · edgexfoundry/edgex-go
edgex-go/internal/core/data/main.go
主要启动的应用有:数据库、消息总线、指标遥测、event/reading读取、订阅服务、http服务、启动信息message等应用
4.2 连接数据库服务
edgex-go/internal/pkg/bootstrap/handlers/database.go
创建dbclient,负责连接数据库服务,配置来自Database
4.3 连接消息总线服务
go-mod-bootstrap/bootstrap/handlers/messaging.go
创建消息总线client,负责连接消息总线服务,配置来自MessageBus
4.4 指标发布
go-mod-bootstrap/bootstrap/handlers/metrics.go
go-mod-bootstrap/bootstrap/metrics/reporter.go
go-mod-bootstrap/bootstrap/metrics/manager.go
根据Writable.Telemetry相关配置,把收集到的指标定时发布到消息总线
4.5 注册计数指标
使用了开源框架:rcrowley/go-metrics: Go port of Coda Hale's Metrics library
注册metric item,包括events和readings
edgex-go/internal/core/data/application/app.go
go-mod-bootstrap/bootstrap/metrics/manager.go
4.6 启动消息总线订阅
- 加载http路由
- 订阅的messagebus对应的event topic
- 定时+异步清理数据
4.6.1 http路由注册
edgex-go/internal/core/data/router.go
提供rest api与core-data服务进行交互,相关路由如下
4.6.2 订阅服务
edgex-go/internal/core/data/controller/messaging/subscriber.go
订阅相关topic,并执行AddEvent操作
采用redisdb进行存储
4.7 启动http服务
4.8 服务启动完毕
go-mod-bootstrap/bootstrap/handlers/message.go
主要是输出startup message
五、交互图
5.1 添加设备或者传感器的读数
5.2 获取设备或者传感器的读数
到此结束!!!!