关于etcd的详细介绍和实战清查看如下文章git
其余操做请查看etcd/clientv3官方文档。github
参考连接:golang
本文仅对go操做etcd的put,get,watch过程作一下简单介绍。json
go get go.etcd.io/etcd/clientv3
因为go版本等问题,可能会在安装时报错,具体解决方案能够查看如下文章:c#
https://learnku.com/articles/43758数据结构
https://my.oschina.net/u/2321997/blog/4258724/printmvc
https://colobu.com/2020/04/09/accidents-of-etcd-and-go-module/app
package main import ( "context" "fmt" "time" "go.etcd.io/etcd/clientv3" ) func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{"127.0.0.1:2379"}, DialTimeout: 5 * time.Second, }) if err != nil { // handle error! fmt.Printf("connect to etcd failed, err:%v\n", err) return } fmt.Println("connect to etcd success") defer cli.Close() // put ctx, cancel := context.WithTimeout(context.Background(), time.Second) _, err = cli.Put(ctx, "q1mi", "dsb") cancel() if err != nil { fmt.Printf("put to etcd failed, err:%v\n", err) return } }
上述代码的逻辑步骤是:ide
client的数据结构为:函数
type Client
type Client struct { Cluster KV Lease Watcher Auth Maintenance // Username is a user name for authentication. Username string // Password is a password for authentication. Password string // contains filtered or unexported fields }
kv接口的数据结构:
type KV
type KV interface { Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) Do(ctx context.Context, op Op) (OpResponse, error) Txn(ctx context.Context) Txn }
package main import ( "context" "fmt" "time" "go.etcd.io/etcd/clientv3" ) func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{"127.0.0.1:2379"}, DialTimeout: 5 * time.Second, }) if err != nil { // handle error! fmt.Printf("connect to etcd failed, err:%v\n", err) return } fmt.Println("connect to etcd success") defer cli.Close() // get ctx, cancel = context.WithTimeout(context.Background(), time.Second) resp, err := cli.Get(ctx, "q1mi") cancel() if err != nil { fmt.Printf("get from etcd failed, err:%v\n", err) return } for _, ev := range resp.Kvs { fmt.Printf("%s:%s\n", ev.Key, ev.Value) } }
上述代码的逻辑步骤是:
这里说一下返回值得数据结构:*GetResponse
type RangeResponse struct { Header *ResponseHeader `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` // kvs is the list of key-value pairs matched by the range request. // kvs is empty when count is requested. Kvs []*mvccpb.KeyValue `protobuf:"bytes,2,rep,name=kvs,proto3" json:"kvs,omitempty"` // more indicates if there are more keys to return in the requested range. More bool `protobuf:"varint,3,opt,name=more,proto3" json:"more,omitempty"` // count is set to the number of keys within the range when requested. Count int64 `protobuf:"varint,4,opt,name=count,proto3" json:"count,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
get返回的信息会存储在RangeResponse结构体的Kvs字段,这是一个KeyValue结构体的指针类型切片,所以取值时要遍历这个切片。
package main import ( "context" "fmt" "time" "go.etcd.io/etcd/clientv3" ) // watch demo func main() { cli, err := clientv3.New(clientv3.Config{ Endpoints: []string{"127.0.0.1:2379"}, DialTimeout: 5 * time.Second, }) if err != nil { fmt.Printf("connect to etcd failed, err:%v\n", err) return } fmt.Println("connect to etcd success") defer cli.Close() // watch key:q1mi change rch := cli.Watch(context.Background(), "q1mi") // <-chan WatchResponse for wresp := range rch { for _, ev := range wresp.Events { fmt.Printf("Type: %s Key:%s Value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value) } } }
上述代码的逻辑步骤是:
// If the context is "context.Background/TODO", returned "WatchChan" will // not be closed and block until event is triggered, except when server // returns a non-recoverable error (e.g. ErrCompacted).
对返回值说明:
cli.Watch返回一个WatchResponse类型通道。
type WatchChan <-chan WatchResponse
type WatchResponse ¶
type WatchResponse struct { Header pb.ResponseHeader Events []*Event // CompactRevision is the minimum revision the watcher may receive. CompactRevision int64 // Canceled is used to indicate watch failure. // If the watch failed and the stream was about to close, before the channel is closed, // the channel sends a final response that has Canceled set to true with a non-nil Err(). Canceled bool // Created is used to indicate the creation of the watcher. Created bool // contains filtered or unexported fields }
返回值存储在WatchResponse例的Event字段中,这个一个指针类型的切片,所以取值时要遍历此切片。