Define the configuration structure with default values
type TestConfig struct {
IntVal int `json:"int_val" default:"8080"`
StrVal string `default:"test"`
SliceVal []int `default:"1,2,3,4,ee"`
MapVal map[string]string `default:"a:aa,b:bb,c,d"`
}
Create grc instance
rc, err := grc.New(context.TODO(), grc.EtcdV3, "127.0.0.1:2379", "username", "password")
if err != nil {
//return
}
Subscribe configuration
path := "/test/conf"
err := rc.SubscribeConf(path, TestConfig{})
if err != nil {
//return
}
Use configuration
conf := rc.GetConf(path).(TestConfig)
- The return type of
rc.GetConf(path)
is the same as the second parameter passed intorc.SubscribeConf
. - The type can also be a pointor:
err := rc.SubscribeConf(path, &TestConfig{})
if err != nil {
//return
}
conf := rc.GetConf(path).(*TestConfig)
Register node
node := "127.0.0.1:6600"
path := "/test/service/a"
err := rc.RegisterNode(path, node, time.Second*5)
if err != nil {
//return
}
Get service nodes
nodes := rc.GetService(path)