Documentation ¶
Overview ¶
Example (Rcc) ¶
Example_RCC example code to show how to use by default rcc client
/* @Time : 2021-06-11 @Author : xiemalin */ package main import ( "context" "fmt" ) // DemoSet demo set type DemoSet struct { data map[string]string } // Update callback function while regitered in Watch method. func (d *DemoSet) Update(ce *ChangeEvent) { // 建议defer捕获协程panic defer func() { if r := recover(); r != nil { fmt.Println("watch update callback panic") } }() for key, change := range ce.Changes { if change.ChangeType == ADD || change.ChangeType == MODIFY { d.data[key] = change.NewValue } } } // Example_RCC example code to show how to use by default rcc client func main() { conf, err := NewConf(TestConfFile) if err != nil { fmt.Printf("new conf fail, got :%v", err) return } err = StartWithConf(context.Background(), conf) if err != nil { fmt.Printf("start default cient fail, got :%v", err) return } a1 := GetValue("a1", "222") fmt.Println("get value", a1) keys := GetAllKeys() for _, key := range keys { fmt.Println("print out key", key) } d := DemoSet{data: map[string]string{}} d.data["a1"] = "a1" d.data["a2"] = "a2" // 监听变更 Watch(d.Update) if err := Stop(); err != nil { fmt.Printf("Stop should return nil, got :%v", err) return } }
Output:
Index ¶
- Constants
- func Bind(s interface{}) error
- func GetAllKeys() []string
- func GetValue(key, defaultValue string) string
- func StartWithConf(ctx context.Context, conf *Conf) error
- func StartWithConfFile(ctx context.Context, name string) error
- func Stop() error
- func Watch(callback func(ce *ChangeEvent))
- type ApiResponse
- type Cache
- type Change
- type ChangeEvent
- type ChangeType
- type Client
- func (c *Client) Bind(s interface{}) error
- func (c *Client) GetAllKeys() []string
- func (c *Client) GetValue(key, defaultValue string) string
- func (c *Client) Start() error
- func (c *Client) Stop() error
- func (c *Client) Watch(callback func(ce *ChangeEvent))
- func (c *Client) WatchUpdate() <-chan *ChangeEvent
- type Conf
- type IPoller
- type IRequester
- type Item
Examples ¶
Constants ¶
View Source
const ( DefaultCallbackIntervalInt = 300 // 默认300s触发一次更新拉取 DefaultRequestTimeoutInt = 5 // 默认5秒钟接口调用超时时间 DefaultFilePath = "/tmp/rcc" )
Variables ¶
This section is empty.
Functions ¶
func StartWithConf ¶
StartWithConf run rcc-client with Conf
func StartWithConfFile ¶
StartWithConfFile run rcc-client with conf file
Types ¶
type ApiResponse ¶
type Change ¶
type Change struct { OldValue string NewValue string ChangeType ChangeType }
Change represent a single key change
type ChangeType ¶
type ChangeType int
ChangeType for a key
const ( // ADD a new value ADD ChangeType = iota // MODIFY a old value MODIFY // DELETE ... DELETE )
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client for rcc
Example ¶
ExampleNewConf the example to show use client to connect server
client, err := NewClientWithConfFile(TestConfFile) if err != nil { return } err = client.Start() if err != nil { return } defer client.Stop() keys := client.GetAllKeys() for _, v := range keys { client.GetValue(v, "DefaultValue") } s := struct { Key string `rcc:"mykey"` }{} client.Bind(&s)
Output:
func NewClientWithConf ¶
NewClientWithConf create client from conf
func NewClientWithConfFile ¶
NewClientWithConfFile create client from conf file
func (*Client) GetAllKeys ¶
GetAllKeys return all config keys in given namespace
func (*Client) Watch ¶
func (c *Client) Watch(callback func(ce *ChangeEvent))
Watch
Example ¶
ExampleClient_Watch he example to show use Watch function
client, err := NewClientWithConfFile(TestConfFile) if err != nil { return } err = client.Start() if err != nil { return } defer client.Stop() // define update callback for Watch function callback := func(ce *ChangeEvent) { // 建议defer捕获协程panic defer func() { if r := recover(); r != nil { fmt.Println("watch update callback panic") } }() for key, change := range ce.Changes { if change.ChangeType == ADD || change.ChangeType == MODIFY { fmt.Println("changed item", key, change.NewValue, change.OldValue) } } } client.Watch(callback)
Output:
func (*Client) WatchUpdate ¶
func (c *Client) WatchUpdate() <-chan *ChangeEvent
WatchUpdate get all updates
type Conf ¶
type Conf struct { ProjectName string `toml:"projectName"` EnvName string `toml:"envName"` ServerUrl string `toml:"serverUrl"` ApiPassword string `toml:"apiPassword"` VersionName string `toml:"versionName"` EnableCallback bool `toml:"enableCallback"` CallbackIntervalInt int64 `toml:"callbackInterval"` RequestTimeoutInt int64 `toml:"requestTimeout"` EnableCache bool `toml:"enableCache"` CacheDir string `json:"cacheDir,omitempty"` Token string EnvId int64 CallbackInterval time.Duration RequestTimeout time.Duration }
Conf
type IPoller ¶
type IPoller interface {
// contains filtered or unexported methods
}
Poller fetch conf updates
Source Files ¶
Click to show internal directories.
Click to hide internal directories.