configuration

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2024 License: MPL-2.0 Imports: 15 Imported by: 17

README

Aluka-7 配置管理引擎

提供给所有业务系统使用的配置管理引擎(基于Zookeeper),所有业务系统/中间件的可变配置通过集中配置中心进行统一配置,业务系统可通过该类来管理自己的配置数据并在配置中心的数据发生变化时得到及时的通知。

配置管理中心的数据存储为树形目录结构(类似文件系统的目录结构),每一个节点都可以存储相应的数据。业务系统可在基础目录树的基础上往下继续扩展新的目录结构,从而可以区分存储不同的配置项信息。比如系统分配给业务系统A的基础目录(称为path)为 /sysa ,那么业务系统可以扩充到如下path: /sysa/key1、/sysa/key2、/sysa/key3 ,每个path可存储对应的数据。

配置管理引擎客户端依赖连接配置管理中心,需要在同一配置管理中心下载对应的授权文件方可。

快速使用

  1. 获取配置管理引擎的唯一实例

config:=configuration.Engine()
  1. 获取多个配置项的配置信息,返回原始的配置数据格式(map集合),如果获取失败则抛出异常。
config.Values(path []string) (map[string]string, error)
  1. 获取指定配置项的配置信息,返回原始的配置数据格式,如果获取失败则抛出异常。
config.String(path string) (string, error) 
  1. 获取指定配置项的配置信息,并且将配置信息(JSON格式的)转换为指定的Go结构体,如果获取失败或转换失败则抛出异常。
config.Clazz(path string, clazz interface{}) error 
  1. 判断些path是否存在
config.Has(path string) (bool, error)
  1. 获取指定路径下的配置信息,并实现监听,当有数据变化时自动调用解析器进行解析。
par := &Parser{path: []string{path}}
config.Get(path []string, par).Process([]string{path}, par)
type Parser struct {
    path []string
}

func (p Parser) Parse(data map[string]string) {
   //此处对于获取的值进行处理,map的key为path
}
func (p Parser) Changed(data map[string]string) {
    p.Parse(data)
}

Documentation

Index

Constants

View Source
const DesKey = "aluka-7!"
View Source
const Namespace = "/system"

Variables

This section is empty.

Functions

func EventEngine

func EventEngine(conf backends.StoreConfig) eventEngine

func NewStoreConfig

func NewStoreConfig() (conf backends.StoreConfig)

NewStoreConfig 提供给所有业务系统使用的配置管理引擎,所有业务系统/中间件的可变配置通过集中配置中心进行统一 配置,业务系统可通过该类来管理 自己的配置数据并在配置中心的数据发生变化时得到及时的通知。

配置管理中心的数据存储为树形目录结构(类似文件系统的目录结构),每一个节点都可以存储相应的数据。业务系统可在基础目录树的基础上 往下继续扩展新的目录结构,从而可以区分存储不同的配置项信息。比如系统分配给业务系统A的基础目录(称为path)为/sysa, 那么业务系统可以扩充到如下path:/sysa/key1、/sysa/key2、/sysa/key3,每个path可存储对应的数据。

配置管理引擎客户端依赖连接的配置管理中心,需要在同一配置管理中心下载对应的授权文件方可。

Types

type ChangedListener

type ChangedListener interface {
	// Changed 指定配置变化后的通知接口,标示变化的路径和变化后的data(变化后的新数据值)。
	Changed(data map[string]string)
}

ChangedListener 配置数据发生变化时的监听器接口。

type Configuration

type Configuration interface {
	Values(app, group, tag string, path []string) (map[string]string, error)
	String(app, group, tag, path string) (string, error)
	Clazz(app, group, tag, path string, clazz interface{}) error
	Get(app, group, tag string, path []string, parser ChangedListener)
	Watch(app, group, tag, path string, callback EndpointCacher)
	Lock(app, group, tag, path string) *zk.Lock
	Add(app, group, tag, path string, value []byte, flags int32) (string, error)
	Modify(app, group, tag, path string, value []byte) error
	Delete(app, group, tag, path string) error
}

func DefaultEngine

func DefaultEngine() Configuration

