etcd

etcdv3 client的创建流程

Posted by Liangjf on December 31, 2019

etcdv3 client的创建流程

newClient

1、根据配置项来初始化client

2、NewCluster、NewKV、NewLease、NewWatcher、NewAuth

3、自动同步watch集群列表变更 【go client.autoSync()】

3.1、根据AutoSyncInterval时间间隔来同步一次MemberList

3.2、grpc远程调用,获取MemberList

3.2.1、c.MemberList(ctx)

3.2.2、(c *cluster) MemberList

3.2.3、c.remote.MemberList

以下是grpc的rpc.pb.go文件 github.com/coreos/etcd@v3.3.17+incompatible/etcdserver/etcdserverpb/rpc.pb.go:6853

3.2.4、(c *clusterClient) MemberList

3.2.5、c.cc.Invoke

【github.com/coreos/etcd@v3.3.17+incompatible/clientv3/retry.go:165】

3.2.6、服务端响应MemberListgrpc请求

3.2.6.1、(rcc *retryClusterClient) MemberList, rcc.cc.MemberList

【github.com/coreos/etcd@v3.3.17+incompatible/proxy/grpcproxy/cluster.go:158】

3.2.6.2、(cp *clusterProxy) MemberList

3.2.6.3、cp.clus.MemberList(ctx)

【服务端响应 github.com/coreos/etcd@v3.3.17+incompatible/etcdserver/api/v3rpc/member.go:80】

3.2.6.4、(cs *ClusterServer) MemberList

3.2.6.5、cs.cluster.Members()

3.2.6.6、(c *RaftCluster) Members()【因为 EtcdServer 结构的cluster *membership.RaftCluster】

因为每加入集群的节点,都会向 EtcdServer (c *RaftCluster) AddMember请求

func (c *RaftCluster) Members() []*Member {
    c.Lock()
    defer c.Unlock()
    var ms MembersByID
    for _, m := range c.members {
        ms = append(ms, m.Clone())
    }
    sort.Sort(ms)
    return []*Member(ms)
}