README ¶
paladin
项目简介
paladin 是一个config SDK客户端,包括了file、mock几个抽象功能,方便使用本地文件或者sven\apollo配置中心,并且集成了对象自动reload功能。
local files:
demo -conf=/data/conf/app/msm-servie.toml
// or dir
demo -conf=/data/conf/app/
注:使用远程配置中心的用户在执行应用,如这里的demo
时务必不要带上-conf
参数,具体见下文远程配置中心的例子
local file example:
type exampleConf struct {
Bool bool
Int int64
Float float64
String string
}
func (e *exampleConf) Set(text string) error {
var ec exampleConf
if err := toml.Unmarshal([]byte(text), &ec); err != nil {
return err
}
*e = ec
return nil
}
func ExampleClient() {
if err := paladin.Init(); err != nil {
panic(err)
}
var (
ec exampleConf
eo exampleConf
m paladin.TOML
strs []string
)
// config unmarshal
if err := paladin.Get("example.toml").UnmarshalTOML(&ec); err != nil {
panic(err)
}
// config setter
if err := paladin.Watch("example.toml", &ec); err != nil {
panic(err)
}
// paladin map
if err := paladin.Watch("example.toml", &m); err != nil {
panic(err)
}
s, err := m.Value("key").String()
b, err := m.Value("key").Bool()
i, err := m.Value("key").Int64()
f, err := m.Value("key").Float64()
// value slice
err = m.Value("strings").Slice(&strs)
// watch key
for event := range paladin.WatchEvent(context.TODO(), "key") {
fmt.Println(event)
}
}
remote config center example:
type exampleConf struct {
Bool bool
Int int64
Float float64
String string
}
func (e *exampleConf) Set(text string) error {
var ec exampleConf
if err := yaml.Unmarshal([]byte(text), &ec); err != nil {
return err
}
*e = ec
return nil
}
func ExampleApolloClient() {
/*
pass flags or set envs that apollo needs, for example:
```
export APOLLO_APP_ID=SampleApp
export APOLLO_CLUSTER=default
export APOLLO_CACHE_DIR=/tmp
export APOLLO_META_ADDR=localhost:8080
export APOLLO_NAMESPACES=example.yml
```
*/
if err := paladin.Init(apollo.PaladinDriverApollo); err != nil {
panic(err)
}
var (
ec exampleConf
eo exampleConf
m paladin.Map
strs []string
)
// config unmarshal
if err := paladin.Get("example.yml").UnmarshalYAML(&ec); err != nil {
panic(err)
}
// config setter
if err := paladin.Watch("example.yml", &ec); err != nil {
panic(err)
}
// paladin map
if err := paladin.Watch("example.yml", &m); err != nil {
panic(err)
}
s, err := m.Value("key").String()
b, err := m.Value("key").Bool()
i, err := m.Value("key").Int64()
f, err := m.Value("key").Float64()
// value slice
err = m.Value("strings").Slice(&strs)
// watch key
for event := range paladin.WatchEvent(context.TODO(), "key") {
fmt.Println(event)
}
}
编译环境
- 请只用 Golang v1.12.x 以上版本编译执行
依赖包
Documentation ¶
Index ¶
- Variables
- func Bool(v *Value, def bool) bool
- func Close() error
- func Drivers() []string
- func Duration(v *Value, def time.Duration) time.Duration
- func Float32(v *Value, def float32) float32
- func Float64(v *Value, def float64) float64
- func Init(args ...interface{}) (err error)
- func Int(v *Value, def int) int
- func Int32(v *Value, def int32) int32
- func Int64(v *Value, def int64) int64
- func KeyNamed(key string) string
- func Keys() []string
- func Register(name string, driver Driver)
- func String(v *Value, def string) string
- func Watch(key string, s Setter) error
- func WatchEvent(ctx context.Context, keys ...string) <-chan Event
- type Client
- type Driver
- type Event
- type EventType
- type Getter
- type Map
- type Mock
- type Setter
- type TOML
- type Value
- func (v *Value) Bool() (bool, error)
- func (v *Value) Duration() (time.Duration, error)
- func (v *Value) Float32() (float32, error)
- func (v *Value) Float64() (float64, error)
- func (v *Value) Int() (int, error)
- func (v *Value) Int32() (int32, error)
- func (v *Value) Int64() (int64, error)
- func (v *Value) Raw() (string, error)
- func (v *Value) Slice(dst interface{}) error
- func (v *Value) String() (string, error)
- func (v *Value) Unmarshal(un encoding.TextUnmarshaler) error
- func (v *Value) UnmarshalJSON(dst interface{}) error
- func (v *Value) UnmarshalTOML(dst interface{}) error
- func (v *Value) UnmarshalYAML(dst interface{}) error
- type Watcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotExist = errors.New("paladin: value key not exist") ErrTypeAssertion = errors.New("paladin: value type assertion no match") ErrDifferentTypes = errors.New("paladin: value different types") )
ErrNotExist value key not exist.
Functions ¶
func Drivers ¶
func Drivers() []string
Drivers returns a sorted list of the names of the registered paladin driver.
func Duration ¶
Duration parses a duration string. A duration string is a possibly signed sequence of decimal numbers each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func Init ¶
func Init(args ...interface{}) (err error)
Init init config client. If confPath is set, it inits file client by default Otherwise we could pass args to init remote client args[0]: driver name, string type
func Register ¶
Register makes a paladin driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.
Types ¶
type Client ¶
Client is config client.
Example ¶
ExampleClient is an example client usage. exmaple.toml:
bool = true int = 100 float = 100.1 string = "text" strings = ["a", "b", "c"]
Output:
var ( // DefaultClient default client. DefaultClient Client )
type Driver ¶
Driver defined paladin remote client impl each remote config center driver must do 1. implements `New` method 2. call `Register` to register itself
type Getter ¶
type Getter interface { // Get a config value by a config key(may be a sven filename). Get(string) *Value // GetAll return all config key->value map. GetAll() *Map }
Getter is value getter.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is config map, key(filename) -> value(file).
Example ¶
ExampleMap is an example map usage. exmaple.toml:
bool = true int = 100 float = 100.1 string = "text" strings = ["a", "b", "c"] [object] string = "text" bool = true int = 100 float = 100.1 strings = ["a", "b", "c"]
Output:
type TOML ¶
type TOML = Map
TOML is toml map.
func (*TOML) UnmarshalText ¶
UnmarshalText implemented toml.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is config value, maybe a json/toml/ini/string file.
func (*Value) Duration ¶
Duration parses a duration string. A duration string is a possibly signed sequence of decimal numbers each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (*Value) Unmarshal ¶
func (v *Value) Unmarshal(un encoding.TextUnmarshaler) error
Unmarshal is the interface implemented by an object that can unmarshal a textual representation of itself.
func (*Value) UnmarshalJSON ¶
UnmarshalJSON unmarhsal json to struct.
func (*Value) UnmarshalTOML ¶
UnmarshalTOML unmarhsal toml to struct.
func (*Value) UnmarshalYAML ¶
UnmarshalYAML unmarshal yaml to struct.