gRPC源码/服务发现

蓝白狂想 · · 2134 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

代码

GitHub - messixukejia/grpc-wrapper: wrapper of grpc

关键数据结构

流程处理

关键流程图

clipboard.png
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"}

有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:蓝白狂想

查看原文:gRPC源码/服务发现

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2134 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传