From 6ae2e8712d7f1c8fefa42e3852943315bcdee7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=88=E5=A2=A8=E5=A4=95?= <99347745@qq.com> Date: Wed, 11 Aug 2021 18:51:00 +0800 Subject: [PATCH] fix:etcd<3.13 block question (#1317) --- examples/go.mod | 2 +- examples/go.sum | 4 +-- examples/registry/etcd/client/main.go | 42 +++++++++++++++------------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index d070a1dd1..64048ffd3 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -8,7 +8,7 @@ require ( github.com/envoyproxy/protoc-gen-validate v0.6.1 github.com/gin-gonic/gin v1.7.3 github.com/go-kratos/consul v0.1.4 - github.com/go-kratos/etcd v0.1.2 + github.com/go-kratos/etcd v0.1.3 github.com/go-kratos/gin v0.1.0 github.com/go-kratos/kratos/v2 v2.0.3 github.com/go-kratos/nacos v0.1.1 diff --git a/examples/go.sum b/examples/go.sum index 007f81d57..3b3b8bea3 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -178,8 +178,8 @@ github.com/go-kratos/consul v0.1.2/go.mod h1:FhxdCfkqaqIYfgpGbzGb8ywM/zV3jk+EBS+ github.com/go-kratos/consul v0.1.4 h1:qOOQE/FYpbWLV4cipC+jPrrc/gQY1DdfnX0IJvQleXE= github.com/go-kratos/consul v0.1.4/go.mod h1:egsB9apqg+4BMYVnY/VuQCeC3aAmd7mpYfIMhs4ga4U= github.com/go-kratos/etcd v0.1.0/go.mod h1:RzWYbka3LjS0v2RbCtADN7d2Ng8eH774ehHlTGgbma4= -github.com/go-kratos/etcd v0.1.2 h1:/thmpcEUleRro1bbRN3NfJHFwYzjVvbojvQv1QKGljg= -github.com/go-kratos/etcd v0.1.2/go.mod h1:+MGVcBnNfpiZ2kw05P/m0ijKNLhRz1p0HXWJhO85AJI= +github.com/go-kratos/etcd v0.1.3 h1:ARz1j4nZGgpeSw8WYau5y8LiKbsty/tMASQCaiOGZnM= +github.com/go-kratos/etcd v0.1.3/go.mod h1:4UAwbmm/PIjUS9KwhqmfsBi9pksuvwc3ae+xaKF0Lwk= github.com/go-kratos/gin v0.1.0 h1:yq5GfZnSNo8cOIqxqPE0FVNQ8fm++oKQBd3/rTTp4oI= github.com/go-kratos/gin v0.1.0/go.mod h1:KzE88+mV1QJdmiT+sU2D560ucOXnWZx+4m0OnVcSyzQ= github.com/go-kratos/grpc-gateway/v2 v2.0.0-20210804092615-bdc92eb5ce83 h1:YN1KsqX3mB8hkSrOLZv3xZaZh+lZ/L7QC00RATFLJSg= diff --git a/examples/registry/etcd/client/main.go b/examples/registry/etcd/client/main.go index d7d02c22f..00653466f 100644 --- a/examples/registry/etcd/client/main.go +++ b/examples/registry/etcd/client/main.go @@ -10,6 +10,7 @@ import ( "github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/http" clientv3 "go.etcd.io/etcd/client/v3" + srcgrpc "google.golang.org/grpc" ) func main() { @@ -20,15 +21,8 @@ func main() { panic(err) } r := registry.New(cli) - for { - callHTTP(r) - callGRPC(r) - time.Sleep(time.Second) - } -} -func callGRPC(r *registry.Registry) { - conn, err := grpc.DialInsecure( + connGrpc, err := grpc.DialInsecure( context.Background(), grpc.WithEndpoint("discovery:///helloworld"), grpc.WithDiscovery(r), @@ -36,17 +30,9 @@ func callGRPC(r *registry.Registry) { if err != nil { log.Fatal(err) } - defer conn.Close() - client := helloworld.NewGreeterClient(conn) - reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) - if err != nil { - log.Fatal(err) - } - log.Printf("[grpc] SayHello %+v\n", reply) -} + defer connGrpc.Close() -func callHTTP(r *registry.Registry) { - conn, err := http.NewClient( + connHttp, err := http.NewClient( context.Background(), http.WithEndpoint("discovery:///helloworld"), http.WithDiscovery(r), @@ -55,7 +41,25 @@ func callHTTP(r *registry.Registry) { if err != nil { log.Fatal(err) } - defer conn.Close() + defer connHttp.Close() + + for { + callHTTP(r, connHttp) + callGRPC(r, connGrpc) + time.Sleep(time.Second) + } +} + +func callGRPC(r *registry.Registry, conn *srcgrpc.ClientConn) { + client := helloworld.NewGreeterClient(conn) + reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) + if err != nil { + log.Fatal(err) + } + log.Printf("[grpc] SayHello %+v\n", reply) +} + +func callHTTP(r *registry.Registry,conn *http.Client) { client := helloworld.NewGreeterHTTPClient(conn) reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) if err != nil {