EdgeX Core Service 核心服务之 Core Data 核心数据

概述

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包括eventsreadings

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 apicore-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 获取设备或者传感器的读数

到此结束!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值