gRPC源码/服务发现

代码

GitHub - messixukejia/grpc-wrapper: wrapper of grpc

关键数据结构

流程处理

关键流程图

gRPC源码/服务发现
1、客户端开工阶段从etcd获取所有服务端地址,并watch服务端变更。
2、服务端变化,推送给watch的客户端。
3、服务端开工阶段,将自己的地址信息put到etcd。

服务端

1、向ETCD建立客户端连接:etcd.New。
2、创建ETCD注册器:NewEtcdRegisty,用于与ETCD的交互,例如注册服务地址等。
3、建立grpc server,并注册相应的服务。
4、启动server,并将自己服务信息通过etcdRegistry.Register
向ETCD注册,在监听客户端连接强求,处理相应业务。

客户端

1、向ETCD建立客户端连接:etcd.New。
2、创建ETCD resolver:NewEtcdResolver,用于与ETCD的交互。
3、建立负载均衡策略,并向服务端Dial。
3.1、newCCResolverWrapper->Builder
.Build(Builder creates a resolver that will be used to watch name resolution updates。默认使用defaultScheme = ”passthrough“。)->passthroughResolver.start->NewAddress->转到3.2
3.2、resolverWrapper.start->ccResolverWrapper.watcher监控地址变化->handleResolvedAddrs/handleServiceConfig

3.3、首次newCCBalancerWrapper->balancerWrapperBuilder.Build->根据制定的负载均衡策略Start->Resolve(创建watcher)->起goroutine监控地址变化watchAddrUpdates(Next中watch etcd数据变化)-> update地址写入addrCh
3.4、lbWatcher监控addrCh->生效到客户端NewSubConn/RemoveSubConn

4、向服务端发起数据请求。

日志

客户端日志:
ETCD相关:
{"level":"info","ts":1528723680.321994,"caller":"client/client.go:64","msg":"begin to create etcd"}
{"level":"info","ts":1528723680.32224,"caller":"grpc/clientconn.go:494","msg":"parsed scheme: \"\""}
{"level":"info","ts":1528723680.322293,"caller":"grpc/clientconn.go:500","msg":"scheme \"\" not registered, fallback to default scheme"}
{"level":"info","ts":1528723680.3223171,"caller":"passthrough/passthrough.go:51","msg":"passthrough newaddress add 127.0.0.1:2379\n"}
{"level":"info","ts":1528723680.322419,"caller":"client/client.go:70","msg":"end etcd"}
{"level":"info","ts":1528723680.322929,"caller":"grpc/resolver_conn_wrapper.go:116","msg":"ccResolverWrapper: sending new addresses to cc: [{127.0.0.1:2379 0  <nil>}]"}
{"level":"warn","ts":1528723680.323195,"caller":"grpc/balancer_v1_wrapper.go:269","msg":"Handle [{127.0.0.1:2379 0  <nil>}]. Err: <nil>"}
{"level":"info","ts":1528723680.32329,"caller":"grpc/balancer_v1_wrapper.go:125","msg":"balancerWrapper: got update addr from Notify: [{127.0.0.1:2379 <nil>}]\n"}

服务侧相关:
2018/06/11 21:28:00 Initializing logging reporter
{"level":"info","ts":1528723680.324028,"caller":"client/client.go:85","msg":"begin to dial"}
{"level":"info","ts":1528723680.324024,"caller":"grpc/balancer_v1_wrapper.go:125","msg":"balancerWrapper: got update addr from Notify: [{127.0.0.1:2379 <nil>}]\n"}
{"level":"info","ts":1528723680.324088,"caller":"grpc/clientconn.go:494","msg":"parsed scheme: \"\""}
{"level":"info","ts":1528723680.324113,"caller":"grpc/clientconn.go:500","msg":"scheme \"\" not registered, fallback to default scheme"}
{"level":"info","ts":1528723680.324137,"caller":"passthrough/passthrough.go:51","msg":"passthrough newaddress add HelloServer\n"}
{"level":"info","ts":1528723680.324235,"caller":"grpc/resolver_conn_wrapper.go:116","msg":"ccResolverWrapper: sending new addresses to cc: [{HelloServer 0  <nil>}]"}
{"level":"warn","ts":1528723680.324367,"caller":"grpc/balancer_v1_wrapper.go:269","msg":"Handle [{HelloServer 0  <nil>}]. Err: <nil>"}
{"level":"info","ts":1528723680.3257082,"caller":"plugins/etcd.go:136","msg":"etcd get upt is {0 127.0.0.1:1234 ...}\n"}
{"level":"info","ts":1528723680.327142,"caller":"grpc/balancer_v1_wrapper.go:125","msg":"balancerWrapper: got update addr from Notify: [{127.0.0.1:1234 ...}]\n"}
2018/06/11 21:28:00 Reporting span 72551c84db313989:72551c84db313989:0:1
{"level":"info","ts":1528723680.431375,"caller":"client/client.go:106","msg":"Greeting: Hello defaultName"}

相关推荐