archaius

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2019 License: Apache-2.0 Imports: 18 Imported by: 66

README

go-archaius

Build Status

This is a light weight configuration management framework which helps to manage configurations in distributed system

The main objective of go archaius is to pull and sync the configuration from config-sources

Why use go-archaius

it is hard to manage configurations in a distributed system. archaius is able to put all configuration in distributed system together and manage them. To make it simple to get the exact config you want in distributed system. It also keeps watching configuration changes, and fire change event if value changes. so that you can easily implement a service which has hot-reconfiguration features. when you need to change configurations, your service has zero-down time.

Conceptions

Sources

Go-archaius can manage multiple sources at the same time. Each source can holds same or different key value pairs. go-archaius keeps all the sources marked with their precedence, and merge key value based on precedence. in case if two sources have same key then key with higher precedence will be selected, and you can use archaius API to get its value

Here is the precedence list:

0: Config Center Source - use config client to pull remote config server data into local

0: Test Source - it can be used in UT or AT scenario

1: Memory Source - after process start, you can set key value in runtime.

2: Command Line Source - read the command lines arguments, while starting the process.

3: Environment Variable Source - read configuration in Environment variable.

4: Files Source - read files content and convert it into key values based on the FileHandler you define

Dimension

It only works if you enable Config Center Source, as remote config server, it could has a lot of same key but value is different. so we use dimension to identify kv. by default you can not get other dimension kv, but after you enable config center source, you can get kv in other dimension by archaius API

Event management

You can register event listener by key(exactly match or pattern match) to watch value change.

File Handler

It works in File source, it decide how to convert your file to key value pairs. check FileHandler, currently we have 2 file handler implementation

archaius API

developer usually only use API to interact with archaius, check API

Example: Manage local configurations

Complete example

Example: Manage key value change events

Complete example

Example: Manage runtime configurations by remote config server

import a config client implementation

import _ "github.com/go-chassis/go-cc-client/configcenter"

give config client to init config center source

	ci := archaius.ConfigCenterInfo{
	//input your config center source config
	}
	//create config client 
	cc:=ccclient.NewClient("config_center",ccclient.Options{
    		ServerURI:"the address of config server endpoint",
    	})
	//manage local and remote key value at same time
	err = archaius.Init(
		archaius.WithRequiredFiles([]string{filename1}),
		archaius.WithOptionalFiles([]string{filename2}),
		archaius.WithConfigCenterSource(ci, cc),
	)

To check config server that archaius supports, access https://github.com/go-chassis/go-cc-client

Documentation

Overview

Package archaius provides you APIs which helps to manage files, remote config center configurations

Index

Constants

View Source
const (
	//UnsuccessfulArchaiusInit is of type string
	UnsuccessfulArchaiusInit = "issue with go-archaius initialization"
)

Variables

This section is empty.

Functions

func AddDI

func AddDI(dimensionInfo string) (map[string]string, error)

AddDI adds a NewDimensionInfo of which configurations needs to be taken

func AddFile

func AddFile(file string, opts ...FileOption) error

AddFile is for to add the configuration files into the configfactory at run time

func AddKeyValue

func AddKeyValue(key string, value interface{}) error

AddKeyValue add the configuration key, value pairs into memory source at runtime it is just affect the local configs

func AddSource

func AddSource(source core.ConfigSource) error

AddSource add source implementation

func Clean added in v0.15.0

func Clean() error

Clean will call config manager CleanUp Method, it deletes all sources which means all of key value is deleted. after you call Clean, you can init archaius again

func CustomInit added in v0.13.0

func CustomInit(sources ...core.ConfigSource) error

CustomInit accept is able to accept a list of config source, add it into archaius runtime. it almost like Init(), but you can fully control config sources you inject to archaius

func DeleteKeyValue

func DeleteKeyValue(key string, value interface{}) error

DeleteKeyValue delete the configuration key, value pairs in memory source

func EnableConfigCenterSource added in v0.12.0

func EnableConfigCenterSource(ci ConfigCenterInfo, cc config.Client) error

EnableConfigCenterSource create a config center source singleton A config center source pull remote config server key values into local memory so that you can use GetXXX to get value easily

func Exist

func Exist(key string) bool

Exist check the configuration key existence

func Get

func Get(key string) interface{}

Get is for to get the value of configuration key

func GetBool

func GetBool(key string, defaultValue bool) bool

GetBool is gives the key value in the form of bool

func GetConfigs

func GetConfigs() map[string]interface{}

GetConfigs gives the information about all configurations

func GetConfigsByDI

func GetConfigsByDI(dimensionInfo string) map[string]interface{}

GetConfigsByDI get the all configurations in other dimension

func GetFloat64

func GetFloat64(key string, defaultValue float64) float64

GetFloat64 gives the key value in the form of float64

func GetInt

func GetInt(key string, defaultValue int) int

GetInt gives the key value in the form of GetInt

func GetString

func GetString(key string, defaultValue string) string

GetString gives the key value in the form of GetString

func GetStringByDI

func GetStringByDI(dimensionInfo, key string, defaultValue string) string