func Engine

func Engine(conf backends.StoreConfig) Configuration

Engine 获取配置管理引擎的唯一实例。

func MockEngine

func MockEngine(t *testing.T, conf backends.StoreConfig) Configuration

type EndpointCacher added in v1.0.4

type EndpointCacher interface {
	Add(string, []byte)
	Edit(string, []byte)
	Del(string)
}

type Event

type Event struct {
	Key       string                 `json:"key"`       // 事件的唯一key
	PubTime   int64                  `json:"pub_time"`  // 事件的发布时间,单位毫秒
	Timeout   int64                  `json:"timeout"`   // 超时时间,单位毫秒
	Body      map[string]interface{} `json:"body"`      // 事件的数据
	Published bool                   `json:"published"` // 是否发布了,防止重复发送
}

Event 系统之间的准实时通知事件对象,封装需要发布的事件信息。 需要特别注意的是,每个事件的key必须是全局唯一的,不仅仅是系统内唯一,而是整个系统生态内唯一。 另外,每个时间会被赋予一个超时时间(默认是10s),当事件被发布时会被设置一个发布时间,接收端接收到事件后会根据其系统本地时间和发布时间进行对 比,如果差额超过了超时时间则认为事件超时了则事件监听程序不会被执行,反之事件程序会得到执行。 每个事件允许携带一定量的数据,携带的数据总量以字节长度计算(JSON格式化后的),限制最大数据长度为1k,超过则不允许发送事件。

func NewFCEvent

func NewFCEvent(key string) (*Event, error)

NewFCEvent 使用事件key构造一个具有默认超时时间的事件对象,默认的超时时间为10s。

func NewFCEventTimeout

func NewFCEventTimeout(key string, timeout int64) (*Event, error)

NewFCEventTimeout 给定事件的key和超时时间来构建一个事件对象,每个事件都有一个发布时间,如果接收时间的系统时间-发布时间>超时时间则认为事件超时了,则接收端系统不会被触发事件处理程序。

func (*Event) AddData

func (e *Event) AddData(key string, value interface{}) *Event

AddData 添加一个key(数据对应的key)-value(数据对应的值)数据到事件对象中。

func (Event) GeKey

func (e Event) GeKey() string

GeKey 获取当前事件对象对应的事件key,总是不为空。

func (Event) GePubTime

func (e Event) GePubTime() int64

GePubTime 获取时间的发布时间,单位毫秒。

func (*Event) GetData

func (e *Event) GetData(key string) interface{}

GetData 获取指定key的事件数据,如果指定的数据key不存在则会返回nil。

func (*Event) IsOverloaded

func (e *Event) IsOverloaded() bool

IsOverloaded 判断当前事件数据载体的总字节大小是否超标了,以JSON格式化后的字节数为标准,最大只允许1024字节的数据。

func (Event) IsPublished

func (e Event) IsPublished() bool

IsPublished 事件是否被发布了,防止重复发送。

func (Event) IsTimeout

func (e Event) IsTimeout() bool

IsTimeout 判断当前事件是否超时了,以系统本地时间和发布时间的差额为判断标准。

func (Event) Json

func (e Event) Json() ([]byte, error)

func (*Event) SetPubTime

func (e *Event) SetPubTime(pubTime int64) *Event

SetPubTime 设置事件的pubTime(发布时间),当事件被发布时由系统自动设置。

func (*Event) SetPublished

func (e *Event) SetPublished() *Event

SetPublished 标示该事件对象已被发布过。

type EventListener

type EventListener interface {
	// EventKeys 该监听器要监听的事件key列表(可指定多个),总是返回非空列表。
	EventKeys() []string

	// OnEvent 事件监听到的回调处理方法,由业务系统自行处理。
	OnEvent(event Event)
}

EventListener 跨系统的事件监听器接口定义,适用于两个/多个在线系统之间的实时通知,业务系统只需要实现该接口并注册到服务中后即可实现跨系统的事件监听。

type Processor

type Processor interface {
	Process(listener ChangedListener)
}

func WatchProcessor

func WatchProcessor(path []string, store backends.StoreClient) Processor

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL