core-data是主要是EDGEX系统中数据持久化的服务,core-data使用介绍如下,他有2个启动参数可以配置:
- registry是一个bool量,True表示core-data的配置参数是从consul拉取,False表示从本地配置文件载入。
- profile本地配置文件的路径, 即便registry=True, 该参数也必须要指明一个配置文件,因为连接consul服务所需的host,port等参数必须从本地的配置文件中获取。
Usage: %s [options]
Server Options:
-r, --registry Indicates service should use Registry
-p, --profile <name> Indicate configuration profile other than default
Common Options:
-h, --help Show this message
Table of Contents
core-data的配置文件
[Writable]
DeviceUpdateLastConnected = false
MetaDataCheck = false
PersistData = true
ServiceUpdateLastConnected = false
ValidateCheck = false
LogLevel = 'INFO'
[Service]
BootTimeout = 30000
ClientMonitor = 15000
CheckInterval = '10s'
Host = 'localhost'
Port = 48080
Protocol = 'http'
ReadMaxLimit = 100
StartupMsg = 'This is the Core Data Microservice'
Timeout = 5000
[Registry]
Host = 'localhost'
Port = 8500
Type = 'consul'
[Logging]
EnableRemote = false
File = './logs/edgex-core-data.log'
[Clients]
[Clients.Metadata]
Protocol = 'http'
Host = 'localhost'
Port = 48081
[Clients.Logging]
Protocol = 'http'
Host = 'localhost'
Port = 48061
[Databases]
[Databases.Primary]
Host = 'localhost'
Name = 'coredata'
Password = ''
Port = 27017
Username = ''
Timeout = 5000
Type = 'mongodb'
[MessageQueue]
Protocol = 'tcp'
Host = '*'
Port = 5563
Type = 'zero'
Topic = 'events'
如上是项目自带的core-data的配置文件 ,主要分7部分,core-data启动后会将配置文件的信息导入如下结构中,若registry=True则根据Registry中的信息连接consul并拉取相应的配置信息覆盖之前从本地配置文件中读取到的信息。
type ConfigurationStruct struct {
Writable WritableInfo
MessageQueue config.MessageQueueInfo
Clients map[string]config.ClientInfo
Databases map[string]config.DatabaseInfo
Logging config.LoggingInfo
Registry config.RegistryInfo
Service config.ServiceInfo
}
Writable
type WritableInfo struct {
DeviceUpdateLastConnected bool
MetaDataCheck bool
PersistData bool
ServiceUpdateLastConnected bool
ValidateCheck bool
LogLevel string
}
与其他微服务不同,core-data有多个参数可以在运行时被修改
DeviceUpdateLastConnected bool,设备连接状态更新开关,若使能,则每次设备提交数据后都会更新设备在metadata中最近提交数的时间,该信息可用于检测设备是否在线正常工作
MetaDataCheck bool,设备有效检查使能开关,任何Event都是绑定在一个Device下,只有Device已经被注册(能够在metadata服务中查找)其相应的Event才会被认为是有效的
PersistData bool, event和reading持久化使能开关
ServiceUpdateLastConnected bool,设备微服务连接状态更新开关,若使能,则每次设备微服提交数据(无论是哪个设备的数据)后都会更新设备在metadata中最近提交数的时间,该信息可用于检测设备微服务是否在线正常工作
ValidateCheck bool,接收数据的有效性检查,若使能,每次接收到数据后会根据其名字匹配ValueDESCRIPTOR,只有匹配成功才能通过有效性检查
LogLevel 日志等级
Clients
// ClientInfo provides the host and port of another service in the eco-system.
type ClientInfo struct {
// Host is the hostname or IP address of a service.
Host string
// Port defines the port on which to access a given service
Port int
// Protocol indicates the protocol to use when accessing a given service
Protocol string
}
Host 微服务的地址
Port 微服务的端口
Protocol 微服务所使用的协议,目前仅支持http
core-data运行时需要使用其他微服务提供的功能,Client便是连接其他微服务所必须的参数,这是一个map参数,key值便是其他微服务的名字,value便是client的具体参数信息, core-data正常运行一般需要使用logging, metadata, 两种微服务,需要注意的是在配置文件Clients字段下面各个服务名字按照项目自带配置文件给出的名字命令不要修改,因为core-data引用这些参数时是硬编码的,如初始化metadata-client的部分:
Configuration.Clients["Metadata"].Url() + clients.ApiDeviceRoute
直接通过字符串寻找 metadata的参数,如果配置文件中服务名被修改了,便无法找到。
// Create metadata clients
params := types.EndpointParams{
ServiceKey: internal.CoreMetaDataServiceKey,
Path: clients.ApiDeviceRoute,
UseRegistry: useRegistry,
Url: Configuration.Clients["Metadata"].Url() + clients.ApiDeviceRoute,
Interval: Configuration.Service.ClientMonitor,
}
mdc = metadata.NewDeviceClient(params, startup.Endpoint{RegistryClient: ®istryClient})
Databases
type DatabaseInfo struct {
Type string
Timeout int
Host string
Port int
Username string
Password string
Name string
}
Type 数据库类型,目前支持 mongodb, redisdb
Timeout 数据库连接超时,单位:s
Host 数据库地址
Port 数据库端口
Username 数据库登录用户名
Password 数据库登录密码
Name 数据库名字
与support-logging不同,core-data当前支持两种Primary数据库mongodb,redisdb, 不过需要注意的是从源码中可以分析出使用redis数据库时目前还不支持redis加密功能,这点应该很快会被修正。
dbConfig中没有传入Redis加密相关的参数,如下:
// Return the dbClient interface
func newDBClient(dbType string) (interfaces.DBClient, error) {
switch dbType {
case db.MongoDB:
dbConfig := db.Configuration{
Host: Configuration.Databases["Primary"].Host,
Port: Configuration.Databases["Primary"].Port,
Timeout: Configuration.Databases["Primary"].Timeout,
DatabaseName: Configuration.Databases["Primary"].Name,
Username: Configuration.Databases["Primary"].Username,
Password: Configuration.Databases["Primary"].Password,
}
return mongo.NewClient(dbConfig)
case db.RedisDB:
dbConfig := db.Configuration{
Host: Configuration.Databases["Primary"].Host,
Port: Configuration.Databases["Primary"].Port,
}
return redis.NewClient(dbConfig) //TODO: Verify this also connects to Redis
default:
return nil, db.ErrUnsupportedDatabase
}
}
Logging
// LoggingInfo provides basic parameters related to where logs should be written.
type LoggingInfo struct {
EnableRemote bool
File string
}
EnableRemote True: log于support-logging微服务持久化(support-logging的连接参数由Clients字段提供), False:log于本地文件持久化
File log本地持久化时日志文件的路径
Registry
// RegistryInfo defines the type and location (via host/port) of the desired service registry (e.g. Consul, Eureka)
type RegistryInfo struct {
Host string
Port int
Type string
}
consul连接参数
Service
同support-logging
MessageQueue
// MessageQueueInfo provides parameters related to connecting to a message queue
type MessageQueueInfo struct {
// Host is the hostname or IP address of the broker, if applicable.
Host string
// Port defines the port on which to access the message queue.
Port int
// Protocol indicates the protocol to use when accessing the message queue.
Protocol string
// Indicates the message queue platform being used.
Type string
// Indicates the topic the data is published/subscribed
Topic string
}
MessageQueue是消息队列的连接参数,源码中给出的默认消息队列使用的是ZeroMQ,当然目前ZeroMQ也是唯一支持的消息队列,这点可从源码中得知,如下:
// NewMessageClient is a factory function to instantiate different message client depending on
// the "Type" from the configuration
func NewMessageClient(msgConfig types.MessageBusConfig) (MessageClient, error) {
if msgConfig.PublishHost.IsHostInfoEmpty() && msgConfig.SubscribeHost.IsHostInfoEmpty() {
return nil, fmt.Errorf("unable to create messageClient: host info not set")
}
switch lowerMsgType := strings.ToLower(msgConfig.Type); lowerMsgType {
case ZeroMQ:
return zeromq.NewZeroMqClient(msgConfig)
default:
return nil, fmt.Errorf("unknown message type '%s' requested", msgConfig.Type)
}
}
Host 消息队列服务接入主机名
Port 消息队列服务接入端口
Protocol 协议类型
Type 消息队列的类型,目前仅支持 zero
Topic 消息主题,消息队列采用订阅/发布模式