GetStringByDI get the value of configuration key in other dimension

func Init

func Init(opts ...Option) error

Init create a Archaius config singleton

func RegisterListener

func RegisterListener(listenerObj core.EventListener, key ...string) error

RegisterListener to Register all listener for different key changes, each key could be a regular expression

func UnRegisterListener

func UnRegisterListener(listenerObj core.EventListener, key ...string) error

UnRegisterListener is to remove the listener

func UnmarshalConfig

func UnmarshalConfig(obj interface{}) error

UnmarshalConfig is for unmarshalling the configuraions of receiving object

Types

type ConfigCenterInfo

type ConfigCenterInfo struct {
	//required.
	//Key value can be in different namespace, we call it dimension.
	//although key is same but in different dimension, the value is different.
	//you must specify the default dimension, so that the config center source will just pull this dimension's key value
	DefaultDimensionInfo string

	//archaius config center source support 2 types of refresh mechanism:
	//0: Web-Socket Based -  client makes an web socket connection with
	//the config server and keeps getting an events whenever any data changes.
	//1: Pull Configuration interval- In this type client keeps polling the configuration from
	//the config server at regular intervals.
	RefreshMode int

	//Pull Configuration interval, unit is second
	RefreshInterval int

	//Configurations for config client implementation
	//if you alread create a client, don't need to set those config
	URL           string
	TenantName    string
	EnableSSL     bool
	TLSConfig     *tls.Config
	AutoDiscovery bool
	ClientType    string
	Version       string
	RefreshPort   string
	Environment   string
}

ConfigCenterInfo has attribute for config center source initialization

type ConfigFactory

type ConfigFactory struct {
	// contains filtered or unexported fields
}

ConfigFactory is a struct which stores configuration information

func (*ConfigFactory) AddByDimensionInfo

func (arc *ConfigFactory) AddByDimensionInfo(dimensionInfo string) (map[string]string, error)

AddByDimensionInfo adds a NewDimensionInfo of which configurations needs to be taken

func (*ConfigFactory) AddSource

func (arc *ConfigFactory) AddSource(source core.ConfigSource) error

AddSource return all values of different sources

func (*ConfigFactory) DeInit

func (arc *ConfigFactory) DeInit() error

DeInit return all values of different sources

func (*ConfigFactory) GetConfigurationByKey

func (arc *ConfigFactory) GetConfigurationByKey(key string) interface{}

GetConfigurationByKey return all values of different sources

func (*ConfigFactory) GetConfigurationByKeyAndDimensionInfo

func (arc *ConfigFactory) GetConfigurationByKeyAndDimensionInfo(dimensionInfo, key string) interface{}

GetConfigurationByKeyAndDimensionInfo get the value for a key in a particular dimensionInfo

func (*ConfigFactory) GetConfigurations

func (arc *ConfigFactory) GetConfigurations() map[string]interface{}

GetConfigurations dump complete configuration managed by config-client

Only return highest priority key value:-
1. ConfigFile 		2. Environment Variable
3. Commandline Argument	4. config Center configuration
config-center value being the highest priority

func (*ConfigFactory) GetConfigurationsByDimensionInfo

func (arc *ConfigFactory) GetConfigurationsByDimensionInfo(dimensionInfo string) map[string]interface{}

GetConfigurationsByDimensionInfo dump complete configuration managed by config-client Only return config Center configurations.

func (*ConfigFactory) GetValue

func (arc *ConfigFactory) GetValue(key string) cast.Value

GetValue an abstraction to return key's value in respective type

func (*ConfigFactory) GetValueByDI

func (arc *ConfigFactory) GetValueByDI(dimensionInfo, key string) cast.Value

GetValueByDI an abstraction to return key's value in respective type based on dimension info which is provided by user

func (*ConfigFactory) Init

func (arc *ConfigFactory) Init() error

Init intiates the Configurationfatory

func (*ConfigFactory) IsKeyExist

func (arc *ConfigFactory) IsKeyExist(key string) bool

IsKeyExist check existence of key

func (*ConfigFactory) Refresh

func (arc *ConfigFactory) Refresh(name string) error

Refresh pull config from source and update configuration map

func (*ConfigFactory) RegisterListener

func (arc *ConfigFactory) RegisterListener(listenerObj core.EventListener, keys ...string) error

RegisterListener Function to Register all listener for different key changes

func (*ConfigFactory) UnRegisterListener

func (arc *ConfigFactory) UnRegisterListener(listenerObj core.EventListener, keys ...string) error

UnRegisterListener remove listener

func (*ConfigFactory) Unmarshal

func (arc *ConfigFactory) Unmarshal(obj interface{}) error

Unmarshal function is used in the case when user want his yaml file to be unmarshalled to structure pointer Unmarshal function accepts a pointer and in called function anyone can able to get the data in passed object Unmarshal only accepts a pointer values Unmarshal returns error if obj values are 0. nil and value type. Procedure:

  1. Unmarshal first checks the passed object type using reflection.
  2. Based on type Unmarshal function will check and set the values ex: If type is basic types like int, string, float then it will assigb directly values, If type is map, ptr and struct then it will again send for unmarshal untill it find the basic type and set the values

