Etcd和Consul都是非常出名的服務(wù)化治理工具叫潦。本篇先來講一下Etcd的使用沧侥。
學(xué)會使用
安裝
go get github.com/coreos/etcd/client
引用
"github.com/coreos/etcd/client"
Example
cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:2379"},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
使用說明
創(chuàng)建客戶端
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
設(shè)置配置
kapi := client.NewKeysAPI(c)
log.Print("Setting '/foo' key with 'bar' value")
resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
if err != nil {
log.Fatal(err)
} else {
log.Printf("Set is done. Metadata is %q\n", resp)
}
讀取配置
kapi := client.NewKeysAPI(c)
resp, err = kapi.Get(context.Background(), "/foo", nil)
if err != nil {
log.Fatal(err)
} else {
log.Printf("Get is done. Metadata is %q\n", resp)
log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
}