serviceKV service Watch service Lease service Cluster service Maintenance service Auth ``` 定义了6个服务,服务里面有多个RPC方法,我们选一个最常用的KV来进行简单的分析吧。 首先看KV这个服务,代码如下 ```protobuf service KV { // Range gets the keys in the range from the key-value store. rpc Range(RangeRequest) returns (RangeResponse) { option (google.api.http) = { post: "/v3beta/kv/range" body: "*" }; }
// Put puts the given key into the key-value store. // A put request increments the revision of the key-value store // and generates one event in the event history. rpc Put(PutRequest) returns (PutResponse) { option (google.api.http) = { post: "/v3beta/kv/put" body: "*" }; }
// DeleteRange deletes the given range from the key-value store. // A delete request increments the revision of the key-value store // and generates a delete event in the event history for every deleted key. rpc DeleteRange(DeleteRangeRequest) returns (DeleteRangeResponse) { option (google.api.http) = { post: "/v3beta/kv/deleterange" body: "*" }; }
// Txn processes multiple requests in a single transaction. // A txn request increments the revision of the key-value store // and generates events with the same revision for every completed request. // It is not allowed to modify the same key several times within one txn. rpc Txn(TxnRequest) returns (TxnResponse) { option (google.api.http) = { post: "/v3beta/kv/txn" body: "*" }; }
// Compact compacts the event history in the etcd key-value store. The key-value // store should be periodically compacted or the event history will continue to grow // indefinitely. rpc Compact(CompactionRequest) returns (CompactionResponse) { option (google.api.http) = { post: "/v3beta/kv/compaction" body: "*" }; } }
// use Endpoints[0] so that for https:// without any tls config given, then // grpc will assume the certificate server name is the endpoint host. // 建立一个网络连接 conn, err := client.dial(cfg.Endpoints[0], grpc.WithBalancer(client.balancer)) if err != nil { client.cancel() client.balancer.Close() returnnil, err } client.conn = conn
type KV interface { // Put puts a key-value pair into etcd. // Note that key,value can be plain bytes array and string is // an immutable representation of that bytes array. // To get a string of bytes, do string([]byte{0x10, 0x20}). Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) .... }