├── .vscode └── settings.json ├── 2-etcd-go-client ├── README.md ├── example_maintenence_test.go ├── example_test.go ├── example_metrics_test.go ├── example_cluster_test.go ├── example_watch_test.go ├── example_auth_test.go ├── example_lease_test.go └── example_kv_test.go ├── README.md ├── 3-etcd-service-discovery ├── .vscode │ └── launch.json ├── register │ └── register.go ├── discovery │ └── discovery.go └── README.md ├── 4-etcd-grpclb ├── proto │ ├── simple.proto │ └── simple.pb.go ├── client │ └── client.go ├── server │ └── server.go ├── etcdv3 │ ├── register.go │ └── discovery.go └── README.md ├── 5-etcd-grpclb-balancer ├── proto │ ├── simple.proto │ └── simple.pb.go ├── client │ └── client.go ├── server │ └── server.go ├── etcdv3 │ ├── register.go │ └── discovery.go ├── balancer │ └── weight │ │ └── weight.go └── README.md ├── go.mod ├── 6-etcd-mutex ├── example_mutex_test.go └── README.md ├── 1-etcd环境安装与使用 └── README.md ├── LICENSE └── go.sum /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "go.inferGopath": false 3 | } -------------------------------------------------------------------------------- /2-etcd-go-client/README.md: -------------------------------------------------------------------------------- 1 | 代码来源:https://github.com/etcd-io/etcd/tree/master/clientv3 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 教程 2 | 3 | * [etcd环境安装与使用](https://www.cnblogs.com/FireworksEasyCool/p/12858570.html) 4 | 5 | * [etcd实现服务发现](https://www.cnblogs.com/FireworksEasyCool/p/12890649.html) 6 | 7 | * [gRPC负载均衡(客户端负载均衡)--基于etcd服务发现](https://www.cnblogs.com/FireworksEasyCool/p/12912839.html) 8 | 9 | * [gRPC负载均衡(自定义负载均衡策略)--基于etcd服务发现](https://www.cnblogs.com/FireworksEasyCool/p/12924701.html) 10 | 11 | * [etcd分布式锁及事务](https://www.cnblogs.com/FireworksEasyCool/p/12937882.html) -------------------------------------------------------------------------------- /3-etcd-service-discovery/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Launch", 9 | "type": "go", 10 | "request": "launch", 11 | "mode": "auto", 12 | "program": "${file}", 13 | "env": {}, 14 | "args": [] 15 | } 16 | ] 17 | } -------------------------------------------------------------------------------- /4-etcd-grpclb/proto/simple.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3";// 协议为proto3 2 | 3 | package proto; 4 | 5 | // 定义发送请求信息 6 | message SimpleRequest{ 7 | // 定义发送的参数,采用驼峰命名方式,小写加下划线,如:student_name 8 | // 参数类型 参数名 标识号(不可重复) 9 | string data = 1; 10 | } 11 | 12 | // 定义响应信息 13 | message SimpleResponse{ 14 | // 定义接收的参数 15 | // 参数类型 参数名 标识号(不可重复) 16 | int32 code = 1; 17 | string value = 2; 18 | } 19 | 20 | // 定义我们的服务(可定义多个服务,每个服务可定义多个接口) 21 | service Simple{ 22 | rpc Route (SimpleRequest) returns (SimpleResponse){}; 23 | } -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/proto/simple.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3";// 协议为proto3 2 | 3 | package proto; 4 | 5 | // 定义发送请求信息 6 | message SimpleRequest{ 7 | // 定义发送的参数,采用驼峰命名方式,小写加下划线,如:student_name 8 | // 参数类型 参数名 标识号(不可重复) 9 | string data = 1; 10 | } 11 | 12 | // 定义响应信息 13 | message SimpleResponse{ 14 | // 定义接收的参数 15 | // 参数类型 参数名 标识号(不可重复) 16 | int32 code = 1; 17 | string value = 2; 18 | } 19 | 20 | // 定义我们的服务(可定义多个服务,每个服务可定义多个接口) 21 | service Simple{ 22 | rpc Route (SimpleRequest) returns (SimpleResponse){}; 23 | } -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module etcd-example 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/coreos/etcd v3.3.20+incompatible 7 | github.com/coreos/go-semver v0.3.0 // indirect 8 | github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect 9 | github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect 10 | github.com/gogo/protobuf v1.3.1 // indirect 11 | github.com/golang/protobuf v1.4.1 12 | github.com/google/uuid v1.1.1 // indirect 13 | github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 14 | github.com/prometheus/client_golang v1.6.0 15 | go.etcd.io/etcd v3.3.20+incompatible 16 | go.uber.org/zap v1.15.0 // indirect 17 | golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect 18 | golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25 // indirect 19 | golang.org/x/text v0.3.2 // indirect 20 | google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 // indirect 21 | google.golang.org/grpc v1.29.1 22 | ) 23 | 24 | replace google.golang.org/grpc => google.golang.org/grpc v1.26.0 25 | -------------------------------------------------------------------------------- /4-etcd-grpclb/client/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "strconv" 8 | "time" 9 | 10 | "google.golang.org/grpc" 11 | "google.golang.org/grpc/resolver" 12 | 13 | "etcd-example/4-etcd-grpclb/etcdv3" 14 | pb "etcd-example/4-etcd-grpclb/proto" 15 | ) 16 | 17 | var ( 18 | // EtcdEndpoints etcd地址 19 | EtcdEndpoints = []string{"localhost:2379"} 20 | // SerName 服务名称 21 | SerName = "simple_grpc" 22 | grpcClient pb.SimpleClient 23 | ) 24 | 25 | func main() { 26 | r := etcdv3.NewServiceDiscovery(EtcdEndpoints) 27 | resolver.Register(r) 28 | // 连接服务器 29 | conn, err := grpc.Dial( 30 | fmt.Sprintf("%s:///%s", r.Scheme(), SerName), 31 | grpc.WithBalancerName("round_robin"), 32 | grpc.WithInsecure(), 33 | ) 34 | if err != nil { 35 | log.Fatalf("net.Connect err: %v", err) 36 | } 37 | defer conn.Close() 38 | 39 | // 建立gRPC连接 40 | grpcClient = pb.NewSimpleClient(conn) 41 | for i := 0; i < 100; i++ { 42 | route(i) 43 | time.Sleep(1 * time.Second) 44 | } 45 | 46 | } 47 | 48 | // route 调用服务端Route方法 49 | func route(i int) { 50 | // 创建发送结构体 51 | req := pb.SimpleRequest{ 52 | Data: "grpc " + strconv.Itoa(i), 53 | } 54 | // 调用我们的服务(Route方法) 55 | // 同时传入了一个 context.Context ,在有需要时可以让我们改变RPC的行为,比如超时/取消一个正在运行的RPC 56 | res, err := grpcClient.Route(context.Background(), &req) 57 | if err != nil { 58 | log.Fatalf("Call Route err: %v", err) 59 | } 60 | // 打印返回值 61 | log.Println(res) 62 | } 63 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/client/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "strconv" 8 | "time" 9 | 10 | "google.golang.org/grpc" 11 | "google.golang.org/grpc/resolver" 12 | 13 | "etcd-example/5-etcd-grpclb-balancer/etcdv3" 14 | pb "etcd-example/5-etcd-grpclb-balancer/proto" 15 | ) 16 | 17 | var ( 18 | // EtcdEndpoints etcd地址 19 | EtcdEndpoints = []string{"localhost:2379"} 20 | // SerName 服务名称 21 | SerName = "simple_grpc" 22 | grpcClient pb.SimpleClient 23 | ) 24 | 25 | func main() { 26 | r := etcdv3.NewServiceDiscovery(EtcdEndpoints) 27 | resolver.Register(r) 28 | // 连接服务器 29 | conn, err := grpc.Dial( 30 | fmt.Sprintf("%s:///%s", r.Scheme(), SerName), 31 | grpc.WithBalancerName("weight"), 32 | grpc.WithInsecure(), 33 | ) 34 | if err != nil { 35 | log.Fatalf("net.Connect err: %v", err) 36 | } 37 | defer conn.Close() 38 | 39 | // 建立gRPC连接 40 | grpcClient = pb.NewSimpleClient(conn) 41 | for i := 0; i < 100; i++ { 42 | route(i) 43 | time.Sleep(1 * time.Second) 44 | } 45 | 46 | } 47 | 48 | // route 调用服务端Route方法 49 | func route(i int) { 50 | // 创建发送结构体 51 | req := pb.SimpleRequest{ 52 | Data: "grpc " + strconv.Itoa(i), 53 | } 54 | // 调用我们的服务(Route方法) 55 | // 同时传入了一个 context.Context ,在有需要时可以让我们改变RPC的行为,比如超时/取消一个正在运行的RPC 56 | res, err := grpcClient.Route(context.Background(), &req) 57 | if err != nil { 58 | log.Fatalf("Call Route err: %v", err) 59 | } 60 | // 打印返回值 61 | log.Println(res) 62 | } 63 | -------------------------------------------------------------------------------- /4-etcd-grpclb/server/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net" 7 | 8 | "google.golang.org/grpc" 9 | 10 | "etcd-example/4-etcd-grpclb/etcdv3" 11 | pb "etcd-example/4-etcd-grpclb/proto" 12 | ) 13 | 14 | // SimpleService 定义我们的服务 15 | type SimpleService struct{} 16 | 17 | const ( 18 | // Address 监听地址 19 | Address string = "localhost:8000" 20 | // Network 网络通信协议 21 | Network string = "tcp" 22 | // SerName 服务名称 23 | SerName string = "simple_grpc" 24 | ) 25 | 26 | // EtcdEndpoints etcd地址 27 | var EtcdEndpoints = []string{"localhost:2379"} 28 | 29 | func main() { 30 | // 监听本地端口 31 | listener, err := net.Listen(Network, Address) 32 | if err != nil { 33 | log.Fatalf("net.Listen err: %v", err) 34 | } 35 | log.Println(Address + " net.Listing...") 36 | // 新建gRPC服务器实例 37 | grpcServer := grpc.NewServer() 38 | // 在gRPC服务器注册我们的服务 39 | pb.RegisterSimpleServer(grpcServer, &SimpleService{}) 40 | //把服务注册到etcd 41 | ser, err := etcdv3.NewServiceRegister(EtcdEndpoints, SerName, Address, 5) 42 | if err != nil { 43 | log.Fatalf("register service err: %v", err) 44 | } 45 | defer ser.Close() 46 | //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 47 | err = grpcServer.Serve(listener) 48 | if err != nil { 49 | log.Fatalf("grpcServer.Serve err: %v", err) 50 | } 51 | } 52 | 53 | // Route 实现Route方法 54 | func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { 55 | log.Println("receive: " + req.Data) 56 | res := pb.SimpleResponse{ 57 | Code: 200, 58 | Value: "hello " + req.Data, 59 | } 60 | return &res, nil 61 | } 62 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/server/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "net" 7 | 8 | "google.golang.org/grpc" 9 | 10 | "etcd-example/5-etcd-grpclb-balancer/etcdv3" 11 | pb "etcd-example/5-etcd-grpclb-balancer/proto" 12 | ) 13 | 14 | // SimpleService 定义我们的服务 15 | type SimpleService struct{} 16 | 17 | const ( 18 | // Address 监听地址 19 | Address string = "localhost:8000" 20 | // Network 网络通信协议 21 | Network string = "tcp" 22 | // SerName 服务名称 23 | SerName string = "simple_grpc" 24 | ) 25 | 26 | // EtcdEndpoints etcd地址 27 | var EtcdEndpoints = []string{"localhost:2379"} 28 | 29 | func main() { 30 | // 监听本地端口 31 | listener, err := net.Listen(Network, Address) 32 | if err != nil { 33 | log.Fatalf("net.Listen err: %v", err) 34 | } 35 | log.Println(Address + " net.Listing...") 36 | // 新建gRPC服务器实例 37 | grpcServer := grpc.NewServer() 38 | // 在gRPC服务器注册我们的服务 39 | pb.RegisterSimpleServer(grpcServer, &SimpleService{}) 40 | //把服务注册到etcd 41 | ser, err := etcdv3.NewServiceRegister(EtcdEndpoints, SerName+"/"+Address, "1", 5) 42 | if err != nil { 43 | log.Fatalf("register service err: %v", err) 44 | } 45 | defer ser.Close() 46 | //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 47 | err = grpcServer.Serve(listener) 48 | if err != nil { 49 | log.Fatalf("grpcServer.Serve err: %v", err) 50 | } 51 | } 52 | 53 | // Route 实现Route方法 54 | func (s *SimpleService) Route(ctx context.Context, req *pb.SimpleRequest) (*pb.SimpleResponse, error) { 55 | log.Println("receive: " + req.Data) 56 | res := pb.SimpleResponse{ 57 | Code: 200, 58 | Value: "hello " + req.Data, 59 | } 60 | return &res, nil 61 | } 62 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_maintenence_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | ) 24 | 25 | func ExampleMaintenance_status() { 26 | for _, ep := range endpoints { 27 | cli, err := clientv3.New(clientv3.Config{ 28 | Endpoints: []string{ep}, 29 | DialTimeout: dialTimeout, 30 | }) 31 | if err != nil { 32 | log.Fatal(err) 33 | } 34 | defer cli.Close() 35 | 36 | resp, err := cli.Status(context.Background(), ep) 37 | if err != nil { 38 | log.Fatal(err) 39 | } 40 | fmt.Printf("endpoint: %s / Leader: %v\n", ep, resp.Header.MemberId == resp.Leader) 41 | } 42 | // endpoint: localhost:2379 / Leader: false 43 | // endpoint: localhost:22379 / Leader: false 44 | // endpoint: localhost:32379 / Leader: true 45 | } 46 | 47 | func ExampleMaintenance_defragment() { 48 | for _, ep := range endpoints { 49 | cli, err := clientv3.New(clientv3.Config{ 50 | Endpoints: []string{ep}, 51 | DialTimeout: dialTimeout, 52 | }) 53 | if err != nil { 54 | log.Fatal(err) 55 | } 56 | defer cli.Close() 57 | 58 | if _, err = cli.Defragment(context.TODO(), ep); err != nil { 59 | log.Fatal(err) 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /6-etcd-mutex/example_mutex_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package concurrency_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | "github.com/coreos/etcd/clientv3/concurrency" 24 | ) 25 | 26 | func ExampleMutex_Lock() { 27 | cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints}) 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | defer cli.Close() 32 | 33 | // create two separate sessions for lock competition 34 | s1, err := concurrency.NewSession(cli) 35 | if err != nil { 36 | log.Fatal(err) 37 | } 38 | defer s1.Close() 39 | m1 := concurrency.NewMutex(s1, "/my-lock/") 40 | 41 | s2, err := concurrency.NewSession(cli) 42 | if err != nil { 43 | log.Fatal(err) 44 | } 45 | defer s2.Close() 46 | m2 := concurrency.NewMutex(s2, "/my-lock/") 47 | 48 | // acquire lock for s1 49 | if err := m1.Lock(context.TODO()); err != nil { 50 | log.Fatal(err) 51 | } 52 | fmt.Println("acquired lock for s1") 53 | 54 | m2Locked := make(chan struct{}) 55 | go func() { 56 | defer close(m2Locked) 57 | // wait until s1 is locks /my-lock/ 58 | if err := m2.Lock(context.TODO()); err != nil { 59 | log.Fatal(err) 60 | } 61 | }() 62 | 63 | if err := m1.Unlock(context.TODO()); err != nil { 64 | log.Fatal(err) 65 | } 66 | fmt.Println("released lock for s1") 67 | 68 | <-m2Locked 69 | fmt.Println("acquired lock for s2") 70 | 71 | // Output: 72 | // acquired lock for s1 73 | // released lock for s1 74 | // acquired lock for s2 75 | } 76 | -------------------------------------------------------------------------------- /4-etcd-grpclb/etcdv3/register.go: -------------------------------------------------------------------------------- 1 | package etcdv3 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "time" 7 | 8 | "go.etcd.io/etcd/clientv3" 9 | ) 10 | 11 | //ServiceRegister 创建租约注册服务 12 | type ServiceRegister struct { 13 | cli *clientv3.Client //etcd client 14 | leaseID clientv3.LeaseID //租约ID 15 | //租约keepalieve相应chan 16 | keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse 17 | key string //key 18 | val string //value 19 | } 20 | 21 | //NewServiceRegister 新建注册服务 22 | func NewServiceRegister(endpoints []string, serName, addr string, lease int64) (*ServiceRegister, error) { 23 | cli, err := clientv3.New(clientv3.Config{ 24 | Endpoints: endpoints, 25 | DialTimeout: 5 * time.Second, 26 | }) 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | 31 | ser := &ServiceRegister{ 32 | cli: cli, 33 | key: "/" + schema + "/" + serName + "/" + addr, 34 | val: addr, 35 | } 36 | 37 | //申请租约设置时间keepalive 38 | if err := ser.putKeyWithLease(lease); err != nil { 39 | return nil, err 40 | } 41 | 42 | return ser, nil 43 | } 44 | 45 | //设置租约 46 | func (s *ServiceRegister) putKeyWithLease(lease int64) error { 47 | //设置租约时间 48 | resp, err := s.cli.Grant(context.Background(), lease) 49 | if err != nil { 50 | return err 51 | } 52 | //注册服务并绑定租约 53 | _, err = s.cli.Put(context.Background(), s.key, s.val, clientv3.WithLease(resp.ID)) 54 | if err != nil { 55 | return err 56 | } 57 | //设置续租 定期发送需求请求 58 | leaseRespChan, err := s.cli.KeepAlive(context.Background(), resp.ID) 59 | 60 | if err != nil { 61 | return err 62 | } 63 | s.leaseID = resp.ID 64 | s.keepAliveChan = leaseRespChan 65 | log.Printf("Put key:%s val:%s success!", s.key, s.val) 66 | return nil 67 | } 68 | 69 | //ListenLeaseRespChan 监听 续租情况 70 | func (s *ServiceRegister) ListenLeaseRespChan() { 71 | for leaseKeepResp := range s.keepAliveChan { 72 | log.Println("续约成功", leaseKeepResp) 73 | } 74 | log.Println("关闭续租") 75 | } 76 | 77 | // Close 注销服务 78 | func (s *ServiceRegister) Close() error { 79 | //撤销租约 80 | if _, err := s.cli.Revoke(context.Background(), s.leaseID); err != nil { 81 | return err 82 | } 83 | log.Println("撤销租约") 84 | return s.cli.Close() 85 | } 86 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/etcdv3/register.go: -------------------------------------------------------------------------------- 1 | package etcdv3 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "time" 7 | 8 | "go.etcd.io/etcd/clientv3" 9 | ) 10 | 11 | //ServiceRegister 创建租约注册服务 12 | type ServiceRegister struct { 13 | cli *clientv3.Client //etcd client 14 | leaseID clientv3.LeaseID //租约ID 15 | //租约keepalieve相应chan 16 | keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse 17 | key string //key 18 | weight string //value 19 | } 20 | 21 | //NewServiceRegister 新建注册服务 22 | func NewServiceRegister(endpoints []string, addr, weigit string, lease int64) (*ServiceRegister, error) { 23 | cli, err := clientv3.New(clientv3.Config{ 24 | Endpoints: endpoints, 25 | DialTimeout: 5 * time.Second, 26 | }) 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | 31 | ser := &ServiceRegister{ 32 | cli: cli, 33 | key: "/" + schema + "/" + addr, 34 | weight: weigit, 35 | } 36 | 37 | //申请租约设置时间keepalive 38 | if err := ser.putKeyWithLease(lease); err != nil { 39 | return nil, err 40 | } 41 | 42 | return ser, nil 43 | } 44 | 45 | //设置租约 46 | func (s *ServiceRegister) putKeyWithLease(lease int64) error { 47 | //设置租约时间 48 | resp, err := s.cli.Grant(context.Background(), lease) 49 | if err != nil { 50 | return err 51 | } 52 | //注册服务并绑定租约 53 | _, err = s.cli.Put(context.Background(), s.key, s.weight, clientv3.WithLease(resp.ID)) 54 | if err != nil { 55 | return err 56 | } 57 | //设置续租 定期发送需求请求 58 | leaseRespChan, err := s.cli.KeepAlive(context.Background(), resp.ID) 59 | 60 | if err != nil { 61 | return err 62 | } 63 | s.leaseID = resp.ID 64 | s.keepAliveChan = leaseRespChan 65 | log.Printf("Put key:%s weight:%s success!", s.key, s.weight) 66 | return nil 67 | } 68 | 69 | //ListenLeaseRespChan 监听 续租情况 70 | func (s *ServiceRegister) ListenLeaseRespChan() { 71 | for leaseKeepResp := range s.keepAliveChan { 72 | log.Println("续约成功", leaseKeepResp) 73 | } 74 | log.Println("关闭续租") 75 | } 76 | 77 | // Close 注销服务 78 | func (s *ServiceRegister) Close() error { 79 | //撤销租约 80 | if _, err := s.cli.Revoke(context.Background(), s.leaseID); err != nil { 81 | return err 82 | } 83 | log.Println("撤销租约") 84 | return s.cli.Close() 85 | } 86 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "log" 20 | "os" 21 | "time" 22 | 23 | "github.com/coreos/etcd/clientv3" 24 | "github.com/coreos/etcd/pkg/transport" 25 | 26 | "google.golang.org/grpc/grpclog" 27 | ) 28 | 29 | var ( 30 | dialTimeout = 5 * time.Second 31 | requestTimeout = 10 * time.Second 32 | endpoints = []string{"localhost:2379"} 33 | //endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"} 34 | ) 35 | 36 | func Example() { 37 | clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr)) 38 | 39 | cli, err := clientv3.New(clientv3.Config{ 40 | Endpoints: endpoints, 41 | DialTimeout: dialTimeout, 42 | }) 43 | if err != nil { 44 | log.Fatal(err) 45 | } 46 | defer cli.Close() // make sure to close the client 47 | 48 | _, err = cli.Put(context.TODO(), "foo", "bar") 49 | if err != nil { 50 | log.Fatal(err) 51 | } 52 | } 53 | 54 | func ExampleConfig_withTLS() { 55 | tlsInfo := transport.TLSInfo{ 56 | CertFile: "/tmp/test-certs/test-name-1.pem", 57 | KeyFile: "/tmp/test-certs/test-name-1-key.pem", 58 | TrustedCAFile: "/tmp/test-certs/trusted-ca.pem", 59 | } 60 | tlsConfig, err := tlsInfo.ClientConfig() 61 | if err != nil { 62 | log.Fatal(err) 63 | } 64 | cli, err := clientv3.New(clientv3.Config{ 65 | Endpoints: endpoints, 66 | DialTimeout: dialTimeout, 67 | TLS: tlsConfig, 68 | }) 69 | if err != nil { 70 | log.Fatal(err) 71 | } 72 | defer cli.Close() // make sure to close the client 73 | 74 | _, err = cli.Put(context.TODO(), "foo", "bar") 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_metrics_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "io/ioutil" 21 | "log" 22 | "net" 23 | "net/http" 24 | "strings" 25 | 26 | "github.com/coreos/etcd/clientv3" 27 | 28 | grpcprom "github.com/grpc-ecosystem/go-grpc-prometheus" 29 | "github.com/prometheus/client_golang/prometheus/promhttp" 30 | "google.golang.org/grpc" 31 | ) 32 | 33 | func ExampleClient_metrics() { 34 | cli, err := clientv3.New(clientv3.Config{ 35 | Endpoints: endpoints, 36 | DialOptions: []grpc.DialOption{ 37 | grpc.WithUnaryInterceptor(grpcprom.UnaryClientInterceptor), 38 | grpc.WithStreamInterceptor(grpcprom.StreamClientInterceptor), 39 | }, 40 | }) 41 | if err != nil { 42 | log.Fatal(err) 43 | } 44 | defer cli.Close() 45 | 46 | // get a key so it shows up in the metrics as a range RPC 47 | cli.Get(context.TODO(), "test_key") 48 | 49 | // listen for all Prometheus metrics 50 | ln, err := net.Listen("tcp", ":0") 51 | if err != nil { 52 | log.Fatal(err) 53 | } 54 | donec := make(chan struct{}) 55 | go func() { 56 | defer close(donec) 57 | http.Serve(ln, promhttp.Handler()) 58 | }() 59 | defer func() { 60 | ln.Close() 61 | <-donec 62 | }() 63 | 64 | // make an http request to fetch all Prometheus metrics 65 | url := "http://" + ln.Addr().String() + "/metrics" 66 | resp, err := http.Get(url) 67 | if err != nil { 68 | log.Fatalf("fetch error: %v", err) 69 | } 70 | b, err := ioutil.ReadAll(resp.Body) 71 | resp.Body.Close() 72 | if err != nil { 73 | log.Fatalf("fetch error: reading %s: %v", url, err) 74 | } 75 | 76 | // confirm range request in metrics 77 | for _, l := range strings.Split(string(b), "\n") { 78 | if strings.Contains(l, `grpc_client_started_total{grpc_method="Range"`) { 79 | fmt.Println(l) 80 | break 81 | } 82 | } 83 | // Output: 84 | // grpc_client_started_total{grpc_method="Range",grpc_service="etcdserverpb.KV",grpc_type="unary"} 1 85 | } 86 | -------------------------------------------------------------------------------- /3-etcd-service-discovery/register/register.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "time" 7 | 8 | "go.etcd.io/etcd/clientv3" 9 | ) 10 | 11 | //ServiceRegister 创建租约注册服务 12 | type ServiceRegister struct { 13 | cli *clientv3.Client //etcd client 14 | leaseID clientv3.LeaseID //租约ID 15 | //租约keepalieve相应chan 16 | keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse 17 | key string //key 18 | val string //value 19 | } 20 | 21 | //NewServiceRegister 新建注册服务 22 | func NewServiceRegister(endpoints []string, key, val string, lease int64) (*ServiceRegister, error) { 23 | cli, err := clientv3.New(clientv3.Config{ 24 | Endpoints: endpoints, 25 | DialTimeout: 5 * time.Second, 26 | }) 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | 31 | ser := &ServiceRegister{ 32 | cli: cli, 33 | key: key, 34 | val: val, 35 | } 36 | 37 | //申请租约设置时间keepalive 38 | if err := ser.putKeyWithLease(lease); err != nil { 39 | return nil, err 40 | } 41 | 42 | return ser, nil 43 | } 44 | 45 | //设置租约 46 | func (s *ServiceRegister) putKeyWithLease(lease int64) error { 47 | //设置租约时间 48 | resp, err := s.cli.Grant(context.Background(), lease) 49 | if err != nil { 50 | return err 51 | } 52 | //注册服务并绑定租约 53 | _, err = s.cli.Put(context.Background(), s.key, s.val, clientv3.WithLease(resp.ID)) 54 | if err != nil { 55 | return err 56 | } 57 | //设置续租 定期发送需求请求 58 | leaseRespChan, err := s.cli.KeepAlive(context.Background(), resp.ID) 59 | 60 | if err != nil { 61 | return err 62 | } 63 | s.leaseID = resp.ID 64 | s.keepAliveChan = leaseRespChan 65 | log.Printf("Put key:%s val:%s success!", s.key, s.val) 66 | return nil 67 | } 68 | 69 | //ListenLeaseRespChan 监听 续租情况 70 | func (s *ServiceRegister) ListenLeaseRespChan() { 71 | for leaseKeepResp := range s.keepAliveChan { 72 | log.Println("续约成功", leaseKeepResp) 73 | } 74 | log.Println("关闭续租") 75 | } 76 | 77 | // Close 注销服务 78 | func (s *ServiceRegister) Close() error { 79 | //撤销租约 80 | if _, err := s.cli.Revoke(context.Background(), s.leaseID); err != nil { 81 | return err 82 | } 83 | log.Println("撤销租约") 84 | return s.cli.Close() 85 | } 86 | 87 | func main() { 88 | var endpoints = []string{"localhost:2379"} 89 | ser, err := NewServiceRegister(endpoints, "/web/node1", "localhost:8000", 5) 90 | if err != nil { 91 | log.Fatalln(err) 92 | } 93 | //监听续租相应chan 94 | go ser.ListenLeaseRespChan() 95 | select { 96 | // case <-time.After(20 * time.Second): 97 | // ser.Close() 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /3-etcd-service-discovery/discovery/discovery.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "sync" 7 | "time" 8 | 9 | "github.com/coreos/etcd/mvcc/mvccpb" 10 | "go.etcd.io/etcd/clientv3" 11 | ) 12 | 13 | //ServiceDiscovery 服务发现 14 | type ServiceDiscovery struct { 15 | cli *clientv3.Client //etcd client 16 | serverList sync.Map 17 | } 18 | 19 | //NewServiceDiscovery 新建发现服务 20 | func NewServiceDiscovery(endpoints []string) *ServiceDiscovery { 21 | cli, err := clientv3.New(clientv3.Config{ 22 | Endpoints: endpoints, 23 | DialTimeout: 5 * time.Second, 24 | }) 25 | if err != nil { 26 | log.Fatal(err) 27 | } 28 | 29 | return &ServiceDiscovery{ 30 | cli: cli, 31 | } 32 | } 33 | 34 | //WatchService 初始化服务列表和监视 35 | func (s *ServiceDiscovery) WatchService(prefix string) error { 36 | //根据前缀获取现有的key 37 | resp, err := s.cli.Get(context.Background(), prefix, clientv3.WithPrefix()) 38 | if err != nil { 39 | return err 40 | } 41 | 42 | for _, ev := range resp.Kvs { 43 | s.SetServiceList(string(ev.Key), string(ev.Value)) 44 | } 45 | 46 | //监视前缀,修改变更的server 47 | go s.watcher(prefix) 48 | return nil 49 | } 50 | 51 | //watcher 监听前缀 52 | func (s *ServiceDiscovery) watcher(prefix string) { 53 | rch := s.cli.Watch(context.Background(), prefix, clientv3.WithPrefix()) 54 | log.Printf("watching prefix:%s now...", prefix) 55 | for wresp := range rch { 56 | for _, ev := range wresp.Events { 57 | switch ev.Type { 58 | case mvccpb.PUT: //修改或者新增 59 | s.SetServiceList(string(ev.Kv.Key), string(ev.Kv.Value)) 60 | case mvccpb.DELETE: //删除 61 | s.DelServiceList(string(ev.Kv.Key)) 62 | } 63 | } 64 | } 65 | } 66 | 67 | //SetServiceList 新增服务地址 68 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 69 | s.serverList.Store(key, val) 70 | log.Println("put key :", key, "val:", val) 71 | } 72 | 73 | //DelServiceList 删除服务地址 74 | func (s *ServiceDiscovery) DelServiceList(key string) { 75 | s.serverList.Delete(key) 76 | log.Println("del key:", key) 77 | } 78 | 79 | //GetServices 获取服务地址 80 | func (s *ServiceDiscovery) GetServices() []string { 81 | addrs := make([]string, 0, 10) 82 | s.serverList.Range(func(k, v interface{}) bool { 83 | addrs = append(addrs, v.(string)) 84 | return true 85 | }) 86 | return addrs 87 | } 88 | 89 | //Close 关闭服务 90 | func (s *ServiceDiscovery) Close() error { 91 | return s.cli.Close() 92 | } 93 | 94 | func main() { 95 | var endpoints = []string{"localhost:2379"} 96 | ser := NewServiceDiscovery(endpoints) 97 | defer ser.Close() 98 | ser.WatchService("/web/") 99 | ser.WatchService("/gRPC/") 100 | for { 101 | select { 102 | case <-time.Tick(10 * time.Second): 103 | log.Println(ser.GetServices()) 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/balancer/weight/weight.go: -------------------------------------------------------------------------------- 1 | package weight 2 | 3 | import ( 4 | "math/rand" 5 | "sync" 6 | 7 | "google.golang.org/grpc/attributes" 8 | "google.golang.org/grpc/balancer" 9 | "google.golang.org/grpc/balancer/base" 10 | "google.golang.org/grpc/grpclog" 11 | "google.golang.org/grpc/resolver" 12 | ) 13 | 14 | // Name is the name of weight balancer. 15 | const Name = "weight" 16 | 17 | var ( 18 | minWeight = 1 19 | maxWeight = 5 20 | ) 21 | 22 | // attributeKey is the type used as the key to store AddrInfo in the Attributes 23 | // field of resolver.Address. 24 | type attributeKey struct{} 25 | 26 | // AddrInfo will be stored inside Address metadata in order to use weighted balancer. 27 | type AddrInfo struct { 28 | Weight int 29 | } 30 | 31 | // SetAddrInfo returns a copy of addr in which the Attributes field is updated 32 | // with addrInfo. 33 | func SetAddrInfo(addr resolver.Address, addrInfo AddrInfo) resolver.Address { 34 | addr.Attributes = attributes.New() 35 | addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo) 36 | return addr 37 | } 38 | 39 | // GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr. 40 | func GetAddrInfo(addr resolver.Address) AddrInfo { 41 | v := addr.Attributes.Value(attributeKey{}) 42 | ai, _ := v.(AddrInfo) 43 | return ai 44 | } 45 | 46 | // NewBuilder creates a new weight balancer builder. 47 | func newBuilder() balancer.Builder { 48 | return base.NewBalancerBuilderV2(Name, &rrPickerBuilder{}, base.Config{HealthCheck: false}) 49 | } 50 | 51 | func init() { 52 | balancer.Register(newBuilder()) 53 | } 54 | 55 | type rrPickerBuilder struct{} 56 | 57 | func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.V2Picker { 58 | grpclog.Infof("weightPicker: newPicker called with info: %v", info) 59 | if len(info.ReadySCs) == 0 { 60 | return base.NewErrPickerV2(balancer.ErrNoSubConnAvailable) 61 | } 62 | var scs []balancer.SubConn 63 | for subConn, addr := range info.ReadySCs { 64 | node := GetAddrInfo(addr.Address) 65 | if node.Weight <= 0 { 66 | node.Weight = minWeight 67 | } else if node.Weight > 5 { 68 | node.Weight = maxWeight 69 | } 70 | for i := 0; i < node.Weight; i++ { 71 | scs = append(scs, subConn) 72 | } 73 | } 74 | return &rrPicker{ 75 | subConns: scs, 76 | } 77 | } 78 | 79 | type rrPicker struct { 80 | // subConns is the snapshot of the roundrobin balancer when this picker was 81 | // created. The slice is immutable. Each Get() will do a round robin 82 | // selection from it and return the selected SubConn. 83 | subConns []balancer.SubConn 84 | 85 | mu sync.Mutex 86 | } 87 | 88 | func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { 89 | p.mu.Lock() 90 | index := rand.Intn(len(p.subConns)) 91 | sc := p.subConns[index] 92 | p.mu.Unlock() 93 | return balancer.PickResult{SubConn: sc}, nil 94 | } 95 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_cluster_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | ) 24 | 25 | func ExampleCluster_memberList() { 26 | cli, err := clientv3.New(clientv3.Config{ 27 | Endpoints: endpoints, 28 | DialTimeout: dialTimeout, 29 | }) 30 | if err != nil { 31 | log.Fatal(err) 32 | } 33 | defer cli.Close() 34 | 35 | resp, err := cli.MemberList(context.Background()) 36 | if err != nil { 37 | log.Fatal(err) 38 | } 39 | fmt.Println("members:", len(resp.Members)) 40 | // Output: members: 3 41 | } 42 | 43 | func ExampleCluster_memberAdd() { 44 | cli, err := clientv3.New(clientv3.Config{ 45 | Endpoints: endpoints[:2], 46 | DialTimeout: dialTimeout, 47 | }) 48 | if err != nil { 49 | log.Fatal(err) 50 | } 51 | defer cli.Close() 52 | 53 | peerURLs := endpoints[2:] 54 | mresp, err := cli.MemberAdd(context.Background(), peerURLs) 55 | if err != nil { 56 | log.Fatal(err) 57 | } 58 | fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs) 59 | // added member.PeerURLs: [http://localhost:32380] 60 | } 61 | 62 | func ExampleCluster_memberRemove() { 63 | cli, err := clientv3.New(clientv3.Config{ 64 | Endpoints: endpoints[1:], 65 | DialTimeout: dialTimeout, 66 | }) 67 | if err != nil { 68 | log.Fatal(err) 69 | } 70 | defer cli.Close() 71 | 72 | resp, err := cli.MemberList(context.Background()) 73 | if err != nil { 74 | log.Fatal(err) 75 | } 76 | 77 | _, err = cli.MemberRemove(context.Background(), resp.Members[0].ID) 78 | if err != nil { 79 | log.Fatal(err) 80 | } 81 | } 82 | 83 | func ExampleCluster_memberUpdate() { 84 | cli, err := clientv3.New(clientv3.Config{ 85 | Endpoints: endpoints, 86 | DialTimeout: dialTimeout, 87 | }) 88 | if err != nil { 89 | log.Fatal(err) 90 | } 91 | defer cli.Close() 92 | 93 | resp, err := cli.MemberList(context.Background()) 94 | if err != nil { 95 | log.Fatal(err) 96 | } 97 | 98 | peerURLs := []string{"http://localhost:12380"} 99 | _, err = cli.MemberUpdate(context.Background(), resp.Members[0].ID, peerURLs) 100 | if err != nil { 101 | log.Fatal(err) 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_watch_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | ) 24 | 25 | func ExampleWatcher_watch() { 26 | cli, err := clientv3.New(clientv3.Config{ 27 | Endpoints: endpoints, 28 | DialTimeout: dialTimeout, 29 | }) 30 | if err != nil { 31 | log.Fatal(err) 32 | } 33 | defer cli.Close() 34 | 35 | rch := cli.Watch(context.Background(), "foo") 36 | for wresp := range rch { 37 | for _, ev := range wresp.Events { 38 | fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value) 39 | } 40 | } 41 | // PUT "foo" : "bar" 42 | } 43 | 44 | func ExampleWatcher_watchWithPrefix() { 45 | cli, err := clientv3.New(clientv3.Config{ 46 | Endpoints: endpoints, 47 | DialTimeout: dialTimeout, 48 | }) 49 | if err != nil { 50 | log.Fatal(err) 51 | } 52 | defer cli.Close() 53 | 54 | rch := cli.Watch(context.Background(), "foo", clientv3.WithPrefix()) 55 | for wresp := range rch { 56 | for _, ev := range wresp.Events { 57 | fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value) 58 | } 59 | } 60 | // PUT "foo1" : "bar" 61 | } 62 | 63 | func ExampleWatcher_watchWithRange() { 64 | cli, err := clientv3.New(clientv3.Config{ 65 | Endpoints: endpoints, 66 | DialTimeout: dialTimeout, 67 | }) 68 | if err != nil { 69 | log.Fatal(err) 70 | } 71 | defer cli.Close() 72 | 73 | // watches within ['foo1', 'foo4'), in lexicographical order 74 | rch := cli.Watch(context.Background(), "foo1", clientv3.WithRange("foo4")) 75 | for wresp := range rch { 76 | for _, ev := range wresp.Events { 77 | fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value) 78 | } 79 | } 80 | // PUT "foo1" : "bar" 81 | // PUT "foo2" : "bar" 82 | // PUT "foo3" : "bar" 83 | } 84 | 85 | func ExampleWatcher_watchWithProgressNotify() { 86 | cli, err := clientv3.New(clientv3.Config{ 87 | Endpoints: endpoints, 88 | DialTimeout: dialTimeout, 89 | }) 90 | if err != nil { 91 | log.Fatal(err) 92 | } 93 | 94 | rch := cli.Watch(context.Background(), "foo", clientv3.WithProgressNotify()) 95 | wresp := <-rch 96 | fmt.Printf("wresp.Header.Revision: %d\n", wresp.Header.Revision) 97 | fmt.Println("wresp.IsProgressNotify:", wresp.IsProgressNotify()) 98 | // wresp.Header.Revision: 0 99 | // wresp.IsProgressNotify: true 100 | } 101 | -------------------------------------------------------------------------------- /4-etcd-grpclb/etcdv3/discovery.go: -------------------------------------------------------------------------------- 1 | package etcdv3 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "sync" 7 | "time" 8 | 9 | "github.com/coreos/etcd/mvcc/mvccpb" 10 | "go.etcd.io/etcd/clientv3" 11 | "google.golang.org/grpc/resolver" 12 | ) 13 | 14 | const schema = "grpclb" 15 | 16 | //ServiceDiscovery 服务发现 17 | type ServiceDiscovery struct { 18 | cli *clientv3.Client //etcd client 19 | cc resolver.ClientConn 20 | serverList sync.Map //服务列表 21 | } 22 | 23 | //NewServiceDiscovery 新建发现服务 24 | func NewServiceDiscovery(endpoints []string) resolver.Builder { 25 | cli, err := clientv3.New(clientv3.Config{ 26 | Endpoints: endpoints, 27 | DialTimeout: 5 * time.Second, 28 | }) 29 | if err != nil { 30 | log.Fatal(err) 31 | } 32 | 33 | return &ServiceDiscovery{ 34 | cli: cli, 35 | } 36 | } 37 | 38 | //Build 为给定目标创建一个新的`resolver`,当调用`grpc.Dial()`时执行 39 | func (s *ServiceDiscovery) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { 40 | log.Println("Build") 41 | s.cc = cc 42 | prefix := "/" + target.Scheme + "/" + target.Endpoint + "/" 43 | //根据前缀获取现有的key 44 | resp, err := s.cli.Get(context.Background(), prefix, clientv3.WithPrefix()) 45 | if err != nil { 46 | return nil, err 47 | } 48 | 49 | for _, ev := range resp.Kvs { 50 | s.SetServiceList(string(ev.Key), string(ev.Value)) 51 | } 52 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 53 | //监视前缀,修改变更的server 54 | go s.watcher(prefix) 55 | return s, nil 56 | } 57 | 58 | // ResolveNow 监视目标更新 59 | func (s *ServiceDiscovery) ResolveNow(rn resolver.ResolveNowOption) { 60 | log.Println("ResolveNow") 61 | } 62 | 63 | //Scheme return schema 64 | func (s *ServiceDiscovery) Scheme() string { 65 | return schema 66 | } 67 | 68 | //Close 关闭 69 | func (s *ServiceDiscovery) Close() { 70 | log.Println("Close") 71 | s.cli.Close() 72 | } 73 | 74 | //watcher 监听前缀 75 | func (s *ServiceDiscovery) watcher(prefix string) { 76 | rch := s.cli.Watch(context.Background(), prefix, clientv3.WithPrefix()) 77 | log.Printf("watching prefix:%s now...", prefix) 78 | for wresp := range rch { 79 | for _, ev := range wresp.Events { 80 | switch ev.Type { 81 | case mvccpb.PUT: //新增或修改 82 | s.SetServiceList(string(ev.Kv.Key), string(ev.Kv.Value)) 83 | case mvccpb.DELETE: //删除 84 | s.DelServiceList(string(ev.Kv.Key)) 85 | } 86 | } 87 | } 88 | } 89 | 90 | //SetServiceList 新增服务地址 91 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 92 | s.serverList.Store(key, resolver.Address{Addr: val}) 93 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 94 | log.Println("put key :", key, "val:", val) 95 | } 96 | 97 | //DelServiceList 删除服务地址 98 | func (s *ServiceDiscovery) DelServiceList(key string) { 99 | s.serverList.Delete(key) 100 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 101 | log.Println("del key:", key) 102 | } 103 | 104 | //GetServices 获取服务地址 105 | func (s *ServiceDiscovery) getServices() []resolver.Address { 106 | addrs := make([]resolver.Address, 0, 10) 107 | s.serverList.Range(func(k, v interface{}) bool { 108 | addrs = append(addrs, v.(resolver.Address)) 109 | return true 110 | }) 111 | return addrs 112 | } 113 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_auth_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | ) 24 | 25 | func ExampleAuth() { 26 | cli, err := clientv3.New(clientv3.Config{ 27 | Endpoints: endpoints, 28 | DialTimeout: dialTimeout, 29 | }) 30 | if err != nil { 31 | log.Fatal(err) 32 | } 33 | defer cli.Close() 34 | 35 | if _, err = cli.RoleAdd(context.TODO(), "root"); err != nil { 36 | log.Fatal(err) 37 | } 38 | if _, err = cli.UserAdd(context.TODO(), "root", "123"); err != nil { 39 | log.Fatal(err) 40 | } 41 | if _, err = cli.UserGrantRole(context.TODO(), "root", "root"); err != nil { 42 | log.Fatal(err) 43 | } 44 | 45 | if _, err = cli.RoleAdd(context.TODO(), "r"); err != nil { 46 | log.Fatal(err) 47 | } 48 | 49 | if _, err = cli.RoleGrantPermission( 50 | context.TODO(), 51 | "r", // role name 52 | "foo", // key 53 | "zoo", // range end 54 | clientv3.PermissionType(clientv3.PermReadWrite), 55 | ); err != nil { 56 | log.Fatal(err) 57 | } 58 | if _, err = cli.UserAdd(context.TODO(), "u", "123"); err != nil { 59 | log.Fatal(err) 60 | } 61 | if _, err = cli.UserGrantRole(context.TODO(), "u", "r"); err != nil { 62 | log.Fatal(err) 63 | } 64 | if _, err = cli.AuthEnable(context.TODO()); err != nil { 65 | log.Fatal(err) 66 | } 67 | 68 | cliAuth, err := clientv3.New(clientv3.Config{ 69 | Endpoints: endpoints, 70 | DialTimeout: dialTimeout, 71 | Username: "u", 72 | Password: "123", 73 | }) 74 | if err != nil { 75 | log.Fatal(err) 76 | } 77 | defer cliAuth.Close() 78 | 79 | if _, err = cliAuth.Put(context.TODO(), "foo1", "bar"); err != nil { 80 | log.Fatal(err) 81 | } 82 | 83 | _, err = cliAuth.Txn(context.TODO()). 84 | If(clientv3.Compare(clientv3.Value("zoo1"), ">", "abc")). 85 | Then(clientv3.OpPut("zoo1", "XYZ")). 86 | Else(clientv3.OpPut("zoo1", "ABC")). 87 | Commit() 88 | fmt.Println(err) 89 | 90 | // now check the permission with the root account 91 | rootCli, err := clientv3.New(clientv3.Config{ 92 | Endpoints: endpoints, 93 | DialTimeout: dialTimeout, 94 | Username: "root", 95 | Password: "123", 96 | }) 97 | if err != nil { 98 | log.Fatal(err) 99 | } 100 | defer rootCli.Close() 101 | 102 | resp, err := rootCli.RoleGet(context.TODO(), "r") 103 | if err != nil { 104 | log.Fatal(err) 105 | } 106 | fmt.Printf("user u permission: key %q, range end %q\n", resp.Perm[0].Key, resp.Perm[0].RangeEnd) 107 | 108 | if _, err = rootCli.AuthDisable(context.TODO()); err != nil { 109 | log.Fatal(err) 110 | } 111 | // Output: etcdserver: permission denied 112 | // user u permission: key "foo", range end "zoo" 113 | } 114 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/etcdv3/discovery.go: -------------------------------------------------------------------------------- 1 | package etcdv3 2 | 3 | import ( 4 | "context" 5 | "log" 6 | "strconv" 7 | "strings" 8 | "sync" 9 | "time" 10 | 11 | "etcd-example/5-etcd-grpclb-balancer/balancer/weight" 12 | 13 | "github.com/coreos/etcd/mvcc/mvccpb" 14 | "go.etcd.io/etcd/clientv3" 15 | "google.golang.org/grpc/resolver" 16 | ) 17 | 18 | const schema = "grpclb" 19 | 20 | //ServiceDiscovery 服务发现 21 | type ServiceDiscovery struct { 22 | cli *clientv3.Client //etcd client 23 | cc resolver.ClientConn 24 | serverList sync.Map //服务列表 25 | prefix string //监视的前缀 26 | } 27 | 28 | //NewServiceDiscovery 新建发现服务 29 | func NewServiceDiscovery(endpoints []string) resolver.Builder { 30 | cli, err := clientv3.New(clientv3.Config{ 31 | Endpoints: endpoints, 32 | DialTimeout: 5 * time.Second, 33 | }) 34 | if err != nil { 35 | log.Fatal(err) 36 | } 37 | 38 | return &ServiceDiscovery{ 39 | cli: cli, 40 | } 41 | } 42 | 43 | //Build 为给定目标创建一个新的`resolver`,当调用`grpc.Dial()`时执行 44 | func (s *ServiceDiscovery) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { 45 | log.Println("Build") 46 | s.cc = cc 47 | s.prefix = "/" + target.Scheme + "/" + target.Endpoint + "/" 48 | //根据前缀获取现有的key 49 | resp, err := s.cli.Get(context.Background(), s.prefix, clientv3.WithPrefix()) 50 | if err != nil { 51 | return nil, err 52 | } 53 | 54 | for _, ev := range resp.Kvs { 55 | s.SetServiceList(string(ev.Key), string(ev.Value)) 56 | } 57 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 58 | //监视前缀,修改变更的server 59 | go s.watcher() 60 | return s, nil 61 | } 62 | 63 | // ResolveNow 监视目标更新 64 | func (s *ServiceDiscovery) ResolveNow(rn resolver.ResolveNowOption) { 65 | log.Println("ResolveNow") 66 | } 67 | 68 | //Scheme return schema 69 | func (s *ServiceDiscovery) Scheme() string { 70 | return schema 71 | } 72 | 73 | //Close 关闭 74 | func (s *ServiceDiscovery) Close() { 75 | log.Println("Close") 76 | s.cli.Close() 77 | } 78 | 79 | //watcher 监听前缀 80 | func (s *ServiceDiscovery) watcher() { 81 | rch := s.cli.Watch(context.Background(), s.prefix, clientv3.WithPrefix()) 82 | log.Printf("watching prefix:%s now...", s.prefix) 83 | for wresp := range rch { 84 | for _, ev := range wresp.Events { 85 | switch ev.Type { 86 | case mvccpb.PUT: //新增或修改 87 | s.SetServiceList(string(ev.Kv.Key), string(ev.Kv.Value)) 88 | case mvccpb.DELETE: //删除 89 | s.DelServiceList(string(ev.Kv.Key)) 90 | } 91 | } 92 | } 93 | } 94 | 95 | //SetServiceList 设置服务地址 96 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 97 | //获取服务地址 98 | addr := resolver.Address{Addr: strings.TrimPrefix(key, s.prefix)} 99 | //获取服务地址权重 100 | nodeWeight, err := strconv.Atoi(val) 101 | if err != nil { 102 | //非数字字符默认权重为1 103 | nodeWeight = 1 104 | } 105 | //把服务地址权重存储到resolver.Address的元数据中 106 | addr = weight.SetAddrInfo(addr, weight.AddrInfo{Weight: nodeWeight}) 107 | s.serverList.Store(key, addr) 108 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 109 | log.Println("put key :", key, "wieght:", val) 110 | } 111 | 112 | //DelServiceList 删除服务地址 113 | func (s *ServiceDiscovery) DelServiceList(key string) { 114 | s.serverList.Delete(key) 115 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 116 | log.Println("del key:", key) 117 | } 118 | 119 | //GetServices 获取服务地址 120 | func (s *ServiceDiscovery) getServices() []resolver.Address { 121 | addrs := make([]resolver.Address, 0, 10) 122 | s.serverList.Range(func(k, v interface{}) bool { 123 | addrs = append(addrs, v.(resolver.Address)) 124 | return true 125 | }) 126 | return addrs 127 | } 128 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_lease_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | ) 24 | 25 | func ExampleLease_grant() { 26 | cli, err := clientv3.New(clientv3.Config{ 27 | Endpoints: endpoints, 28 | DialTimeout: dialTimeout, 29 | }) 30 | if err != nil { 31 | log.Fatal(err) 32 | } 33 | defer cli.Close() 34 | 35 | // minimum lease TTL is 5-second 36 | resp, err := cli.Grant(context.TODO(), 5) 37 | if err != nil { 38 | log.Fatal(err) 39 | } 40 | 41 | // after 5 seconds, the key 'foo' will be removed 42 | _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID)) 43 | if err != nil { 44 | log.Fatal(err) 45 | } 46 | } 47 | 48 | func ExampleLease_revoke() { 49 | cli, err := clientv3.New(clientv3.Config{ 50 | Endpoints: endpoints, 51 | DialTimeout: dialTimeout, 52 | }) 53 | if err != nil { 54 | log.Fatal(err) 55 | } 56 | defer cli.Close() 57 | 58 | resp, err := cli.Grant(context.TODO(), 5) 59 | if err != nil { 60 | log.Fatal(err) 61 | } 62 | 63 | _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID)) 64 | if err != nil { 65 | log.Fatal(err) 66 | } 67 | 68 | // revoking lease expires the key attached to its lease ID 69 | _, err = cli.Revoke(context.TODO(), resp.ID) 70 | if err != nil { 71 | log.Fatal(err) 72 | } 73 | 74 | gresp, err := cli.Get(context.TODO(), "foo") 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | fmt.Println("number of keys:", len(gresp.Kvs)) 79 | // Output: number of keys: 0 80 | } 81 | 82 | func ExampleLease_keepAlive() { 83 | cli, err := clientv3.New(clientv3.Config{ 84 | Endpoints: endpoints, 85 | DialTimeout: dialTimeout, 86 | }) 87 | if err != nil { 88 | log.Fatal(err) 89 | } 90 | defer cli.Close() 91 | 92 | resp, err := cli.Grant(context.TODO(), 5) 93 | if err != nil { 94 | log.Fatal(err) 95 | } 96 | 97 | _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID)) 98 | if err != nil { 99 | log.Fatal(err) 100 | } 101 | 102 | // the key 'foo' will be kept forever 103 | ch, kaerr := cli.KeepAlive(context.TODO(), resp.ID) 104 | if kaerr != nil { 105 | log.Fatal(kaerr) 106 | } 107 | 108 | ka := <-ch 109 | fmt.Println("ttl:", ka.TTL) 110 | // Output: ttl: 5 111 | } 112 | 113 | func ExampleLease_keepAliveOnce() { 114 | cli, err := clientv3.New(clientv3.Config{ 115 | Endpoints: endpoints, 116 | DialTimeout: dialTimeout, 117 | }) 118 | if err != nil { 119 | log.Fatal(err) 120 | } 121 | defer cli.Close() 122 | 123 | resp, err := cli.Grant(context.TODO(), 5) 124 | if err != nil { 125 | log.Fatal(err) 126 | } 127 | 128 | _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID)) 129 | if err != nil { 130 | log.Fatal(err) 131 | } 132 | 133 | // to renew the lease only once 134 | ka, kaerr := cli.KeepAliveOnce(context.TODO(), resp.ID) 135 | if kaerr != nil { 136 | log.Fatal(kaerr) 137 | } 138 | 139 | fmt.Println("ttl:", ka.TTL) 140 | // Output: ttl: 5 141 | } 142 | -------------------------------------------------------------------------------- /6-etcd-mutex/README.md: -------------------------------------------------------------------------------- 1 | ### etcd分布式锁及事务 2 | 3 | ### 前言 4 | 5 | `分布式锁`是控制分布式系统之间同步访问共享资源的一种方式。在分布式系统中,常常需要协调他们的动作。如果不同的系统或是同一个系统的不同主机之间共享了一个或一组资源,那么访问这些资源的时候,往往需要互斥来防止彼此干扰来保证一致性,在这种情况下,便需要使用到分布式锁。 6 | 7 | ### etcd分布式锁设计 8 | 9 | 1. `排他性`:任意时刻,只能有一个机器的一个线程能获取到锁。 10 | 11 | 通过在etcd中存入key值来实现上锁,删除key实现解锁,参考下面伪代码: 12 | 13 | ```go 14 | func Lock(key string, cli *clientv3.Client) error { 15 | //获取key,判断是否存在锁 16 | resp, err := cli.Get(context.Background(), key) 17 | if err != nil { 18 | return err 19 | } 20 | //锁存在,返回上锁失败 21 | if len(resp.Kvs) > 0 { 22 | return errors.New("lock fail") 23 | } 24 | _, err = cli.Put(context.Background(), key, "lock") 25 | if err != nil { 26 | return err 27 | } 28 | return nil 29 | } 30 | //删除key,解锁 31 | func UnLock(key string, cli *clientv3.Client) error { 32 | _, err := cli.Delete(context.Background(), key) 33 | return err 34 | } 35 | ``` 36 | 37 | 当发现已上锁时,直接返回lock fail。也可以处理成等待解锁,解锁后竞争锁。 38 | ```go 39 | //等待key删除后再竞争锁 40 | func waitDelete(key string, cli *clientv3.Client) { 41 | rch := cli.Watch(context.Background(), key) 42 | for wresp := range rch { 43 | for _, ev := range wresp.Events { 44 | switch ev.Type { 45 | case mvccpb.DELETE: //删除 46 | return 47 | } 48 | } 49 | } 50 | } 51 | ``` 52 | 53 | 2. `容错性`:只要分布式锁服务集群节点大部分存活,client就可以进行加锁解锁操作。 54 | `etcd`基于`Raft`算法,确保集群中数据一致性。 55 | 56 | 3. `避免死锁`:分布式锁一定能得到释放,即使client在释放之前崩溃。 57 | 上面分布式锁设计有缺陷,假如client获取到锁后程序直接崩了,没有解锁,那其他线程也无法拿到锁,导致死锁出现。 58 | 通过给key设定`leases`来避免死锁,但是`leases`过期时间设多长呢?假如设了30秒,而上锁后的操作比30秒大,会导致以下问题: 59 | 60 | * 操作没完成,锁被别人占用了,不安全 61 | 62 | * 操作完成后,进行解锁,这时候把别人占用的锁解开了 63 | 64 | `解决方案`:给key添加过期时间后,以`Keep leases alive`方式延续`leases`,当client正常持有锁时,锁不会过期;当client程序崩掉后,程序不能执行`Keep leases alive`,从而让锁过期,避免死锁。看以下伪代码: 65 | 66 | ```go 67 | //上锁 68 | func Lock(key string, cli *clientv3.Client) error { 69 | //获取key,判断是否存在锁 70 | resp, err := cli.Get(context.Background(), key) 71 | if err != nil { 72 | return err 73 | } 74 | //锁存在,等待解锁后再竞争锁 75 | if len(resp.Kvs) > 0 { 76 | waitDelete(key, cli) 77 | return Lock(key) 78 | } 79 | //设置key过期时间 80 | resp, err := cli.Grant(context.TODO(), 30) 81 | if err != nil { 82 | return err 83 | } 84 | //设置key并绑定过期时间 85 | _, err = cli.Put(context.Background(), key, "lock", clientv3.WithLease(resp.ID)) 86 | if err != nil { 87 | return err 88 | } 89 | //延续key的过期时间 90 | _, err = cli.KeepAlive(context.TODO(), resp.ID) 91 | if err != nil { 92 | return err 93 | } 94 | return nil 95 | } 96 | //通过让key值过期来解锁 97 | func UnLock(resp *clientv3.LeaseGrantResponse, cli *clientv3.Client) error { 98 | _, err := cli.Revoke(context.TODO(), resp.ID) 99 | return err 100 | } 101 | ``` 102 | 103 | 经过以上步骤,我们初步完成了分布式锁设计。其实官方已经实现了分布式锁,它大致原理和上述有出入,接下来我们看下如何使用官方的分布式锁。 104 | 105 | ### etcd分布式锁使用 106 | 107 | ```go 108 | func ExampleMutex_Lock() { 109 | cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints}) 110 | if err != nil { 111 | log.Fatal(err) 112 | } 113 | defer cli.Close() 114 | 115 | // create two separate sessions for lock competition 116 | s1, err := concurrency.NewSession(cli) 117 | if err != nil { 118 | log.Fatal(err) 119 | } 120 | defer s1.Close() 121 | m1 := concurrency.NewMutex(s1, "/my-lock/") 122 | 123 | s2, err := concurrency.NewSession(cli) 124 | if err != nil { 125 | log.Fatal(err) 126 | } 127 | defer s2.Close() 128 | m2 := concurrency.NewMutex(s2, "/my-lock/") 129 | 130 | // acquire lock for s1 131 | if err := m1.Lock(context.TODO()); err != nil { 132 | log.Fatal(err) 133 | } 134 | fmt.Println("acquired lock for s1") 135 | 136 | m2Locked := make(chan struct{}) 137 | go func() { 138 | defer close(m2Locked) 139 | // wait until s1 is locks /my-lock/ 140 | if err := m2.Lock(context.TODO()); err != nil { 141 | log.Fatal(err) 142 | } 143 | }() 144 | 145 | if err := m1.Unlock(context.TODO()); err != nil { 146 | log.Fatal(err) 147 | } 148 | fmt.Println("released lock for s1") 149 | 150 | <-m2Locked 151 | fmt.Println("acquired lock for s2") 152 | 153 | // Output: 154 | // acquired lock for s1 155 | // released lock for s1 156 | // acquired lock for s2 157 | } 158 | ``` 159 | 此代码来源于[官方文档](https://github.com/etcd-io/etcd/blob/master/clientv3/concurrency/example_mutex_test.go),etcd分布式锁使用起来很方便。 160 | 161 | ### etcd事务 162 | 163 | 顺便介绍一下etcd事务,先看这段伪代码: 164 | 165 | ```go 166 | Txn(context.TODO()).If(//如果以下判断条件成立 167 | Compare(Value(k1), "<", v1), 168 | Compare(Version(k1), "=", 2) 169 | ).Then(//则执行Then代码段 170 | OpPut(k2,v2), OpPut(k3,v3) 171 | ).Else(//否则执行Else代码段 172 | OpPut(k4,v4), OpPut(k5,v5) 173 | ).Commit()//最后提交事务 174 | ``` 175 | 176 | 使用例子,代码来自[官方文档](https://github.com/etcd-io/etcd/blob/master/clientv3/example_kv_test.go): 177 | 178 | ```go 179 | func ExampleKV_txn() { 180 | cli, err := clientv3.New(clientv3.Config{ 181 | Endpoints: endpoints, 182 | DialTimeout: dialTimeout, 183 | }) 184 | if err != nil { 185 | log.Fatal(err) 186 | } 187 | defer cli.Close() 188 | 189 | kvc := clientv3.NewKV(cli) 190 | 191 | _, err = kvc.Put(context.TODO(), "key", "xyz") 192 | if err != nil { 193 | log.Fatal(err) 194 | } 195 | 196 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 197 | _, err = kvc.Txn(ctx). 198 | // txn value comparisons are lexical 199 | If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). 200 | // the "Then" runs, since "xyz" > "abc" 201 | Then(clientv3.OpPut("key", "XYZ")). 202 | // the "Else" does not run 203 | Else(clientv3.OpPut("key", "ABC")). 204 | Commit() 205 | cancel() 206 | if err != nil { 207 | log.Fatal(err) 208 | } 209 | 210 | gresp, err := kvc.Get(context.TODO(), "key") 211 | cancel() 212 | if err != nil { 213 | log.Fatal(err) 214 | } 215 | for _, ev := range gresp.Kvs { 216 | fmt.Printf("%s : %s\n", ev.Key, ev.Value) 217 | } 218 | // Output: key : XYZ 219 | } 220 | ``` 221 | 222 | ### 总结 223 | 224 | 如果发展到分布式服务阶段,且对数据的可靠性要求很高,选`etcd`实现分布式锁不会错。介于对`ZooKeeper`好感度不强,这里就不介绍`ZooKeeper`分布式锁了。一般的`Redis`分布式锁,可能出现锁丢失的情况(如果你是Java开发者,可以使用Redisson客户端实现分布式锁,据说不会出现锁丢失的情况)。 -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/README.md: -------------------------------------------------------------------------------- 1 | ### gRPC负载均衡(自定义负载均衡策略) 2 | 3 | ### 前言 4 | 上篇文章介绍了如何实现gRPC负载均衡,但目前官方只提供了`pick_first`和`round_robin`两种负载均衡策略,轮询法`round_robin`不能满足因服务器配置不同而承担不同负载量,这篇文章将介绍如何实现自定义负载均衡策略--`加权随机法`。 5 | 6 | `加权随机法`可以根据服务器的处理能力而分配不同的权重,从而实现处理能力高的服务器可承担更多的请求,处理能力低的服务器少承担请求。 7 | 8 | ### 自定义负载均衡策略 9 | 10 | gRPC提供了`V2PickerBuilder`和`V2Picker`接口让我们实现自己的负载均衡策略。 11 | 12 | ```go 13 | type V2PickerBuilder interface { 14 | Build(info PickerBuildInfo) balancer.V2Picker 15 | } 16 | ``` 17 | `V2PickerBuilder`接口:创建V2版本的子连接选择器。 18 | 19 | `Build`方法:返回一个V2选择器,将用于gRPC选择子连接。 20 | 21 | ```go 22 | type V2Picker interface { 23 | Pick(info PickInfo) (PickResult, error) 24 | } 25 | ``` 26 | `V2Picker `接口:用于gRPC选择子连接去发送请求。 27 | `Pick`方法:子连接选择 28 | 29 | 问题来了,我们需要把服务器地址的权重添加进去,但是地址`resolver.Address`并没有提供权重的属性。官方给的答复是:把权重存储到地址的元数据`metadata`中。 30 | 31 | ```go 32 | // attributeKey is the type used as the key to store AddrInfo in the Attributes 33 | // field of resolver.Address. 34 | type attributeKey struct{} 35 | 36 | // AddrInfo will be stored inside Address metadata in order to use weighted balancer. 37 | type AddrInfo struct { 38 | Weight int 39 | } 40 | 41 | // SetAddrInfo returns a copy of addr in which the Attributes field is updated 42 | // with addrInfo. 43 | func SetAddrInfo(addr resolver.Address, addrInfo AddrInfo) resolver.Address { 44 | addr.Attributes = attributes.New() 45 | addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo) 46 | return addr 47 | } 48 | 49 | // GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr. 50 | func GetAddrInfo(addr resolver.Address) AddrInfo { 51 | v := addr.Attributes.Value(attributeKey{}) 52 | ai, _ := v.(AddrInfo) 53 | return ai 54 | } 55 | ``` 56 | 定义`AddrInfo`结构体并添加权重`Weight`属性,`Set`方法把`Weight`存储到`resolver.Address`中,`Get`方法从`resolver.Address`获取`Weight`。 57 | 58 | 解决权重存储问题后,接下来我们实现加权随机法负载均衡策略。 59 | 60 | 首先实现`V2PickerBuilder`接口,返回子连接选择器。 61 | ```go 62 | func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.V2Picker { 63 | grpclog.Infof("weightPicker: newPicker called with info: %v", info) 64 | if len(info.ReadySCs) == 0 { 65 | return base.NewErrPickerV2(balancer.ErrNoSubConnAvailable) 66 | } 67 | var scs []balancer.SubConn 68 | for subConn, addr := range info.ReadySCs { 69 | node := GetAddrInfo(addr.Address) 70 | if node.Weight <= 0 { 71 | node.Weight = minWeight 72 | } else if node.Weight > 5 { 73 | node.Weight = maxWeight 74 | } 75 | for i := 0; i < node.Weight; i++ { 76 | scs = append(scs, subConn) 77 | } 78 | } 79 | return &rrPicker{ 80 | subConns: scs, 81 | } 82 | } 83 | ``` 84 | `加权随机法`中,我使用空间换时间的方式,把权重转成地址个数(例如`addr1`的权重是`3`,那么添加`3`个子连接到切片中;`addr2`权重为`1`,则添加`1`个子连接;选择子连接时候,按子连接切片长度生成随机数,以随机数作为下标就是选中的子连接),避免重复计算权重。考虑到内存占用,权重定义从`1`到`5`权重。 85 | 86 | 接下来实现子连接的选择,获取随机数,选择子连接 87 | ```go 88 | type rrPicker struct { 89 | subConns []balancer.SubConn 90 | mu sync.Mutex 91 | } 92 | 93 | func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { 94 | p.mu.Lock() 95 | index := rand.Intn(len(p.subConns)) 96 | sc := p.subConns[index] 97 | p.mu.Unlock() 98 | return balancer.PickResult{SubConn: sc}, nil 99 | } 100 | ``` 101 | 102 | 关键代码完成后,我们把加权随机法负载均衡策略命名为`weight`,并注册到gRPC的负载均衡策略中。 103 | ```go 104 | // Name is the name of weight balancer. 105 | const Name = "weight" 106 | // NewBuilder creates a new weight balancer builder. 107 | func newBuilder() balancer.Builder { 108 | return base.NewBalancerBuilderV2(Name, &rrPickerBuilder{}, base.Config{HealthCheck: false}) 109 | } 110 | 111 | func init() { 112 | balancer.Register(newBuilder()) 113 | } 114 | ``` 115 | 116 | 完整代码[weight.go](https://github.com/Bingjian-Zhu/etcd-example/blob/master/5-etcd-grpclb-balancer/balancer/weight/weight.go) 117 | 118 | 最后,我们只需要在服务端注册服务时候附带权重,然后客户端在服务发现时把权重`Set`到`resolver.Address`中,最后客户端把负载论衡策略改成`weight`就完成了。 119 | 120 | ```go 121 | //SetServiceList 设置服务地址 122 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 123 | s.lock.Lock() 124 | defer s.lock.Unlock() 125 | //获取服务地址 126 | addr := resolver.Address{Addr: strings.TrimPrefix(key, s.prefix)} 127 | //获取服务地址权重 128 | nodeWeight, err := strconv.Atoi(val) 129 | if err != nil { 130 | //非数字字符默认权重为1 131 | nodeWeight = 1 132 | } 133 | //把服务地址权重存储到resolver.Address的元数据中 134 | addr = weight.SetAddrInfo(addr, weight.AddrInfo{Weight: nodeWeight}) 135 | s.serverList[key] = addr 136 | s.cc.UpdateState(resolver.State{Addresses: s.getServices()}) 137 | log.Println("put key :", key, "wieght:", val) 138 | } 139 | ``` 140 | 141 | 客户端使用`weight`负载均衡策略 142 | 143 | ```go 144 | func main() { 145 | r := etcdv3.NewServiceDiscovery(EtcdEndpoints) 146 | resolver.Register(r) 147 | // 连接服务器 148 | conn, err := grpc.Dial( 149 | fmt.Sprintf("%s:///%s", r.Scheme(), SerName), 150 | grpc.WithBalancerName("weight"), 151 | grpc.WithInsecure(), 152 | ) 153 | if err != nil { 154 | log.Fatalf("net.Connect err: %v", err) 155 | } 156 | defer conn.Close() 157 | ``` 158 | 159 | 运行效果: 160 | 161 | 运行`服务1`,权重为`1` 162 | 163 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520162934052-74794177.png) 164 | 165 | 运行`服务2`,权重为`4` 166 | 167 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520162941378-1116335906.png) 168 | 169 | 运行客户端 170 | 171 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520163515073-1148862720.png) 172 | 173 | 查看前50次请求在`服务1`和`服务器2`的负载情况。`服务1`分配了`9`次请求,`服务2`分配了`41`次请求,接近权重比值。 174 | 175 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520163753358-1654741743.png) 176 | 177 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520163932810-2034341622.png) 178 | 179 | 断开`服务2`,所有请求流向`服务1` 180 | 181 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520164432399-923288256.png) 182 | 183 | 以权重为`4`,重启`服务2`,请求以加权随机法流向两个服务器 184 | 185 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200520164648568-1117742551.png) 186 | 187 | ### 总结 188 | 189 | 本篇文章以加权随机法为例,介绍了如何实现gRPC自定义负载均衡策略,以满足我们的需求。 190 | 191 | 源码地址:https://github.com/Bingjian-Zhu/etcd-example -------------------------------------------------------------------------------- /3-etcd-service-discovery/README.md: -------------------------------------------------------------------------------- 1 | ### etcd实现服务发现 2 | 3 | ### 前言 4 | 5 | [etcd环境安装与使用](https://bingjian-zhu.github.io/2020/05/09/etcd%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8/)文章中介绍了etcd的安装及`v3 API`使用,本篇将介绍如何使用etcd实现服务发现功能。 6 | 7 | ### 服务发现介绍 8 | 服务发现要解决的也是分布式系统中最常见的问题之一,即在同一个分布式集群中的进程或服务,要如何才能找到对方并建立连接。本质上来说,服务发现就是想要了解集群中是否有进程在监听 udp 或 tcp 端口,并且通过名字就可以查找和连接。 9 | 10 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200514171049345-955603950.png) 11 | 12 | 服务发现需要实现一下基本功能: 13 | 14 | * `服务注册`:同一service的所有节点注册到相同目录下,节点启动后将自己的信息注册到所属服务的目录中。 15 | 16 | * `健康检查`:服务节点定时进行健康检查。注册到服务目录中的信息设置一个较短的TTL,运行正常的服务节点每隔一段时间会去更新信息的TTL ,从而达到健康检查效果。 17 | 18 | * `服务发现`:通过服务节点能查询到服务提供外部访问的 IP 和端口号。比如网关代理服务时能够及时的发现服务中新增节点、丢弃不可用的服务节点。 19 | 20 | 接下来介绍如何使用etcd实现服务发现。 21 | 22 | ### 服务注册及健康检查 23 | 24 | 根据etcd的`v3 API`,当启动一个服务时候,我们把服务的地址写进etcd,注册服务。同时绑定租约(lease),并以续租约(keep leases alive)的方式检测服务是否正常运行,从而实现健康检查。 25 | 26 | go代码实现: 27 | ```go 28 | package main 29 | 30 | import ( 31 | "context" 32 | "log" 33 | "time" 34 | 35 | "go.etcd.io/etcd/clientv3" 36 | ) 37 | 38 | //ServiceRegister 创建租约注册服务 39 | type ServiceRegister struct { 40 | cli *clientv3.Client //etcd client 41 | leaseID clientv3.LeaseID //租约ID 42 | //租约keepalieve相应chan 43 | keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse 44 | key string //key 45 | val string //value 46 | } 47 | 48 | //NewServiceRegister 新建注册服务 49 | func NewServiceRegister(endpoints []string, key, val string, lease int64) (*ServiceRegister, error) { 50 | cli, err := clientv3.New(clientv3.Config{ 51 | Endpoints: endpoints, 52 | DialTimeout: 5 * time.Second, 53 | }) 54 | if err != nil { 55 | log.Fatal(err) 56 | } 57 | 58 | ser := &ServiceRegister{ 59 | cli: cli, 60 | key: key, 61 | val: val, 62 | } 63 | 64 | //申请租约设置时间keepalive 65 | if err := ser.putKeyWithLease(lease); err != nil { 66 | return nil, err 67 | } 68 | 69 | return ser, nil 70 | } 71 | 72 | //设置租约 73 | func (s *ServiceRegister) putKeyWithLease(lease int64) error { 74 | //设置租约时间 75 | resp, err := s.cli.Grant(context.Background(), lease) 76 | if err != nil { 77 | return err 78 | } 79 | //注册服务并绑定租约 80 | _, err = s.cli.Put(context.Background(), s.key, s.val, clientv3.WithLease(resp.ID)) 81 | if err != nil { 82 | return err 83 | } 84 | //设置续租 定期发送需求请求 85 | leaseRespChan, err := s.cli.KeepAlive(context.Background(), resp.ID) 86 | 87 | if err != nil { 88 | return err 89 | } 90 | s.leaseID = resp.ID 91 | log.Println(s.leaseID) 92 | s.keepAliveChan = leaseRespChan 93 | log.Printf("Put key:%s val:%s success!", s.key, s.val) 94 | return nil 95 | } 96 | 97 | //ListenLeaseRespChan 监听 续租情况 98 | func (s *ServiceRegister) ListenLeaseRespChan() { 99 | for leaseKeepResp := range s.keepAliveChan { 100 | log.Println("续约成功", leaseKeepResp) 101 | } 102 | log.Println("关闭续租") 103 | } 104 | 105 | // Close 注销服务 106 | func (s *ServiceRegister) Close() error { 107 | //撤销租约 108 | if _, err := s.cli.Revoke(context.Background(), s.leaseID); err != nil { 109 | return err 110 | } 111 | log.Println("撤销租约") 112 | return s.cli.Close() 113 | } 114 | 115 | func main() { 116 | var endpoints = []string{"localhost:2379"} 117 | ser, err := NewServiceRegister(endpoints, "/web/node1", "localhost:8000", 5) 118 | if err != nil { 119 | log.Fatalln(err) 120 | } 121 | //监听续租相应chan 122 | go ser.ListenLeaseRespChan() 123 | select { 124 | // case <-time.After(20 * time.Second): 125 | // ser.Close() 126 | } 127 | } 128 | ``` 129 | 130 | 主动退出服务时,可以调用Close()方法,撤销租约,从而注销服务。 131 | 132 | ### 服务发现 133 | 134 | 根据etcd的`v3 API`,很容易想到使用`Watch`监视某类服务,通过`Watch`感知服务的`添加`,`修改`或`删除`操作,修改服务列表。 135 | 136 | ```go 137 | package main 138 | 139 | import ( 140 | "context" 141 | "log" 142 | "sync" 143 | "time" 144 | 145 | "github.com/coreos/etcd/mvcc/mvccpb" 146 | "go.etcd.io/etcd/clientv3" 147 | ) 148 | 149 | //ServiceDiscovery 服务发现 150 | type ServiceDiscovery struct { 151 | cli *clientv3.Client //etcd client 152 | serverList map[string]string //服务列表 153 | lock sync.Mutex 154 | } 155 | 156 | //NewServiceDiscovery 新建发现服务 157 | func NewServiceDiscovery(endpoints []string) *ServiceDiscovery { 158 | cli, err := clientv3.New(clientv3.Config{ 159 | Endpoints: endpoints, 160 | DialTimeout: 5 * time.Second, 161 | }) 162 | if err != nil { 163 | log.Fatal(err) 164 | } 165 | 166 | return &ServiceDiscovery{ 167 | cli: cli, 168 | serverList: make(map[string]string), 169 | } 170 | } 171 | 172 | //WatchService 初始化服务列表和监视 173 | func (s *ServiceDiscovery) WatchService(prefix string) error { 174 | //根据前缀获取现有的key 175 | resp, err := s.cli.Get(context.Background(), prefix, clientv3.WithPrefix()) 176 | if err != nil { 177 | return err 178 | } 179 | 180 | for _, ev := range resp.Kvs { 181 | s.SetServiceList(string(ev.Key), string(ev.Value)) 182 | } 183 | 184 | //监视前缀,修改变更的server 185 | go s.watcher(prefix) 186 | return nil 187 | } 188 | 189 | //watcher 监听前缀 190 | func (s *ServiceDiscovery) watcher(prefix string) { 191 | rch := s.cli.Watch(context.Background(), prefix, clientv3.WithPrefix()) 192 | log.Printf("watching prefix:%s now...", prefix) 193 | for wresp := range rch { 194 | for _, ev := range wresp.Events { 195 | switch ev.Type { 196 | case mvccpb.PUT: //修改或者新增 197 | s.SetServiceList(string(ev.Kv.Key), string(ev.Kv.Value)) 198 | case mvccpb.DELETE: //删除 199 | s.DelServiceList(string(ev.Kv.Key)) 200 | } 201 | } 202 | } 203 | } 204 | 205 | //SetServiceList 新增服务地址 206 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 207 | s.lock.Lock() 208 | defer s.lock.Unlock() 209 | s.serverList[key] = string(val) 210 | log.Println("put key :", key, "val:", val) 211 | } 212 | 213 | //DelServiceList 删除服务地址 214 | func (s *ServiceDiscovery) DelServiceList(key string) { 215 | s.lock.Lock() 216 | defer s.lock.Unlock() 217 | delete(s.serverList, key) 218 | log.Println("del key:", key) 219 | } 220 | 221 | //GetServices 获取服务地址 222 | func (s *ServiceDiscovery) GetServices() []string { 223 | s.lock.Lock() 224 | defer s.lock.Unlock() 225 | addrs := make([]string, 0) 226 | 227 | for _, v := range s.serverList { 228 | addrs = append(addrs, v) 229 | } 230 | return addrs 231 | } 232 | 233 | //Close 关闭服务 234 | func (s *ServiceDiscovery) Close() error { 235 | return s.cli.Close() 236 | } 237 | 238 | func main() { 239 | var endpoints = []string{"localhost:2379"} 240 | ser := NewServiceDiscovery(endpoints) 241 | defer ser.Close() 242 | ser.WatchService("/web/") 243 | ser.WatchService("/gRPC/") 244 | for { 245 | select { 246 | case <-time.Tick(10 * time.Second): 247 | log.Println(ser.GetServices()) 248 | } 249 | } 250 | } 251 | ``` 252 | 运行: 253 | ``` 254 | #运行服务发现 255 | $go run discovery.go 256 | watching prefix:/web/ now... 257 | put key : /web/node1 val:localhost:8000 258 | [localhost:8000] 259 | 260 | #另一个终端运行服务注册 261 | $go run register.go 262 | Put key:/web/node1 val:localhost:8000 success! 263 | 续约成功 cluster_id:14841639068965178418 member_id:10276657743932975437 revision:29 raft_term:7 264 | 续约成功 cluster_id:14841639068965178418 member_id:10276657743932975437 revision:29 raft_term:7 265 | ... 266 | ``` 267 | 268 | ### 总结 269 | 基于 Raft 算法的 etcd 天生是一个强一致性高可用的服务存储目录,用户可以在 etcd 中注册服务,并且对注册的服务设置key TTL,定时保持服务的心跳以达到监控健康状态的效果。通过在 etcd 指定的主题下注册的服务也能在对应的主题下查找到。 270 | 271 | 为了确保连接,我们可以在每个服务机器上都部署一个 Proxy 模式的 etcd,这样就可以确保能访问 etcd 集群的服务都能互相连接。 272 | 273 | 参考: 274 | * https://segmentfault.com/a/1190000020944777 275 | * https://blog.csdn.net/blogsun/article/details/102861648 276 | * https://www.infoq.cn/article/etcd-interpretation-application-scenario-implement-principle/ 277 | -------------------------------------------------------------------------------- /2-etcd-go-client/example_kv_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 The etcd Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package clientv3_test 16 | 17 | import ( 18 | "context" 19 | "fmt" 20 | "log" 21 | 22 | "github.com/coreos/etcd/clientv3" 23 | "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes" 24 | ) 25 | 26 | func ExampleKV_put() { 27 | cli, err := clientv3.New(clientv3.Config{ 28 | Endpoints: endpoints, 29 | DialTimeout: dialTimeout, 30 | }) 31 | if err != nil { 32 | log.Fatal(err) 33 | } 34 | defer cli.Close() 35 | 36 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 37 | _, err = cli.Put(ctx, "sample_key", "sample_value") 38 | cancel() 39 | if err != nil { 40 | log.Fatal(err) 41 | } 42 | } 43 | 44 | func ExampleKV_putErrorHandling() { 45 | cli, err := clientv3.New(clientv3.Config{ 46 | Endpoints: endpoints, 47 | DialTimeout: dialTimeout, 48 | }) 49 | if err != nil { 50 | log.Fatal(err) 51 | } 52 | defer cli.Close() 53 | 54 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 55 | _, err = cli.Put(ctx, "", "sample_value") 56 | cancel() 57 | if err != nil { 58 | switch err { 59 | case context.Canceled: 60 | fmt.Printf("ctx is canceled by another routine: %v\n", err) 61 | case context.DeadlineExceeded: 62 | fmt.Printf("ctx is attached with a deadline is exceeded: %v\n", err) 63 | case rpctypes.ErrEmptyKey: 64 | fmt.Printf("client-side error: %v\n", err) 65 | default: 66 | fmt.Printf("bad cluster endpoints, which are not etcd servers: %v\n", err) 67 | } 68 | } 69 | // Output: client-side error: etcdserver: key is not provided 70 | } 71 | 72 | func ExampleKV_get() { 73 | cli, err := clientv3.New(clientv3.Config{ 74 | Endpoints: endpoints, 75 | DialTimeout: dialTimeout, 76 | }) 77 | if err != nil { 78 | log.Fatal(err) 79 | } 80 | defer cli.Close() 81 | 82 | _, err = cli.Put(context.TODO(), "foo", "bar") 83 | if err != nil { 84 | log.Fatal(err) 85 | } 86 | 87 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 88 | resp, err := cli.Get(ctx, "foo") 89 | cancel() 90 | if err != nil { 91 | log.Fatal(err) 92 | } 93 | for _, ev := range resp.Kvs { 94 | fmt.Printf("%s : %s\n", ev.Key, ev.Value) 95 | } 96 | // Output: foo : bar 97 | } 98 | 99 | func ExampleKV_getWithRev() { 100 | cli, err := clientv3.New(clientv3.Config{ 101 | Endpoints: endpoints, 102 | DialTimeout: dialTimeout, 103 | }) 104 | if err != nil { 105 | log.Fatal(err) 106 | } 107 | defer cli.Close() 108 | 109 | presp, err := cli.Put(context.TODO(), "foo", "bar1") 110 | if err != nil { 111 | log.Fatal(err) 112 | } 113 | _, err = cli.Put(context.TODO(), "foo", "bar2") 114 | if err != nil { 115 | log.Fatal(err) 116 | } 117 | 118 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 119 | resp, err := cli.Get(ctx, "foo", clientv3.WithRev(presp.Header.Revision)) 120 | cancel() 121 | if err != nil { 122 | log.Fatal(err) 123 | } 124 | for _, ev := range resp.Kvs { 125 | fmt.Printf("%s : %s\n", ev.Key, ev.Value) 126 | } 127 | // Output: foo : bar1 128 | } 129 | 130 | func ExampleKV_getSortedPrefix() { 131 | cli, err := clientv3.New(clientv3.Config{ 132 | Endpoints: endpoints, 133 | DialTimeout: dialTimeout, 134 | }) 135 | if err != nil { 136 | log.Fatal(err) 137 | } 138 | defer cli.Close() 139 | 140 | for i := range make([]int, 3) { 141 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 142 | _, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value") 143 | cancel() 144 | if err != nil { 145 | log.Fatal(err) 146 | } 147 | } 148 | 149 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 150 | resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend)) 151 | cancel() 152 | if err != nil { 153 | log.Fatal(err) 154 | } 155 | for _, ev := range resp.Kvs { 156 | fmt.Printf("%s : %s\n", ev.Key, ev.Value) 157 | } 158 | // Output: 159 | // key_2 : value 160 | // key_1 : value 161 | // key_0 : value 162 | } 163 | 164 | func ExampleKV_delete() { 165 | cli, err := clientv3.New(clientv3.Config{ 166 | Endpoints: endpoints, 167 | DialTimeout: dialTimeout, 168 | }) 169 | if err != nil { 170 | log.Fatal(err) 171 | } 172 | defer cli.Close() 173 | 174 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 175 | defer cancel() 176 | 177 | // count keys about to be deleted 178 | gresp, err := cli.Get(ctx, "key", clientv3.WithPrefix()) 179 | if err != nil { 180 | log.Fatal(err) 181 | } 182 | 183 | // delete the keys 184 | dresp, err := cli.Delete(ctx, "key", clientv3.WithPrefix()) 185 | if err != nil { 186 | log.Fatal(err) 187 | } 188 | 189 | fmt.Println("Deleted all keys:", int64(len(gresp.Kvs)) == dresp.Deleted) 190 | // Output: 191 | // Deleted all keys: true 192 | } 193 | 194 | func ExampleKV_compact() { 195 | cli, err := clientv3.New(clientv3.Config{ 196 | Endpoints: endpoints, 197 | DialTimeout: dialTimeout, 198 | }) 199 | if err != nil { 200 | log.Fatal(err) 201 | } 202 | defer cli.Close() 203 | 204 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 205 | resp, err := cli.Get(ctx, "foo") 206 | cancel() 207 | if err != nil { 208 | log.Fatal(err) 209 | } 210 | compRev := resp.Header.Revision // specify compact revision of your choice 211 | 212 | ctx, cancel = context.WithTimeout(context.Background(), requestTimeout) 213 | _, err = cli.Compact(ctx, compRev) 214 | cancel() 215 | if err != nil { 216 | log.Fatal(err) 217 | } 218 | } 219 | 220 | func ExampleKV_txn() { 221 | cli, err := clientv3.New(clientv3.Config{ 222 | Endpoints: endpoints, 223 | DialTimeout: dialTimeout, 224 | }) 225 | if err != nil { 226 | log.Fatal(err) 227 | } 228 | defer cli.Close() 229 | 230 | kvc := clientv3.NewKV(cli) 231 | 232 | _, err = kvc.Put(context.TODO(), "key", "xyz") 233 | if err != nil { 234 | log.Fatal(err) 235 | } 236 | 237 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) 238 | _, err = kvc.Txn(ctx). 239 | // txn value comparisons are lexical 240 | If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). 241 | // the "Then" runs, since "xyz" > "abc" 242 | Then(clientv3.OpPut("key", "XYZ")). 243 | // the "Else" does not run 244 | Else(clientv3.OpPut("key", "ABC")). 245 | Commit() 246 | cancel() 247 | if err != nil { 248 | log.Fatal(err) 249 | } 250 | 251 | gresp, err := kvc.Get(context.TODO(), "key") 252 | cancel() 253 | if err != nil { 254 | log.Fatal(err) 255 | } 256 | for _, ev := range gresp.Kvs { 257 | fmt.Printf("%s : %s\n", ev.Key, ev.Value) 258 | } 259 | // Output: key : XYZ 260 | } 261 | 262 | func ExampleKV_do() { 263 | cli, err := clientv3.New(clientv3.Config{ 264 | Endpoints: endpoints, 265 | DialTimeout: dialTimeout, 266 | }) 267 | if err != nil { 268 | log.Fatal(err) 269 | } 270 | defer cli.Close() 271 | 272 | ops := []clientv3.Op{ 273 | clientv3.OpPut("put-key", "123"), 274 | clientv3.OpGet("put-key"), 275 | clientv3.OpPut("put-key", "456")} 276 | 277 | for _, op := range ops { 278 | if _, err := cli.Do(context.TODO(), op); err != nil { 279 | log.Fatal(err) 280 | } 281 | } 282 | } 283 | -------------------------------------------------------------------------------- /4-etcd-grpclb/proto/simple.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: 2-simple_rpc/proto/simple.proto 3 | 4 | package proto 5 | 6 | import ( 7 | context "context" 8 | fmt "fmt" 9 | proto "github.com/golang/protobuf/proto" 10 | grpc "google.golang.org/grpc" 11 | codes "google.golang.org/grpc/codes" 12 | status "google.golang.org/grpc/status" 13 | math "math" 14 | ) 15 | 16 | // Reference imports to suppress errors if they are not otherwise used. 17 | var _ = proto.Marshal 18 | var _ = fmt.Errorf 19 | var _ = math.Inf 20 | 21 | // This is a compile-time assertion to ensure that this generated file 22 | // is compatible with the proto package it is being compiled against. 23 | // A compilation error at this line likely means your copy of the 24 | // proto package needs to be updated. 25 | const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package 26 | 27 | // 定义发送请求信息 28 | type SimpleRequest struct { 29 | // 定义发送的参数,采用驼峰命名方式,小写加下划线,如:student_name 30 | // 参数类型 参数名 标识号(不可重复) 31 | Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` 32 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 33 | XXX_unrecognized []byte `json:"-"` 34 | XXX_sizecache int32 `json:"-"` 35 | } 36 | 37 | func (m *SimpleRequest) Reset() { *m = SimpleRequest{} } 38 | func (m *SimpleRequest) String() string { return proto.CompactTextString(m) } 39 | func (*SimpleRequest) ProtoMessage() {} 40 | func (*SimpleRequest) Descriptor() ([]byte, []int) { 41 | return fileDescriptor_31047b63fe44dee8, []int{0} 42 | } 43 | 44 | func (m *SimpleRequest) XXX_Unmarshal(b []byte) error { 45 | return xxx_messageInfo_SimpleRequest.Unmarshal(m, b) 46 | } 47 | func (m *SimpleRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 48 | return xxx_messageInfo_SimpleRequest.Marshal(b, m, deterministic) 49 | } 50 | func (m *SimpleRequest) XXX_Merge(src proto.Message) { 51 | xxx_messageInfo_SimpleRequest.Merge(m, src) 52 | } 53 | func (m *SimpleRequest) XXX_Size() int { 54 | return xxx_messageInfo_SimpleRequest.Size(m) 55 | } 56 | func (m *SimpleRequest) XXX_DiscardUnknown() { 57 | xxx_messageInfo_SimpleRequest.DiscardUnknown(m) 58 | } 59 | 60 | var xxx_messageInfo_SimpleRequest proto.InternalMessageInfo 61 | 62 | func (m *SimpleRequest) GetData() string { 63 | if m != nil { 64 | return m.Data 65 | } 66 | return "" 67 | } 68 | 69 | // 定义响应信息 70 | type SimpleResponse struct { 71 | // 定义接收的参数 72 | // 参数类型 参数名 标识号(不可重复) 73 | Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` 74 | Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` 75 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 76 | XXX_unrecognized []byte `json:"-"` 77 | XXX_sizecache int32 `json:"-"` 78 | } 79 | 80 | func (m *SimpleResponse) Reset() { *m = SimpleResponse{} } 81 | func (m *SimpleResponse) String() string { return proto.CompactTextString(m) } 82 | func (*SimpleResponse) ProtoMessage() {} 83 | func (*SimpleResponse) Descriptor() ([]byte, []int) { 84 | return fileDescriptor_31047b63fe44dee8, []int{1} 85 | } 86 | 87 | func (m *SimpleResponse) XXX_Unmarshal(b []byte) error { 88 | return xxx_messageInfo_SimpleResponse.Unmarshal(m, b) 89 | } 90 | func (m *SimpleResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 91 | return xxx_messageInfo_SimpleResponse.Marshal(b, m, deterministic) 92 | } 93 | func (m *SimpleResponse) XXX_Merge(src proto.Message) { 94 | xxx_messageInfo_SimpleResponse.Merge(m, src) 95 | } 96 | func (m *SimpleResponse) XXX_Size() int { 97 | return xxx_messageInfo_SimpleResponse.Size(m) 98 | } 99 | func (m *SimpleResponse) XXX_DiscardUnknown() { 100 | xxx_messageInfo_SimpleResponse.DiscardUnknown(m) 101 | } 102 | 103 | var xxx_messageInfo_SimpleResponse proto.InternalMessageInfo 104 | 105 | func (m *SimpleResponse) GetCode() int32 { 106 | if m != nil { 107 | return m.Code 108 | } 109 | return 0 110 | } 111 | 112 | func (m *SimpleResponse) GetValue() string { 113 | if m != nil { 114 | return m.Value 115 | } 116 | return "" 117 | } 118 | 119 | func init() { 120 | proto.RegisterType((*SimpleRequest)(nil), "proto.SimpleRequest") 121 | proto.RegisterType((*SimpleResponse)(nil), "proto.SimpleResponse") 122 | } 123 | 124 | func init() { proto.RegisterFile("2-simple_rpc/proto/simple.proto", fileDescriptor_31047b63fe44dee8) } 125 | 126 | var fileDescriptor_31047b63fe44dee8 = []byte{ 127 | // 158 bytes of a gzipped FileDescriptorProto 128 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x37, 0xd2, 0x2d, 0xce, 129 | 0xcc, 0x2d, 0xc8, 0x49, 0x8d, 0x2f, 0x2a, 0x48, 0xd6, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0xd7, 0x87, 130 | 0x08, 0xe8, 0x81, 0x39, 0x42, 0xac, 0x60, 0x4a, 0x49, 0x99, 0x8b, 0x37, 0x18, 0x2c, 0x1c, 0x94, 131 | 0x5a, 0x58, 0x9a, 0x5a, 0x5c, 0x22, 0x24, 0xc4, 0xc5, 0x92, 0x92, 0x58, 0x92, 0x28, 0xc1, 0xa8, 132 | 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0x66, 0x2b, 0x59, 0x71, 0xf1, 0xc1, 0x14, 0x15, 0x17, 0xe4, 0xe7, 133 | 0x15, 0xa7, 0x82, 0x54, 0x25, 0xe7, 0xa7, 0xa4, 0x82, 0x55, 0xb1, 0x06, 0x81, 0xd9, 0x42, 0x22, 134 | 0x5c, 0xac, 0x65, 0x89, 0x39, 0xa5, 0xa9, 0x12, 0x4c, 0x60, 0xad, 0x10, 0x8e, 0x91, 0x03, 0x17, 135 | 0x1b, 0x44, 0xaf, 0x90, 0x19, 0x17, 0x6b, 0x50, 0x7e, 0x69, 0x49, 0xaa, 0x90, 0x08, 0xc4, 0x09, 136 | 0x7a, 0x28, 0x16, 0x4b, 0x89, 0xa2, 0x89, 0x42, 0x6c, 0x52, 0x62, 0x48, 0x62, 0x03, 0x8b, 0x1b, 137 | 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x50, 0xd7, 0x51, 0x6d, 0xd3, 0x00, 0x00, 0x00, 138 | } 139 | 140 | // Reference imports to suppress errors if they are not otherwise used. 141 | var _ context.Context 142 | var _ grpc.ClientConn 143 | 144 | // This is a compile-time assertion to ensure that this generated file 145 | // is compatible with the grpc package it is being compiled against. 146 | const _ = grpc.SupportPackageIsVersion4 147 | 148 | // SimpleClient is the client API for Simple service. 149 | // 150 | // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. 151 | type SimpleClient interface { 152 | Route(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) 153 | } 154 | 155 | type simpleClient struct { 156 | cc *grpc.ClientConn 157 | } 158 | 159 | func NewSimpleClient(cc *grpc.ClientConn) SimpleClient { 160 | return &simpleClient{cc} 161 | } 162 | 163 | func (c *simpleClient) Route(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) { 164 | out := new(SimpleResponse) 165 | err := c.cc.Invoke(ctx, "/proto.Simple/Route", in, out, opts...) 166 | if err != nil { 167 | return nil, err 168 | } 169 | return out, nil 170 | } 171 | 172 | // SimpleServer is the server API for Simple service. 173 | type SimpleServer interface { 174 | Route(context.Context, *SimpleRequest) (*SimpleResponse, error) 175 | } 176 | 177 | // UnimplementedSimpleServer can be embedded to have forward compatible implementations. 178 | type UnimplementedSimpleServer struct { 179 | } 180 | 181 | func (*UnimplementedSimpleServer) Route(ctx context.Context, req *SimpleRequest) (*SimpleResponse, error) { 182 | return nil, status.Errorf(codes.Unimplemented, "method Route not implemented") 183 | } 184 | 185 | func RegisterSimpleServer(s *grpc.Server, srv SimpleServer) { 186 | s.RegisterService(&_Simple_serviceDesc, srv) 187 | } 188 | 189 | func _Simple_Route_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 190 | in := new(SimpleRequest) 191 | if err := dec(in); err != nil { 192 | return nil, err 193 | } 194 | if interceptor == nil { 195 | return srv.(SimpleServer).Route(ctx, in) 196 | } 197 | info := &grpc.UnaryServerInfo{ 198 | Server: srv, 199 | FullMethod: "/proto.Simple/Route", 200 | } 201 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 202 | return srv.(SimpleServer).Route(ctx, req.(*SimpleRequest)) 203 | } 204 | return interceptor(ctx, in, info, handler) 205 | } 206 | 207 | var _Simple_serviceDesc = grpc.ServiceDesc{ 208 | ServiceName: "proto.Simple", 209 | HandlerType: (*SimpleServer)(nil), 210 | Methods: []grpc.MethodDesc{ 211 | { 212 | MethodName: "Route", 213 | Handler: _Simple_Route_Handler, 214 | }, 215 | }, 216 | Streams: []grpc.StreamDesc{}, 217 | Metadata: "2-simple_rpc/proto/simple.proto", 218 | } 219 | -------------------------------------------------------------------------------- /5-etcd-grpclb-balancer/proto/simple.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: 2-simple_rpc/proto/simple.proto 3 | 4 | package proto 5 | 6 | import ( 7 | context "context" 8 | fmt "fmt" 9 | proto "github.com/golang/protobuf/proto" 10 | grpc "google.golang.org/grpc" 11 | codes "google.golang.org/grpc/codes" 12 | status "google.golang.org/grpc/status" 13 | math "math" 14 | ) 15 | 16 | // Reference imports to suppress errors if they are not otherwise used. 17 | var _ = proto.Marshal 18 | var _ = fmt.Errorf 19 | var _ = math.Inf 20 | 21 | // This is a compile-time assertion to ensure that this generated file 22 | // is compatible with the proto package it is being compiled against. 23 | // A compilation error at this line likely means your copy of the 24 | // proto package needs to be updated. 25 | const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package 26 | 27 | // 定义发送请求信息 28 | type SimpleRequest struct { 29 | // 定义发送的参数,采用驼峰命名方式,小写加下划线,如:student_name 30 | // 参数类型 参数名 标识号(不可重复) 31 | Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` 32 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 33 | XXX_unrecognized []byte `json:"-"` 34 | XXX_sizecache int32 `json:"-"` 35 | } 36 | 37 | func (m *SimpleRequest) Reset() { *m = SimpleRequest{} } 38 | func (m *SimpleRequest) String() string { return proto.CompactTextString(m) } 39 | func (*SimpleRequest) ProtoMessage() {} 40 | func (*SimpleRequest) Descriptor() ([]byte, []int) { 41 | return fileDescriptor_31047b63fe44dee8, []int{0} 42 | } 43 | 44 | func (m *SimpleRequest) XXX_Unmarshal(b []byte) error { 45 | return xxx_messageInfo_SimpleRequest.Unmarshal(m, b) 46 | } 47 | func (m *SimpleRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 48 | return xxx_messageInfo_SimpleRequest.Marshal(b, m, deterministic) 49 | } 50 | func (m *SimpleRequest) XXX_Merge(src proto.Message) { 51 | xxx_messageInfo_SimpleRequest.Merge(m, src) 52 | } 53 | func (m *SimpleRequest) XXX_Size() int { 54 | return xxx_messageInfo_SimpleRequest.Size(m) 55 | } 56 | func (m *SimpleRequest) XXX_DiscardUnknown() { 57 | xxx_messageInfo_SimpleRequest.DiscardUnknown(m) 58 | } 59 | 60 | var xxx_messageInfo_SimpleRequest proto.InternalMessageInfo 61 | 62 | func (m *SimpleRequest) GetData() string { 63 | if m != nil { 64 | return m.Data 65 | } 66 | return "" 67 | } 68 | 69 | // 定义响应信息 70 | type SimpleResponse struct { 71 | // 定义接收的参数 72 | // 参数类型 参数名 标识号(不可重复) 73 | Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` 74 | Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` 75 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 76 | XXX_unrecognized []byte `json:"-"` 77 | XXX_sizecache int32 `json:"-"` 78 | } 79 | 80 | func (m *SimpleResponse) Reset() { *m = SimpleResponse{} } 81 | func (m *SimpleResponse) String() string { return proto.CompactTextString(m) } 82 | func (*SimpleResponse) ProtoMessage() {} 83 | func (*SimpleResponse) Descriptor() ([]byte, []int) { 84 | return fileDescriptor_31047b63fe44dee8, []int{1} 85 | } 86 | 87 | func (m *SimpleResponse) XXX_Unmarshal(b []byte) error { 88 | return xxx_messageInfo_SimpleResponse.Unmarshal(m, b) 89 | } 90 | func (m *SimpleResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 91 | return xxx_messageInfo_SimpleResponse.Marshal(b, m, deterministic) 92 | } 93 | func (m *SimpleResponse) XXX_Merge(src proto.Message) { 94 | xxx_messageInfo_SimpleResponse.Merge(m, src) 95 | } 96 | func (m *SimpleResponse) XXX_Size() int { 97 | return xxx_messageInfo_SimpleResponse.Size(m) 98 | } 99 | func (m *SimpleResponse) XXX_DiscardUnknown() { 100 | xxx_messageInfo_SimpleResponse.DiscardUnknown(m) 101 | } 102 | 103 | var xxx_messageInfo_SimpleResponse proto.InternalMessageInfo 104 | 105 | func (m *SimpleResponse) GetCode() int32 { 106 | if m != nil { 107 | return m.Code 108 | } 109 | return 0 110 | } 111 | 112 | func (m *SimpleResponse) GetValue() string { 113 | if m != nil { 114 | return m.Value 115 | } 116 | return "" 117 | } 118 | 119 | func init() { 120 | proto.RegisterType((*SimpleRequest)(nil), "proto.SimpleRequest") 121 | proto.RegisterType((*SimpleResponse)(nil), "proto.SimpleResponse") 122 | } 123 | 124 | func init() { proto.RegisterFile("2-simple_rpc/proto/simple.proto", fileDescriptor_31047b63fe44dee8) } 125 | 126 | var fileDescriptor_31047b63fe44dee8 = []byte{ 127 | // 158 bytes of a gzipped FileDescriptorProto 128 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x37, 0xd2, 0x2d, 0xce, 129 | 0xcc, 0x2d, 0xc8, 0x49, 0x8d, 0x2f, 0x2a, 0x48, 0xd6, 0x2f, 0x28, 0xca, 0x2f, 0xc9, 0xd7, 0x87, 130 | 0x08, 0xe8, 0x81, 0x39, 0x42, 0xac, 0x60, 0x4a, 0x49, 0x99, 0x8b, 0x37, 0x18, 0x2c, 0x1c, 0x94, 131 | 0x5a, 0x58, 0x9a, 0x5a, 0x5c, 0x22, 0x24, 0xc4, 0xc5, 0x92, 0x92, 0x58, 0x92, 0x28, 0xc1, 0xa8, 132 | 0xc0, 0xa8, 0xc1, 0x19, 0x04, 0x66, 0x2b, 0x59, 0x71, 0xf1, 0xc1, 0x14, 0x15, 0x17, 0xe4, 0xe7, 133 | 0x15, 0xa7, 0x82, 0x54, 0x25, 0xe7, 0xa7, 0xa4, 0x82, 0x55, 0xb1, 0x06, 0x81, 0xd9, 0x42, 0x22, 134 | 0x5c, 0xac, 0x65, 0x89, 0x39, 0xa5, 0xa9, 0x12, 0x4c, 0x60, 0xad, 0x10, 0x8e, 0x91, 0x03, 0x17, 135 | 0x1b, 0x44, 0xaf, 0x90, 0x19, 0x17, 0x6b, 0x50, 0x7e, 0x69, 0x49, 0xaa, 0x90, 0x08, 0xc4, 0x09, 136 | 0x7a, 0x28, 0x16, 0x4b, 0x89, 0xa2, 0x89, 0x42, 0x6c, 0x52, 0x62, 0x48, 0x62, 0x03, 0x8b, 0x1b, 137 | 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x50, 0xd7, 0x51, 0x6d, 0xd3, 0x00, 0x00, 0x00, 138 | } 139 | 140 | // Reference imports to suppress errors if they are not otherwise used. 141 | var _ context.Context 142 | var _ grpc.ClientConn 143 | 144 | // This is a compile-time assertion to ensure that this generated file 145 | // is compatible with the grpc package it is being compiled against. 146 | const _ = grpc.SupportPackageIsVersion4 147 | 148 | // SimpleClient is the client API for Simple service. 149 | // 150 | // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. 151 | type SimpleClient interface { 152 | Route(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) 153 | } 154 | 155 | type simpleClient struct { 156 | cc *grpc.ClientConn 157 | } 158 | 159 | func NewSimpleClient(cc *grpc.ClientConn) SimpleClient { 160 | return &simpleClient{cc} 161 | } 162 | 163 | func (c *simpleClient) Route(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) { 164 | out := new(SimpleResponse) 165 | err := c.cc.Invoke(ctx, "/proto.Simple/Route", in, out, opts...) 166 | if err != nil { 167 | return nil, err 168 | } 169 | return out, nil 170 | } 171 | 172 | // SimpleServer is the server API for Simple service. 173 | type SimpleServer interface { 174 | Route(context.Context, *SimpleRequest) (*SimpleResponse, error) 175 | } 176 | 177 | // UnimplementedSimpleServer can be embedded to have forward compatible implementations. 178 | type UnimplementedSimpleServer struct { 179 | } 180 | 181 | func (*UnimplementedSimpleServer) Route(ctx context.Context, req *SimpleRequest) (*SimpleResponse, error) { 182 | return nil, status.Errorf(codes.Unimplemented, "method Route not implemented") 183 | } 184 | 185 | func RegisterSimpleServer(s *grpc.Server, srv SimpleServer) { 186 | s.RegisterService(&_Simple_serviceDesc, srv) 187 | } 188 | 189 | func _Simple_Route_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { 190 | in := new(SimpleRequest) 191 | if err := dec(in); err != nil { 192 | return nil, err 193 | } 194 | if interceptor == nil { 195 | return srv.(SimpleServer).Route(ctx, in) 196 | } 197 | info := &grpc.UnaryServerInfo{ 198 | Server: srv, 199 | FullMethod: "/proto.Simple/Route", 200 | } 201 | handler := func(ctx context.Context, req interface{}) (interface{}, error) { 202 | return srv.(SimpleServer).Route(ctx, req.(*SimpleRequest)) 203 | } 204 | return interceptor(ctx, in, info, handler) 205 | } 206 | 207 | var _Simple_serviceDesc = grpc.ServiceDesc{ 208 | ServiceName: "proto.Simple", 209 | HandlerType: (*SimpleServer)(nil), 210 | Methods: []grpc.MethodDesc{ 211 | { 212 | MethodName: "Route", 213 | Handler: _Simple_Route_Handler, 214 | }, 215 | }, 216 | Streams: []grpc.StreamDesc{}, 217 | Metadata: "2-simple_rpc/proto/simple.proto", 218 | } 219 | -------------------------------------------------------------------------------- /1-etcd环境安装与使用/README.md: -------------------------------------------------------------------------------- 1 | ### etcd简介 2 | [etcd](https://github.com/etcd-io/etcd)是开源的、高可用的分布式key-value存储系统,可用于配置共享和服务的注册和发现,它专注于: 3 | 4 | * 简单:定义清晰、面向用户的API(gRPC) 5 | 6 | * 安全:可选的客户端TLS证书自动认证 7 | 8 | * 快速:支持每秒10,000次写入 9 | 10 | * 可靠:基于Raft算法确保强一致性 11 | 12 | ##### etcd与redis差异 13 | etcd和redis都支持键值存储,也支持分布式特性,redis支持的数据格式更加丰富,但是他们两个定位和应用场景不一样,关键差异如下: 14 | 15 | * redis在分布式环境下不是强一致性的,可能会丢失数据,或者读取不到最新数据 16 | 17 | * redis的数据变化监听机制没有etcd完善 18 | 19 | * etcd强一致性保证数据可靠性,导致性能上要低于redis 20 | 21 | * etcd和ZooKeeper是定位类似的项目,跟redis定位不一样 22 | 23 | ##### 为什么用 etcd 而不用ZooKeeper? 24 | 相较之下,ZooKeeper有如下缺点: 25 | 26 | * `复杂`:ZooKeeper的部署维护复杂,管理员需要掌握一系列的知识和技能;而 Paxos 强一致性算法也是素来以复杂难懂而闻名于世;另外,ZooKeeper的使用也比较复杂,需要安装客户端,官方只提供了 Java 和 C 两种语言的接口。 27 | 28 | * `难以维护`:Java 编写。这里不是对 Java 有偏见,而是 Java 本身就偏向于重型应用,它会引入大量的依赖。而运维人员则普遍希望保持强一致、高可用的机器集群尽可能简单,维护起来也不易出错。 29 | 30 | * `发展缓慢`:Apache 基金会项目特有的“Apache Way”在开源界饱受争议,其中一大原因就是由于基金会庞大的结构以及松散的管理导致项目发展缓慢。 31 | 32 | 而 etcd 作为一个后起之秀,其优点也很明显。 33 | 34 | * `简单`:使用 Go 语言编写部署简单;使用 HTTP 作为接口使用简单;使用 Raft 算法保证强一致性让用户易于理解。 35 | 36 | * `数据持久化`:tcd 默认数据一更新就进行持久化。 37 | 38 | * `安全`:etcd 支持 SSL 客户端安全认证。 39 | 40 | ### 单机部署 41 | 42 | (1)到etcd的github地址,下载最新的安装包(目前最新版本:v3.4.7) 43 | 44 | 下载地址:https://github.com/etcd-io/etcd/releases/ 45 | 46 | (2)解压,把`etcd`和`etcdctl`文件复制到已经配置了环境变量的目录中 47 | 48 | * 方法一:把`etcd`和`etcdctl`文件复制到`GOBIN`目录下。 49 | 50 | * 方法二:在环境变量里添加`etcd`和`etcdctl`文件所在的目录。 51 | 52 | (3)验证是否安装成功 53 | ``` 54 | $ etcd --version 55 | etcd Version: 3.4.7 56 | Git SHA: e694b7bb0 57 | Go Version: go1.12.17 58 | Go OS/Arch: linux/amd64 59 | ``` 60 | 61 | 正常显示etcd版本信息,则证明安装成功。 62 | 63 | ### API学习 64 | `etcdctl`用于与`etcd`交互的控制台程序。`API`版本可以通过`ETCDCTL_API`环境变量设置为2或3版本。默认情况下,`v3.4`以上的`etcdctl`使用`v3 API`,`v3.3`及更早的版本默认使用`v2 API`。 65 | 66 | > 注意:用`v2 API`创建的任何key将不能通过`v3 API`查询。同样,用`v3 API`创建的任何key将不能通过`v2 API`查询。 67 | 68 | 运行etcd,在终端输入:`etcd` 69 | 70 | 在另一个终端运行ctcdctl测试。 71 | ``` 72 | #查看默认API版本 73 | $ etcdctl version 74 | etcdctl version: 3.4.7 75 | API version: 3.4 #v3 API 76 | 77 | #写入key:/test/foo value:hello etcd (双引号可去掉) 78 | $ etcdctl put /test/foo "hello etcd" 79 | OK 80 | $ etcdctl get /test/foo 81 | /test/foo 82 | hello etcd 83 | 84 | #手动切换到v2 API 85 | $ export ETCDCTL_API=2 86 | $ etcdctl --version 87 | etcdctl version: 3.4.7 88 | API version: 2 89 | 90 | $ etcdctl get /test/foo 91 | Error: client: response is invalid json. The endpoint is probably not valid etcd cluster endpoint #查询不到/test/foo的值 92 | ``` 93 | 94 | #### 写入key 95 | ``` 96 | $ etcdctl put foo bar 97 | OK 98 | ``` 99 | #### 读取key值 100 | ``` 101 | $ etcdctl get foo 102 | foo 103 | bar 104 | 105 | #只是获取值 106 | $ etcdctl get foo --print-value-only 107 | bar 108 | ``` 109 | 110 | ``` 111 | $ etcdctl put foo1 bar1 112 | $ etcdctl put foo2 bar2 113 | $ etcdctl put foo3 bar3 114 | 115 | #获取从foo到foo3的值,不包括foo3 116 | $ etcdctl get foo foo3 --print-value-only 117 | bar 118 | bar1 119 | bar2 120 | 121 | # 获取前缀为foo的值 122 | $ etcdctl get --prefix foo --print-value-only 123 | bar 124 | bar1 125 | bar2 126 | bar3 127 | 128 | #获取符合前缀的前两个值 129 | $ etcdctl get --prefix --limit=2 foo --print-value-only 130 | bar 131 | bar1 132 | ``` 133 | 134 | #### 删除key 135 | ``` 136 | #删除foo 137 | $ etcdctl del foo 138 | 1 139 | 140 | #删除foo到foo2,不包括foo2 141 | $ etcdctl del foo foo2 142 | 1 143 | #删除key前缀为foo的 144 | $ etcdctl del --prefix foo 145 | 2 146 | ``` 147 | 148 | #### 监视值变化 149 | ``` 150 | #监视foo单个key 151 | $ etcdctl watch foo 152 | #另一个控制台执行: etcdctl put foo bar 153 | PUT 154 | foo 155 | bar 156 | 157 | #同时监视多个值 158 | $ etcdctl watch -i 159 | $ watch foo 160 | $ watch zoo 161 | # 另一个控制台执行: etcdctl put foo bar 162 | PUT 163 | foo 164 | bar 165 | # 另一个控制台执行: etcdctl put zoo val 166 | PUT 167 | zoo 168 | val 169 | 170 | #监视foo前缀的key 171 | $ etcdctl watch --prefix foo 172 | #另一个控制台执行: etcdctl put foo1 bar1 173 | PUT 174 | foo1 175 | bar1 176 | #另一个控制台执行: etcdctl put fooz1 barz1 177 | PUT 178 | fooz1 179 | barz1 180 | ``` 181 | 182 | #### 设置租约(Grant leases) 183 | 184 | 当一个key被绑定到一个租约上时,它的生命周期与租约的生命周期绑定。 185 | 186 | ``` 187 | #设置60秒后过期时间 188 | $ etcdctl lease grant 60 189 | lease 32695410dcc0ca06 granted with TTL(60s) 190 | 191 | #把foo和租约绑定,设置成60秒后过期 192 | $ etcdctl put --lease=32695410dcc0ca06 foo bar 193 | OK 194 | $ etcdctl get foo 195 | foo 196 | bar 197 | 198 | #60秒后,获取不到foo 199 | $ etcdctl get foo 200 | #返回空 201 | ``` 202 | 203 | #### 主动撤销租约(Revoke leases) 204 | 205 | 通过租赁ID(此处指:`32695410dcc0ca06`)撤销租约。`撤销租约将删除其所有绑定的key`。 206 | 207 | ``` 208 | $ etcdctl lease grant 60 209 | lease 32695410dcc0ca06 granted with TTL(60s) 210 | $ etcdctl put foo bar --lease=32695410dcc0ca06 211 | OK 212 | 213 | #主动撤销租约 214 | $ etcdctl lease revoke 32695410dcc0ca06 215 | lease 32695410dcc0ca06 revoked 216 | 217 | $ etcdctl get foo 218 | #返回空 219 | ``` 220 | 221 | #### 续租约(Keep leases alive) 222 | 223 | 通过刷新其TTL来保持租约的有效,使其不会过期。 224 | 225 | ``` 226 | #设置60秒后过期租约 227 | $ etcdctl lease grant 60 228 | lease 32695410dcc0ca06 granted with TTL(60s) 229 | 230 | #把foo和租约绑定,设置成60秒后过期 231 | $ etcdctl put foo bar --lease=32695410dcc0ca06 232 | 233 | #续租约,自动定时执行续租约,续约成功后每次租约为60秒 234 | $ etcdctl lease keep-alive 32695410dcc0ca06 235 | lease 32695410dcc0ca06 keepalived with TTL(60) 236 | lease 32695410dcc0ca06 keepalived with TTL(60) 237 | lease 32695410dcc0ca06 keepalived with TTL(60) 238 | ... 239 | ``` 240 | 241 | #### 获取租约信息(Get lease information) 242 | 243 | 获取租约信息,以便续租或查看租约是否仍然存在或已过期 244 | 245 | ``` 246 | #设置500秒TTL 247 | $ etcdctl lease grant 500 248 | lease 694d5765fc71500b granted with TTL(500s) 249 | 250 | #keyzoo1绑定694d5765fc71500b租约 251 | $ etcdctl put zoo1 val1 --lease=694d5765fc71500b 252 | OK 253 | 254 | #查看租约信息,remaining(132s)剩余有效时间132秒;--keys获取租约绑定的key 255 | $ etcdctl lease timetolive --keys 694d5765fc71500b 256 | lease 694d5765fc71500b granted with TTL(500s), remaining(132s), attached keys([zoo1]) 257 | ``` 258 | 259 | 值得注意的地方,一个租约可以绑定多个`key` 260 | ``` 261 | $ etcdctl lease grant 500 262 | lease 694d5765fc71500b granted with TTL(500s) 263 | 264 | $ etcdctl put zoo1 val1 --lease=694d5765fc71500b 265 | OK 266 | 267 | $ etcdctl put zoo2 val2 --lease=694d5765fc71500b 268 | OK 269 | ``` 270 | 当租约过期后,所有key值会被删除。 271 | 272 | 当一个租约只绑定了一个`key`时,想删除这个`key`,最好的办法是撤销它的租约,而不是直接删除这个`key`。 273 | 274 | 看下面这个例子: 275 | ``` 276 | #方法一:直接删除`key` 277 | #设置租约并绑定zoo1 278 | $ etcdctl lease grant 60 279 | lease 694d71f80ed8bf1e granted with TTL(60s) 280 | $ etcdctl put zoo1 val1 --lease=694d71f80ed8bf1e 281 | OK 282 | 283 | #续租约 284 | $ etcdctl lease keep-alive 694d71f80ed8bf1e 285 | lease 694d71f80ed8bf1e keepalived with TTL(60) 286 | 287 | #另一个控制台执行:etcdctl del zoo1 288 | 289 | #单纯删除key后,续约操作还会一直进行,造成内存泄露 290 | lease 694d71f80ed8bf1e keepalived with TTL(60) 291 | lease 694d71f80ed8bf1e keepalived with TTL(60) 292 | lease 694d71f80ed8bf1e keepalived with TTL(60) 293 | ... 294 | ``` 295 | 296 | ``` 297 | 方法二:撤销`key`的租约 298 | #设置租约并绑定zoo1 299 | $ etcdctl lease grant 60 300 | lease 694d71f80ed8bf1e granted with TTL(60s) 301 | $ etcdctl put zoo1 val1 --lease=694d71f80ed8bf1e 302 | OK 303 | 304 | #续租约 305 | $ etcdctl lease keep-alive 694d71f80ed8bf1e 306 | lease 694d71f80ed8bf1e keepalived with TTL(60) 307 | lease 694d71f80ed8bf1e keepalived with TTL(60) 308 | 309 | #另一个控制台执行:etcdctl lease revoke 694d71f80ed8bf1e 310 | 311 | #续约操作并退出 312 | lease 694d71f80ed8bf1e expired or revoked. 313 | ``` 314 | 315 | 当租约没有绑定`key`时,应主动把它撤销掉。 316 | 317 | ### 应用场景 318 | 319 | 根据以上特性和API,etcd有应用场景以下应用场景: 320 | 321 | #### 场景一:服务发现 322 | 服务发现要解决的也是分布式系统中最常见的问题之一,即在同一个分布式集群中的进程或服务,要如何才能找到对方并建立连接。本质上来说,服务发现就是想要了解集群中是否有进程在监听 udp 或 tcp 端口,并且通过名字就可以查找和连接。 323 | 324 | #### 场景二:配置中心 325 | etcd的应用场景优化都是围绕存储的东西是“配置” 来设定的。 326 | * 配置的数据量通常都不大,所以默认etcd的存储上限是1GB 327 | * 配置通常对历史版本信息是比较关心的,所以etcd会保存 版本(revision) 信息 328 | * 配置变更是比较常见的,并且业务程序会需要实时知道,所以etcd提供了watch机制,基本就是实时通知配置变化 329 | * 配置的准确性一致性极其重要,所以etcd采用raft算法,保证系统的CP 330 | * 同一份配置通常会被大量客户端同时访问,针对这个做了grpc proxy对同一个key的watcher做了优化 331 | * 配置会被不同的业务部门使用,提供了权限控制和namespace机制 332 | 333 | #### 场景三:负载均衡 334 | 此处指的负载均衡均为软负载均衡,分布式系统中,为了保证服务的高可用以及数据的一致性,通常都会把数据和服务部署多份,以此达到对等服务,即使其中的某一个服务失效了,也不影响使用。由此带来的坏处是数据写入性能下降,而好处则是数据访问时的负载均衡。因为每个对等服务节点上都存有完整的数据,所以用户的访问流量就可以分流到不同的机器上。 335 | 336 | #### 场景四:分布式锁 337 | 因为 etcd 使用 Raft 算法保持了数据的强一致性,某次操作存储到集群中的值必然是全局一致的,所以很容易实现分布式锁。 338 | 339 | #### 场景五:集群监控与 Leader 竞选 340 | 通过 etcd 来进行监控实现起来非常简单并且实时性强。 341 | 342 | * 前面几个场景已经提到 Watcher 机制,当某个节点消失或有变动时,Watcher 会第一时间发现并告知用户。 343 | * 节点可以设置TTL key,比如每隔 30s 发送一次心跳使代表该机器存活的节点继续存在,否则节点消失。 344 | 345 | 这样就可以第一时间检测到各节点的健康状态,以完成集群的监控要求。 346 | 347 | 另外,使用分布式锁,可以完成 Leader 竞选。这种场景通常是一些长时间 CPU 计算或者使用 IO 操作的机器,只需要竞选出的 Leader 计算或处理一次,就可以把结果复制给其他的 Follower。从而避免重复劳动,节省计算资源。 348 | 349 | 参考: 350 | * https://github.com/etcd-io/etcd 351 | * https://etcd.io/docs/v3.4.0/dev-guide/interacting_v3/ 352 | * https://www.infoq.cn/article/etcd-interpretation-application-scenario-implement-principle/ -------------------------------------------------------------------------------- /4-etcd-grpclb/README.md: -------------------------------------------------------------------------------- 1 | ### gRPC负载均衡(客户端负载均衡) 2 | 3 | ### 前言 4 | [上篇](https://bingjian-zhu.github.io/2020/05/14/etcd%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0/)介绍了如何使用`etcd`实现服务发现,本篇将基于etcd的服务发现前提下,介绍如何实现gRPC客户端负载均衡。 5 | 6 | ### gRPC负载均衡 7 | gRPC官方文档提供了关于gRPC负载均衡方案[Load Balancing in gRPC](https://github.com/grpc/grpc/blob/master/doc/load-balancing.md),此方案是为gRPC设计的,下面我们对此进行分析。 8 | 9 | #### 1、对每次调用进行负载均衡 10 | gRPC中的负载平衡是以每次调用为基础,而不是以每个连接为基础。换句话说,即使所有的请求都来自一个客户端,我们仍希望它们在所有的服务器上实现负载平衡。 11 | 12 | #### 2、负载均衡的方法 13 | 14 | * `集中式`(Proxy Model) 15 | 16 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518153536494-684598725.png) 17 | 18 | 在服务消费者和服务提供者之间有一个独立的负载均衡(LB),通常是专门的硬件设备如 F5,或者基于软件如 LVS,HAproxy等实现。LB上有所有服务的地址映射表,通常由运维配置注册,当服务消费方调用某个目标服务时,它向LB发起请求,由LB以某种策略,比如轮询(Round-Robin)做负载均衡后将请求转发到目标服务。LB一般具备健康检查能力,能自动摘除不健康的服务实例。 19 | 20 | 该方案主要问题:服务消费方、提供方之间增加了一级,有一定性能开销,请求量大时,效率较低。 21 | 22 | > 可能有读者会认为集中式负载均衡存在这样的问题,一旦负载均衡服务挂掉,那整个系统将不能使用。 23 | > 解决方案:可以对负载均衡服务进行DNS负载均衡,通过对一个域名设置多个IP地址,每次DNS解析时轮询返回负载均衡服务地址,从而实现简单的DNS负载均衡。 24 | 25 | * `客户端负载`(Balancing-aware Client) 26 | 27 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518155900462-1370526164.png) 28 | 29 | 针对第一个方案的不足,此方案将LB的功能集成到服务消费方进程里,也被称为软负载或者客户端负载方案。服务提供方启动时,首先将服务地址注册到服务注册表,同时定期报心跳到服务注册表以表明服务的存活状态,相当于健康检查,服务消费方要访问某个服务时,它通过内置的LB组件向服务注册表查询,同时缓存并定期刷新目标服务地址列表,然后以某种负载均衡策略选择一个目标服务地址,最后向目标服务发起请求。LB和服务发现能力被分散到每一个服务消费者的进程内部,同时服务消费方和服务提供方之间是直接调用,没有额外开销,性能比较好。 30 | 31 | 该方案主要问题:要用多种语言、多个版本的客户端编写和维护负载均衡策略,使客户端的代码大大复杂化。 32 | 33 | * `独立LB服务`(External Load Balancing Service) 34 | 35 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518170636421-1833253282.png) 36 | 37 | 该方案是针对第二种方案的不足而提出的一种折中方案,原理和第二种方案基本类似。 38 | 39 | 不同之处是将LB和服务发现功能从进程内移出来,变成主机上的一个独立进程。主机上的一个或者多个服务要访问目标服务时,他们都通过同一主机上的独立LB进程做服务发现和负载均衡。该方案也是一种分布式方案没有单点问题,服务调用方和LB之间是进程内调用性能好,同时该方案还简化了服务调用方,不需要为不同语言开发客户库。 40 | 41 | 本篇将介绍第二种负载均衡方法,客户端负载均衡。 42 | 43 | ### 实现gRPC客户端负载均衡 44 | 45 | gRPC已提供了简单的负载均衡策略(如:Round Robin),我们只需实现它提供的`Builder`和`Resolver`接口,就能完成gRPC客户端负载均衡。 46 | 47 | ```go 48 | type Builder interface { 49 | Build(target Target, cc ClientConn, opts BuildOption) (Resolver, error) 50 | Scheme() string 51 | } 52 | ``` 53 | `Builder`接口:创建一个`resolver`(本文称之服务发现),用于监视名称解析更新。 54 | `Build`方法:为给定目标创建一个新的`resolver`,当调用`grpc.Dial()`时执行。 55 | `Scheme`方法:返回此`resolver`支持的方案,`Scheme`定义可参考:https://github.com/grpc/grpc/blob/master/doc/naming.md 56 | 57 | ```go 58 | type Resolver interface { 59 | ResolveNow(ResolveNowOption) 60 | Close() 61 | } 62 | ``` 63 | `Resolver`接口:监视指定目标的更新,包括地址更新和服务配置更新。 64 | `ResolveNow`方法:被 gRPC 调用,以尝试再次解析目标名称。只用于提示,可忽略该方法。 65 | `Close`方法:关闭`resolver` 66 | 67 | 根据以上两个接口,我们把服务发现的功能写在`Build`方法中,把获取到的负载均衡服务地址返回到客户端,并监视服务更新情况,以修改客户端连接。 68 | 修改服务发现代码,`discovery.go` 69 | ```go 70 | package etcdv3 71 | 72 | import ( 73 | "context" 74 | "log" 75 | "sync" 76 | "time" 77 | 78 | "github.com/coreos/etcd/mvcc/mvccpb" 79 | "go.etcd.io/etcd/clientv3" 80 | "google.golang.org/grpc/resolver" 81 | ) 82 | 83 | const schema = "grpclb" 84 | 85 | //ServiceDiscovery 服务发现 86 | type ServiceDiscovery struct { 87 | cli *clientv3.Client //etcd client 88 | cc resolver.ClientConn 89 | serverList map[string]resolver.Address //服务列表 90 | lock sync.Mutex 91 | } 92 | 93 | //NewServiceDiscovery 新建发现服务 94 | func NewServiceDiscovery(endpoints []string) resolver.Builder { 95 | cli, err := clientv3.New(clientv3.Config{ 96 | Endpoints: endpoints, 97 | DialTimeout: 5 * time.Second, 98 | }) 99 | if err != nil { 100 | log.Fatal(err) 101 | } 102 | 103 | return &ServiceDiscovery{ 104 | cli: cli, 105 | } 106 | } 107 | 108 | //Build 为给定目标创建一个新的`resolver`,当调用`grpc.Dial()`时执行 109 | func (s *ServiceDiscovery) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOption) (resolver.Resolver, error) { 110 | log.Println("Build") 111 | s.cc = cc 112 | s.serverList = make(map[string]resolver.Address) 113 | prefix := "/" + target.Scheme + "/" + target.Endpoint + "/" 114 | //根据前缀获取现有的key 115 | resp, err := s.cli.Get(context.Background(), prefix, clientv3.WithPrefix()) 116 | if err != nil { 117 | return nil, err 118 | } 119 | 120 | for _, ev := range resp.Kvs { 121 | s.SetServiceList(string(ev.Key), string(ev.Value)) 122 | } 123 | s.cc.NewAddress(s.getServices()) 124 | //监视前缀,修改变更的server 125 | go s.watcher(prefix) 126 | return s, nil 127 | } 128 | 129 | // ResolveNow 监视目标更新 130 | func (s *ServiceDiscovery) ResolveNow(rn resolver.ResolveNowOption) { 131 | log.Println("ResolveNow") 132 | } 133 | 134 | //Scheme return schema 135 | func (s *ServiceDiscovery) Scheme() string { 136 | return schema 137 | } 138 | 139 | //Close 关闭 140 | func (s *ServiceDiscovery) Close() { 141 | log.Println("Close") 142 | s.cli.Close() 143 | } 144 | 145 | //watcher 监听前缀 146 | func (s *ServiceDiscovery) watcher(prefix string) { 147 | rch := s.cli.Watch(context.Background(), prefix, clientv3.WithPrefix()) 148 | log.Printf("watching prefix:%s now...", prefix) 149 | for wresp := range rch { 150 | for _, ev := range wresp.Events { 151 | switch ev.Type { 152 | case mvccpb.PUT: //新增或修改 153 | s.SetServiceList(string(ev.Kv.Key), string(ev.Kv.Value)) 154 | case mvccpb.DELETE: //删除 155 | s.DelServiceList(string(ev.Kv.Key)) 156 | } 157 | } 158 | } 159 | } 160 | 161 | //SetServiceList 新增服务地址 162 | func (s *ServiceDiscovery) SetServiceList(key, val string) { 163 | s.lock.Lock() 164 | defer s.lock.Unlock() 165 | s.serverList[key] = resolver.Address{Addr: val} 166 | s.cc.NewAddress(s.getServices()) 167 | log.Println("put key :", key, "val:", val) 168 | } 169 | 170 | //DelServiceList 删除服务地址 171 | func (s *ServiceDiscovery) DelServiceList(key string) { 172 | s.lock.Lock() 173 | defer s.lock.Unlock() 174 | delete(s.serverList, key) 175 | s.cc.NewAddress(s.getServices()) 176 | log.Println("del key:", key) 177 | } 178 | 179 | //GetServices 获取服务地址 180 | func (s *ServiceDiscovery) getServices() []resolver.Address { 181 | addrs := make([]resolver.Address, 0, len(s.serverList)) 182 | 183 | for _, v := range s.serverList { 184 | addrs = append(addrs, v) 185 | } 186 | return addrs 187 | } 188 | ``` 189 | 190 | 代码主要修改以下地方: 191 | 192 | 1. 把获取的服务地址转成`resolver.Address`,供gRPC客户端连接。 193 | 194 | 2. 根据`schema`的定义规则,修改`key`格式。 195 | 196 | 服务注册主要修改`key`存储格式,`register.go` 197 | ```go 198 | package etcdv3 199 | 200 | import ( 201 | "context" 202 | "log" 203 | "time" 204 | 205 | "go.etcd.io/etcd/clientv3" 206 | ) 207 | 208 | //ServiceRegister 创建租约注册服务 209 | type ServiceRegister struct { 210 | cli *clientv3.Client //etcd client 211 | leaseID clientv3.LeaseID //租约ID 212 | //租约keepalieve相应chan 213 | keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse 214 | key string //key 215 | val string //value 216 | } 217 | 218 | //NewServiceRegister 新建注册服务 219 | func NewServiceRegister(endpoints []string, serName, addr string, lease int64) (*ServiceRegister, error) { 220 | cli, err := clientv3.New(clientv3.Config{ 221 | Endpoints: endpoints, 222 | DialTimeout: 5 * time.Second, 223 | }) 224 | if err != nil { 225 | log.Fatal(err) 226 | } 227 | 228 | ser := &ServiceRegister{ 229 | cli: cli, 230 | key: "/" + schema + "/" + serName + "/" + addr, 231 | val: addr, 232 | } 233 | 234 | //申请租约设置时间keepalive 235 | if err := ser.putKeyWithLease(lease); err != nil { 236 | return nil, err 237 | } 238 | 239 | return ser, nil 240 | } 241 | 242 | //设置租约 243 | func (s *ServiceRegister) putKeyWithLease(lease int64) error { 244 | //设置租约时间 245 | resp, err := s.cli.Grant(context.Background(), lease) 246 | if err != nil { 247 | return err 248 | } 249 | //注册服务并绑定租约 250 | _, err = s.cli.Put(context.Background(), s.key, s.val, clientv3.WithLease(resp.ID)) 251 | if err != nil { 252 | return err 253 | } 254 | //设置续租 定期发送需求请求 255 | leaseRespChan, err := s.cli.KeepAlive(context.Background(), resp.ID) 256 | 257 | if err != nil { 258 | return err 259 | } 260 | s.leaseID = resp.ID 261 | s.keepAliveChan = leaseRespChan 262 | log.Printf("Put key:%s val:%s success!", s.key, s.val) 263 | return nil 264 | } 265 | 266 | //ListenLeaseRespChan 监听 续租情况 267 | func (s *ServiceRegister) ListenLeaseRespChan() { 268 | for leaseKeepResp := range s.keepAliveChan { 269 | log.Println("续约成功", leaseKeepResp) 270 | } 271 | log.Println("关闭续租") 272 | } 273 | 274 | // Close 注销服务 275 | func (s *ServiceRegister) Close() error { 276 | //撤销租约 277 | if _, err := s.cli.Revoke(context.Background(), s.leaseID); err != nil { 278 | return err 279 | } 280 | log.Println("撤销租约") 281 | return s.cli.Close() 282 | } 283 | ``` 284 | 285 | 客户端修改gRPC连接服务的部分代码即可: 286 | ```go 287 | func main() { 288 | r := etcdv3.NewServiceDiscovery(EtcdEndpoints) 289 | resolver.Register(r) 290 | // 连接服务器 291 | conn, err := grpc.Dial(r.Scheme()+"://8.8.8.8/simple_grpc", grpc.WithBalancerName("round_robin"), grpc.WithInsecure()) 292 | if err != nil { 293 | log.Fatalf("net.Connect err: %v", err) 294 | } 295 | defer conn.Close() 296 | 297 | // 建立gRPC连接 298 | grpcClient = pb.NewSimpleClient(conn) 299 | ``` 300 | gRPC内置了简单的负载均衡策略`round_robin`,根据负载均衡地址,以轮询的方式进行调用服务。 301 | 302 | 服务端启动时,把服务地址注册到`etcd`中即可: 303 | ```go 304 | func main() { 305 | // 监听本地端口 306 | listener, err := net.Listen(Network, Address) 307 | if err != nil { 308 | log.Fatalf("net.Listen err: %v", err) 309 | } 310 | log.Println(Address + " net.Listing...") 311 | // 新建gRPC服务器实例 312 | grpcServer := grpc.NewServer() 313 | // 在gRPC服务器注册我们的服务 314 | pb.RegisterSimpleServer(grpcServer, &SimpleService{}) 315 | //把服务注册到etcd 316 | ser, err := etcdv3.NewServiceRegister(EtcdEndpoints, SerName, Address, 5) 317 | if err != nil { 318 | log.Fatalf("register service err: %v", err) 319 | } 320 | defer ser.Close() 321 | //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 322 | err = grpcServer.Serve(listener) 323 | if err != nil { 324 | log.Fatalf("grpcServer.Serve err: %v", err) 325 | } 326 | } 327 | ``` 328 | 329 | ### 运行效果 330 | 我们先启动并注册三个服务 331 | 332 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201520301-2141314089.png) 333 | 334 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201526062-1105611810.png) 335 | 336 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201529806-1864982377.png) 337 | 338 | 然后客户端进行调用 339 | 340 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201645385-8940133.png) 341 | 342 | 看服务端接收到的请求 343 | 344 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201919646-136721041.png) 345 | 346 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201925163-1429636105.png) 347 | 348 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518201929077-822092499.png) 349 | 350 | 关闭`localhost:8000`服务,剩余`localhost:8001`和`localhost:8002`服务接收请求 351 | 352 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518202359155-2143850614.png) 353 | 354 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518202405272-1990664274.png) 355 | 356 | 重新打开`localhost:8000`服务 357 | 358 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518202655967-135791051.png) 359 | 360 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518202700598-298101288.png) 361 | 362 | ![](https://img2020.cnblogs.com/blog/1508611/202005/1508611-20200518202703530-602882933.png) 363 | 364 | 可以看到,gRPC客户端负载均衡运行良好。 365 | 366 | ### 总结 367 | 本文介绍了gRPC客户端负载均衡的实现,它简单实现了gRPC负载均衡的功能。但在对接其他语言时候比较麻烦,需要每种语言都实现一套服务发现和负载策略,且如果要较为复杂的负载策略,需要修改客户端代码才能完成。 368 | 369 | 下篇将介绍如何实现官方推荐的负载均衡策略(`External Load Balancing Service`)。 370 | 371 | 源码地址:https://github.com/Bingjian-Zhu/etcd-example 372 | 373 | 参考: 374 | 375 | * https://segmentfault.com/a/1190000008672912 376 | 377 | * https://github.com/wothing/wonaming -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 4 | github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 5 | github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 6 | github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 7 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 8 | github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= 9 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= 10 | github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= 11 | github.com/bingjian-zhu/etcd-example v0.0.0-20200509084147-ecf5c76cccde h1:sa9ttAnwsJ+GgzqpHQSs4Iq7h/BJgrOxQAEJweaw4Tc= 12 | github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= 13 | github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= 14 | github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 15 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 16 | github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= 17 | github.com/coreos/etcd v3.3.20+incompatible h1:jIrdkuJDHmyh6VZsxQQ3LQGfOrwgJx6sILz/lxzXsGw= 18 | github.com/coreos/etcd v3.3.20+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 19 | github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= 20 | github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 21 | github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf h1:iW4rZ826su+pqaw19uhpSCzhj44qo35pNgKFGqzDKkU= 22 | github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 23 | github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbpBpLoyyu8B6e44T7hJy6potg= 24 | github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= 25 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 26 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 27 | github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 28 | github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 29 | github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 30 | github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= 31 | github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 32 | github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 33 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 34 | github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= 35 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 36 | github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 37 | github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= 38 | github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= 39 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= 40 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= 41 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 42 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 43 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 44 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 45 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 46 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 47 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 48 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 49 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 50 | github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0= 51 | github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= 52 | github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= 53 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 54 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 55 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 56 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 57 | github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= 58 | github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= 59 | github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 60 | github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= 61 | github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= 62 | github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= 63 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 64 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= 65 | github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= 66 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 67 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 68 | github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= 69 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 70 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 71 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 72 | github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= 73 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 74 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 75 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 76 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 77 | github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 78 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 79 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 80 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 81 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 82 | github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 83 | github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= 84 | github.com/prometheus/client_golang v1.6.0 h1:YVPodQOcK15POxhgARIvnDRVpLcuK8mglnMrWfyrw6A= 85 | github.com/prometheus/client_golang v1.6.0/go.mod h1:ZLOG9ck3JLRdB5MgO8f+lLTe83AXG6ro35rLTxvnIl4= 86 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 87 | github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 88 | github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 89 | github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= 90 | github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 91 | github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= 92 | github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U= 93 | github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= 94 | github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 95 | github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= 96 | github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI= 97 | github.com/prometheus/procfs v0.0.11/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= 98 | github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= 99 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= 100 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= 101 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 102 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 103 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 104 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 105 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 106 | go.etcd.io/etcd v3.3.20+incompatible h1:EyOVslCepyFB2JcbYXvqcYdBTh7cyBKU2NYdKfgTSC0= 107 | go.etcd.io/etcd v3.3.20+incompatible/go.mod h1:yaeTdrJi5lOmYerz05bd8+V7KubZs8YSFZfzsF9A6aI= 108 | go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= 109 | go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= 110 | go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= 111 | go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= 112 | go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= 113 | go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM= 114 | go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= 115 | golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 116 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 117 | golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 118 | golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= 119 | golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= 120 | golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= 121 | golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 122 | golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 123 | golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= 124 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 125 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 126 | golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 127 | golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 128 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 129 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 130 | golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 131 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= 132 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 133 | golang.org/x/net v0.0.0-20200506145744-7e3656a0809f h1:QBjCr1Fz5kw158VqdE9JfI9cJnl/ymnJWAdMuinqL7Y= 134 | golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 135 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 136 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 137 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 138 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 139 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 140 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 141 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 142 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 143 | golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 144 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 145 | golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= 146 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 147 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 148 | golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 149 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 150 | golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 151 | golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25 h1:OKbAoGs4fGM5cPLlVQLZGYkFC8OnOfgo6tt0Smf9XhM= 152 | golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 153 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 154 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 155 | golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= 156 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 157 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 158 | golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 159 | golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 160 | golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= 161 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 162 | golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 163 | golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= 164 | golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 165 | golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 166 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 167 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 168 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 169 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 170 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= 171 | google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= 172 | google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380 h1:xriR1EgvKfkKxIoU2uUvrMVl+H26359loFFUleSMXFo= 173 | google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= 174 | google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= 175 | google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= 176 | google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= 177 | google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= 178 | google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 179 | google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= 180 | google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= 181 | google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4= 182 | google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= 183 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 184 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 185 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 186 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 187 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 188 | google.golang.org/protobuf v1.22.0 h1:cJv5/xdbk1NnMPR1VP9+HU6gupuG9MLBoH1r6RHZ2MY= 189 | google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 190 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 191 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 192 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 193 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 194 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 195 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 196 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 197 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 198 | gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 199 | honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 200 | honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= 201 | honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= 202 | --------------------------------------------------------------------------------