1.gRPCè´è½½åè¡¡ï¼èªå®ä¹è´è½½åè¡¡çç¥--etcdå®ç°ï¼
gRPCè´è½½åè¡¡ï¼èªå®ä¹è´è½½åè¡¡çç¥--etcdå®ç°ï¼
èæ¯
å¨å·¥ä½å¦ä¹ ä¸ä½¿ç¨gRPCçå°æ¹æ¯è¾å¤ï¼é常æ们é½ä½¿ç¨çæ¯èªå¸¦çè´è½½åè¡¡ç®æ³ï¼ä½æ¯å¨æäºåºæ¯ä¸æ们éè¦å¯¹æå¡ççæ¬è¿è¡æ§å¶æ¯å¦[appV2åªè½å»é¾æ¥userV3],擒牛诀源码å¨è¿æ ·çæ åµä¸å°±åªè½éèªå®ä¹è´è½½åè¡¡çç¥
ç®æ å®ç°åºäºçæ¬ï¼versionï¼çgrpcè´è½½åè¡¡å¨ï¼äºè§£è¿ç¨åå¯èªå·±å®ç°æ´å¤çè´è½½åè¡¡åè½
注åä¸å¿
EtcdLeaseæ¯ä¸ç§æ£æµå®¢æ·ç«¯åæ´»ç¶åµçæºå¶ã群éæäºå ·æçåæ¶é´çç§çº¦ãå¦æetcd群éå¨ç»å®çTTLæ¶é´å æªæ¶å°keepAliveï¼åç§çº¦å°æã为äºå°ç§çº¦ç»å®å°é®å¼åå¨ä¸ï¼æ¯ä¸ªkeyæå¤å¯ä»¥éå ä¸ä¸ªç§çº¦
æå¡æ³¨å(注åæå¡)
å®æ¶ææ¬å°æå¡ï¼APPï¼å°å,çæ¬çä¿¡æ¯æ³¨åå°æå¡å¨
æå¡åç°(客æ·ç«¯åèµ·æå¡è§£æ请æ±ï¼APPï¼)
æ¥è¯¢æ³¨åä¸å¿ï¼APPï¼ä¸æé£äºæå¡
并åææçæå¡å»ºç«HTTP2é¿é¾æ¥
éè¿Etcdwatchçå¬æå¡ï¼APPï¼ï¼éè¿ååæ´æ°é¾æ¥
è´è½½åè¡¡(客æ·ç«¯å起请æ±ï¼APPï¼)
è´è½½åè¡¡éæ©åéçæå¡ï¼APPHTTP2é¿é¾æ¥ï¼
åèµ·è°ç¨
æå¡æ³¨å(注åæå¡)æºç register.go
funcNewRegister(opt...RegisterOptions)(*Register,error){ s:=&Register{ opts:newOptions(opt...),}varctx,cancel=context.WithTimeout(context.Background(),time.Duration(s.opts.RegisterTtl)*time.Second)defercancel()data,err:=json.Marshal(s.opts)iferr!=nil{ returnnil,err}etcdCli,err:=clientv3.New(s.opts.EtcdConf)iferr!=nil{ returnnil,err}s.etcdCli=etcdCli//ç³è¯·ç§çº¦resp,err:=etcdCli.Grant(ctx,s.opts.RegisterTtl)iferr!=nil{ returns,err}s.name=fmt.Sprintf("%s/%s",s.opts.Node.Path,s.opts.Node.Id)//注åèç¹_,err=etcdCli.Put(ctx,s.name,string(data),clientv3.WithLease(resp.ID))iferr!=nil{ returns,err}//ç»çº¦ç§çº¦s.keepAliveChan,err=etcdCli.KeepAlive(context.Background(),resp.ID)iferr!=nil{ returns,err}returns,nil}å¨etcdéé¢æ们å¯ä»¥çå°å¦ä¸ä¿¡æ¯APPv1çæ¬æå¡å¨èç¹çkey/hwholiday/srv/app/app-beb3cb-eb-eb-d-2cfdc7c
{ "node":{ "name":"app","path":"/hwholiday/srv/app","id":"app-beb3cb-eb-eb-d-2cfdc7c","version":"v1","address":"...:"}}APPv2çæ¬æå¡å¨èç¹çkey/hwholiday/srv/app/app-beb3cb-eb-eb-d-2cfdc7c
{ "node":{ "name":"app","path":"/hwholiday/srv/app","id":"app--eb-eb-c0-2cfdc7c","version":"v2","address":"...:"},}æå¡åç°(客æ·ç«¯åèµ·æå¡è§£æ请æ±ï¼APPï¼)æºç discovery.goå®ç°grpcå çresolver.Builderæ¥å£ï¼Builderå建ä¸ä¸ªè§£æå¨ï¼ç¨äºçè§å称解ææ´æ°ï¼
funcNewDiscovery(opt...ClientOptions)resolver.Builder{ s:=&Discovery{ opts:newOptions(opt...),}etcdCli,err:=clientv3.New(s.opts.EtcdConf)iferr!=nil{ panic(err)}s.etcdCli=etcdClireturns}//Buildå½è°ç¨`grpc.Dial()`æ¶æ§è¡func(d*Discovery)Build(targetresolver.Target,ccresolver.ClientConn,optsresolver.BuildOptions)(resolver.Resolver,error){ d.cc=ccres,err:=d.etcdCli.Get(context.Background(),d.opts.SrvName,clientv3.WithPrefix())iferr!=nil{ returnnil,err}for_,v:=rangeres.Kvs{ iferr=d.AddNode(v.Key,v.Value);err!=nil{ log.Println(err)continue}}gofunc(dd*Discovery){ dd.watcher()}(d)returnd,err}//æ ¹æ®å®æ¹ç建议æ们æä»æ³¨åä¸å¿æ¿å°çæå¡ä¿¡æ¯å¨åå°Attributesä¸//Attributescontainsarbitrarydataabouttheresolverintendedfor//consumptionbytheloadbalancingpolicy.//å±æ§å å«æå ³ä¾è´è½½å¹³è¡¡çç¥ä½¿ç¨ç解æå¨çä»»ææ°æ®ã//Attributes*attributes.Attributesfunc(d*Discovery)AddNode(key,val[]byte)error{ vardata=new(register.Options)err:=json.Unmarshal(val,data)iferr!=nil{ returnerr}addr:=resolver.Address{ Addr:data.Node.Address}addr=SetNodeInfo(addr,data)d.Node.Store(string(key),addr)returnd.cc.UpdateState(resolver.State{ Addresses:d.GetAddress()})}è´è½½åè¡¡(客æ·ç«¯å起请æ±ï¼APPï¼)æºç version_balancer.go
gRPCæä¾äºPickerBuilderåPickeræ¥å£è®©æ们å®ç°èªå·±çè´è½½åè¡¡çç¥
//PickerBuilderå建balancer.PickerãtypePickerBuilderinterface{ //Buildè¿åä¸ä¸ªéæ©å¨ï¼gRPCå°ä½¿ç¨å®æ¥éæ©ä¸ä¸ªSubConnãBuild(infoPickerBuildInfo)balancer.Picker}//gRPC使ç¨Pickeræ¥éæ©ä¸ä¸ªSubConnæ¥åéRPCã//æ¯æ¬¡å¹³è¡¡å¨çå é¨ç¶æåçååæ¶ï¼å®é½ä¼ä»å®çå¿«ç §ä¸çæä¸ä¸ªæ°çéæ©å¨ã//gRPC使ç¨çéæ©å¨å¯ä»¥éè¿ClientConn.UpdateState()æ´æ°ãtypePickerinterface{ //éæ©åéçåé¾æ¥åé请æ±Pick(infoPickInfo)(PickResult,error)}ä»ä¸é¢å¾ç¥æ们å¯ä»¥å¹²äºçå°æ¹å¨Buildæ¹æ³æè Pickæ¹æ³ï¼è°ç¨gRPCæ¹æ³æ¶å æ§è¡Buildåæ§è¡Pickï¼
Build(infoPickerBuildInfo)balancer.Pickerinfoéé¢ææå¡çé¾æ¥ï¼åé¾æ¥å¯¹åºçååéè¿AddNodeæ¹æ³åå ¥çæå¡ä¿¡æ¯è¿éæ们å¯ä»¥åºäºgrpc-clientå±é¢æ¥åè´è½½ï¼æ¯å¦ï¼å æéæºè´è½½ï¼
Pick(infoPickInfo)(PickResult,error)infoéé¢æè°ç¨çæ¹æ³ååcontext.Contextéè¿context.Contextæ们å¯ä»¥è·å¾è¿ä¸ªæ¥è·åå起请æ±çæ¶åå¡«å ¥çåæ°ï¼è¿æ ·æ们å¯ä»¥å¾çµæ´»çé对æ¯ä¸ªæ¹æ³è¿è¡ä¸åçè´è½½è¿éæ们å¯ä»¥åºäºgrpc-client-apiå±é¢æ¥åè´è½½
func(*rrPickerBuilder)Build(infobase.PickerBuildInfo)balancer.Picker{ iflen(info.ReadySCs)==0{ returnbase.NewErrPicker(balancer.ErrNoSubConnAvailable)}varscs=make(map[balancer.SubConn]*register.Options,len(info.ReadySCs))forconn,addr:=rangeinfo.ReadySCs{ nodeInfo:=GetNodeInfo(addr.Address)ifnodeInfo!=nil{ scs[conn]=nodeInfo}}iflen(scs)==0{ returnbase.NewErrPicker(balancer.ErrNoSubConnAvailable)}return&rrPicker{ node:scs,}}func(p*rrPicker)Pick(infobalancer.PickInfo)(balancer.PickResult,error){ p.mu.Lock()deferp.mu.Unlock()version:=info.Ctx.Value("version")varsubConns[]balancer.SubConnforconn,node:=rangep.node{ ifversion!=""{ ifnode.Node.Version==version.(string){ subConns=append(subConns,conn)}}}iflen(subConns)==0{ returnbalancer.PickResult{ },errors.New("nomatchfoundconn")}index:=rand.Intn(len(subConns))sc:=subConns[index]returnbalancer.PickResult{ SubConn:sc},nil}客æ·ç使ç¨æ们å®ä¹çversionè´è½½åè¡¡çç¥r:=discovery.NewDiscovery(discovery.SetName("hwholiday.srv.app"),discovery.SetEtcdConf(clientv3.Config{ Endpoints:[]string{ "...:"},DialTimeout:time.Second*5,}))resolver.Register(r)//è¿æ¥æå¡å¨conn,err:=grpc.Dial("hwholiday.srv.app",//没æ使ç¨è¿ä¸ªåæ°grpc.WithDefaultServiceConfig(fmt.Sprintf(`{ "LoadBalancingPolicy":"%s"}`,"version")),grpc.WithInsecure(),)iferr!=nil{ log.Fatalf("net.Connecterr:%v",err)}deferconn.Close()//è°ç¨æå¡apiClient:=api.NewApiClient(conn)ctx:=context.WithValue(context.Background(),"version","v1")_,err=apiClient.ApiTest(ctx,&api.Request{ Input:"v1v1v1v1v1"})iferr!=nil{ fmt.Println(err)}è¿è¡æææµè¯æºç
è¿è¡APPæå¡v1,è°ç¨grpc-client使ç¨v1
APPæå°
å¯å¨æå===>0.0.0.0:
input:"v1v1v1v1v1"
grpc-clientæå°
===RUNTestClient
v1v1v1v1v1v1v1v1v1v1
è¿è¡APPæå¡v1,è°ç¨grpc-client使ç¨v2
APPæå°
å¯å¨æå===>0.0.0.0:
grpc-clientæå°
===RUNTestClient
rpcerror:code=Unavailabledesc=nomatchfoundconn
æ»ç»è¯¦æ ä»ç»å°å
æºç å°å:/hwholiday/learning_tools/tree/master/etcd
éè¿å¦ä¹ æ们å¯ä»¥å®ç°åºäºversionçè´è½½çç¥ï¼è¿éåªæ¯æä¾ä¸ç§æè·¯æä¹å»å®ç°å¯è½æçè¿ä¸ªä¾åä¸å¤ªéåè¿ä¸ªï¼ä½æ¯æä¾äºä¸ç§æè·¯ï¼æ¬¢è¿ä¸èµ·è®¨è®ºã
普渡三牲四果吃了消化不良怎辦? 營養師推1必吃水果緩解脹氣、便秘
如何扒下软件的源码_怎么把软件源码提取出来
长沙溯源码燕窝品牌排行_溯源码燕窝厂家有哪些
金防部士官酒醉擾民 凌晨街上大喊「部隊起床!」
馬爾代夫與印度簽署協議 確認印軍事人員5月10日前撤出馬爾代夫
仿咸鱼网站php源码_仿咸鱼网站php源码是什么