core-metadata是主要是EDGEX系统中设备微服务和设备以及设备数据相关的元数据管理服务,core-command使用介绍如下,他有2个启动参数可以配置:
Usage: %s [options]
Server Options:
-r, --registry Indicates service should use Registry
-p, --profile Indicate configuration profile other than default
Common Options:
-h, --help Show this message
Table of Contents
core-metadata的配置文件
Writable
Clients
Databases
Logging
Registry
Service
Notifications
[Writable]
LogLevel = 'INFO'
[Service]
BootTimeout = 30000
ClientMonitor = 15000
CheckInterval = '10s'
Host = 'localhost'
Port = 48081
Protocol = 'http'
ReadMaxLimit = 100
StartupMsg = 'This is the EdgeX Core Metadata Microservice'
Timeout = 5000
[Registry]
Host = 'localhost'
Port = 8500
Type = 'consul'
[Logging]
EnableRemote = false
File = './logs/edgex-core-metadata.log'
[Clients]
[Clients.Logging]
Protocol = 'http'
Host = 'localhost'
Port = 48061
[Clients.Notifications]
Protocol = 'http'
Host = 'localhost'
Port = 48060
[Databases]
[Databases.Primary]
Host = 'localhost'
Name = 'metadata'
Password = ''
Port = 27017
Username = ''
Timeout = 5000
Type = 'mongodb'
[Notifications]
PostDeviceChanges = true
Slug = 'device-change-'
Content = 'Device update: '
Sender = 'core-metadata'
Description = 'Metadata device notice'
Label = 'metadata'
如上是项目自带的core-metadata的配置文件 ,主要分7部分,core-metadata启动后会将配置文件的信息导入如下结构中,若registry=True则根据Registry中的信息连接consul并拉取相应的配置信息覆盖之前从本地配置文件中读取到的信息。
// Struct used to parse the JSON configuration file
type ConfigurationStruct struct {
Writable WritableInfo
Clients map[string]config.ClientInfo
Databases map[string]config.DatabaseInfo
Logging config.LoggingInfo
Notifications config.NotificationInfo
Registry config.RegistryInfo
Service config.ServiceInfo
}
type WritableInfo struct {
LogLevel string
}
LogLevel 日志等级,这也是唯一一个可以在运行时被修改并被应用的参数
// 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-metadata运行时需要使用其他微服务提供的功能,Client便是连接其他微服务所必须的参数,这是一个map参数,key值便是其他微服务的名字,value便是client的具体参数信息, core-metadata正常运行一般需要使用logging, notification两种微服务,需要注意的是在配置文件Clients字段下面各个服务名字按照项目自带配置文件给出的名字命名不要修改,因为core-metadata引用这些参数时是硬编码的,如初始化Notification的部分:
Configuration.Clients["Notifications"].Url()
直接通过字符串寻找 Notification的参数,如果配置文件中服务名被修改了,便无法找到。
func initializeClients(useRegistry bool) {
// Create notification client
params := types.EndpointParams{
ServiceKey: internal.SupportNotificationsServiceKey,
Path: clients.ApiNotificationRoute,
UseRegistry: useRegistry,
Url: Configuration.Clients["Notifications"].Url() + clients.ApiNotificationRoute,
Interval: Configuration.Service.ClientMonitor,
}
nc = notifications.NewNotificationsClient(params, startup.Endpoint{RegistryClient: ®istryClient})
}
type DatabaseInfo struct {
Type string
Timeout int
Host string
Port int
Username string
Password string
Name string
}
Type 数据库类型,目前仅支持 mongodb
Timeout 数据库连接超时,单位:s
Host 数据库地址
Port 数据库端口
Username 数据库登录用户名
Password 数据库登录密码
Name 数据库名字
与support-logging不同,core-metadata当前支持两种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
}
}
// 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本地持久化时日志文件的路径
// 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连接参数
同support-logging
// Notification Info provides properties related to the assembly of notification content
type NotificationInfo struct {
Content string
Description string
Label string
PostDeviceChanges bool
Sender string
Slug string
}
此处的Notification与Clients下的Notification不是同一个概念,Clients.Notification是接入微服务support-notifications的参数,而此处是发送notification是的消息组织方法的配置参数。
Notification的数据结构定义如下:
// Notification defines the structure of data being sent.
type Notification struct {
Id string `json:"id,omitempty"` // Generated by the system, users can ignore
Slug string `json:"slug"` // A meaningful identifier provided by client
Sender string `json:"sender"` // Indicates the sender of a notification -- possibly a service name or email address
Category CategoryEnum `json:"category"` // Allows for categorization of notifications
Severity SeverityEnum `json:"severity"` // Denotes the severity of a notification
Content string `json:"content"` // Content contains the body of the notification
Description string `json:"description,omitempty"` // Description content for the notification
Status StatusEnum `json:"status,omitempty"` // Status reflects a simple workflow assignment for the notification
Labels []string `json:"labels,omitempty"` // Labels allows the notification to be further described/classified
Created int `json:"created,omitempty"` // The creation timestamp
Modified int `json:"modified,omitempty"` // The last modification timestamp
}
每次发送notification的消息时便需要依据配置参数和具体的消息内容构造如上的结构,然后才能使用相应的接口发送消息,如下:
func postNotification(name string, action string, ctx context.Context) {
// Only post notification if the configuration is set
if Configuration.Notifications.PostDeviceChanges {
// Make the notification
notification := notifications.Notification{
Slug: Configuration.Notifications.Slug + strconv.FormatInt(db.MakeTimestamp(), 10),
Content: Configuration.Notifications.Content + name + "-" + string(action),
Category: notifications.SW_HEALTH,
Description: Configuration.Notifications.Description,
Labels: []string{Configuration.Notifications.Label},
Sender: Configuration.Notifications.Sender,
Severity: notifications.NORMAL,
}
nc.SendNotification(notification, ctx)
}
}
Content Notification中content的前缀,真实的content内容参考如上代码
Description 消息类型的描述
Label 标签
PostDeviceChanges bool量,Notification使能开关
Sender 发送者,发送消息的微服务的名称,如 "core-metadata"
Slug Notification中Slug的前缀,真实的Slug会在此字段后面加上timestamp