type ConfigurationFactory

type ConfigurationFactory interface {
	// Init ConfigurationFactory
	Init() error
	// dump complete configuration managed by config-client based on priority
	// (1. config Center 2. Commandline Argument 3.Environment Variable  4.ConfigFile , 1 with highest priority
	GetConfigurations() map[string]interface{}
	// dump complete configuration managed by config-client for config Center based on dimension info.
	GetConfigurationsByDimensionInfo(dimensionInfo string) map[string]interface{}
	// add the dimension info for other services
	AddByDimensionInfo(dimensionInfo string) (map[string]string, error)
	// return all values of different sources
	GetConfigurationByKey(key string) interface{}
	// check for existence of key
	IsKeyExist(string) bool
	// unmarshal data on user define structure
	Unmarshal(structure interface{}) error
	// Add custom sources
	AddSource(core.ConfigSource) error
	//Function to Register all listener for different key changes, each key could be a regular expression
	RegisterListener(listenerObj core.EventListener, key ...string) error
	// remove listener
	UnRegisterListener(listenerObj core.EventListener, key ...string) error
	// DeInit
	DeInit() error
	// an abstraction to return key's value in respective type
	GetValue(key string) cast.Value
	// return values of config-center source based on key and dimension info
	GetConfigurationByKeyAndDimensionInfo(dimensionInfo, key string) interface{}
	// an abstraction to return key's value in respective type based on dimension info which is provided by user
	GetValueByDI(dimensionInfo, key string) cast.Value
	Refresh(name string) error
}

ConfigurationFactory is a list of Interface for config Center

func GetConfigFactory

func GetConfigFactory() ConfigurationFactory

GetConfigFactory return factory

func NewConfigFactory

func NewConfigFactory() (ConfigurationFactory, error)

NewConfigFactory creates a new configuration object for config center

type EventListener

type EventListener struct {
	Name    string
	Factory ConfigurationFactory
}

EventListener is a struct having information about registering key and object

func (EventListener) Event

func (e EventListener) Event(event *core.Event)

Event is invoked while generating events at run time

type FileOption

type FileOption func(options *FileOptions)

FileOption is a func

func WithFileHandler

func WithFileHandler(h filesource.FileHandler) FileOption

WithFileHandler use custom handler

type FileOptions

type FileOptions struct {
	Handler filesource.FileHandler
}

FileOptions for AddFile func

type Option

type Option func(options *Options)

Option is a func

func WithCommandLineSource

func WithCommandLineSource() Option

WithCommandLineSource enable cmd line source archaius will read command line params as key value

func WithConfigCenterSource added in v0.12.0

func WithConfigCenterSource(cci ConfigCenterInfo, c config.Client) Option

WithConfigCenterSource accept the information for initiating a config center source, ConfigCenterInfo is required if you want to use config center source client is optional,if client is nil, archaius will create one based on ConfigCenterInfo config client will be injected into config source as a client to interact with a config server

func WithDefaultFileHandler

func WithDefaultFileHandler(handler filesource.FileHandler) Option

WithDefaultFileHandler let user custom handler you can decide how to convert file into kv pairs

func WithENVSource

func WithENVSource() Option

WithENVSource enable env source archaius will read ENV as key value

func WithMemorySource

func WithMemorySource() Option

WithMemorySource accept the information for initiating a Memory source

func WithOptionalFiles

func WithOptionalFiles(f []string) Option

WithOptionalFiles tell archaius to manage files, if not exist will NOT return error

func WithRequiredFiles

func WithRequiredFiles(f []string) Option

WithRequiredFiles tell archaius to manage files, if not exist will return error

type Options

type Options struct {
	RequiredFiles    []string
	OptionalFiles    []string
	FileHandler      filesource.FileHandler
	ConfigCenterInfo ConfigCenterInfo
	ConfigClient     config.Client
	UseCLISource     bool
	UseENVSource     bool
	UseMemSource     bool
}

Options hold options

Directories

Path Synopsis
Package core provides a list of interface for Dispatcher and ConfigMgr
Package core provides a list of interface for Dispatcher and ConfigMgr
cast
Package cast provides the typeCasting of an object
Package cast provides the typeCasting of an object
config-manager
Package configmanager provides functions to communicate to Config-Center Package configmanager provides deserializer
Package configmanager provides functions to communicate to Config-Center Package configmanager provides deserializer
event-system
Package eventsystem provides the different Listeners
Package eventsystem provides the different Listeners
examples
hack
* Created by on 2017/7/19.
* Created by on 2017/7/19.
sources
commandline-source
Package commandlinesource created on 2017/6/22.
Package commandlinesource created on 2017/6/22.
configcenter
Package configcenter created on 2017/6/22.
Package configcenter created on 2017/6/22.
enviromentvariable-source
Package envconfigsource created on 2017/6/22.
Package envconfigsource created on 2017/6/22.
file-source
Package filesource created on 2017/6/22.
Package filesource created on 2017/6/22.

Jump to

Keyboard shortcuts

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