From d4fa2c82bf99c3d414cff6a5aa052d876a117874 Mon Sep 17 00:00:00 2001 From: oldme <45782393+oldme-git@users.noreply.github.com> Date: Mon, 30 Sep 2024 20:41:14 +0800 Subject: [PATCH] feat(contrib/rpc/grpcx): use grpc.NewClient instead of grpc.Dial (#3684) --- contrib/rpc/grpcx/go.mod | 9 ++++---- contrib/rpc/grpcx/go.sum | 19 ++++++---------- contrib/rpc/grpcx/grpcx_grpc_client.go | 7 +++--- contrib/rpc/grpcx/grpcx_grpc_server.go | 30 +++++++++++++------------- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/contrib/rpc/grpcx/go.mod b/contrib/rpc/grpcx/go.mod index 4cd91ac05..86ed96b74 100644 --- a/contrib/rpc/grpcx/go.mod +++ b/contrib/rpc/grpcx/go.mod @@ -1,14 +1,14 @@ module github.com/gogf/gf/contrib/rpc/grpcx/v2 -go 1.18 +go 1.20 require ( github.com/gogf/gf/contrib/registry/file/v2 v2.7.4 github.com/gogf/gf/v2 v2.7.4 go.opentelemetry.io/otel v1.24.0 go.opentelemetry.io/otel/trace v1.24.0 - google.golang.org/grpc v1.57.2 - google.golang.org/protobuf v1.33.0 + google.golang.org/grpc v1.64.1 + google.golang.org/protobuf v1.34.2 ) require ( @@ -19,7 +19,6 @@ require ( github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/grokify/html-strip-tags-go v0.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect @@ -33,7 +32,7 @@ require ( golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/contrib/rpc/grpcx/go.sum b/contrib/rpc/grpcx/go.sum index 55c614648..7afa6ebd7 100644 --- a/contrib/rpc/grpcx/go.sum +++ b/contrib/rpc/grpcx/go.sum @@ -14,10 +14,6 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -56,15 +52,12 @@ golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/grpc v1.57.2 h1:uw37EN34aMFFXB2QPW7Tq6tdTbind1GpRxw5aOX3a5k= -google.golang.org/grpc v1.57.2/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= +google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/contrib/rpc/grpcx/grpcx_grpc_client.go b/contrib/rpc/grpcx/grpcx_grpc_client.go index 51b77ffc8..47aa27c7b 100644 --- a/contrib/rpc/grpcx/grpcx_grpc_client.go +++ b/contrib/rpc/grpcx/grpcx_grpc_client.go @@ -8,6 +8,7 @@ package grpcx import ( "fmt" + "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -31,11 +32,11 @@ func (c modClient) NewGrpcClientConn(serviceNameOrAddress string, opts ...grpc.D autoLoadAndRegisterFileRegistry() var ( - dialAddress = serviceNameOrAddress + target = serviceNameOrAddress grpcClientOptions = make([]grpc.DialOption, 0) ) if isServiceName(serviceNameOrAddress) { - dialAddress = fmt.Sprintf( + target = fmt.Sprintf( `%s://%s`, gsvc.Schema, gsvc.NewServiceWithName(serviceNameOrAddress).GetKey(), ) @@ -69,7 +70,7 @@ func (c modClient) NewGrpcClientConn(serviceNameOrAddress string, opts ...grpc.D grpcClientOptions = append(grpcClientOptions, c.ChainStream( c.StreamTracing, )) - conn, err := grpc.Dial(dialAddress, grpcClientOptions...) + conn, err := grpc.NewClient(target, grpcClientOptions...) if err != nil { return nil, err } diff --git a/contrib/rpc/grpcx/grpcx_grpc_server.go b/contrib/rpc/grpcx/grpcx_grpc_server.go index 7d14d30d9..c022e6ea9 100644 --- a/contrib/rpc/grpcx/grpcx_grpc_server.go +++ b/contrib/rpc/grpcx/grpcx_grpc_server.go @@ -285,23 +285,23 @@ func (s *GrpcServer) calculateListenedEndpoints(ctx context.Context) gsvc.Endpoi s.Logger().Errorf(ctx, `error retrieving intranet ip: %+v`, err) return nil } - // If no intranet ips found, it uses all ips that can be retrieved, - // it may include internet ip. - if len(intranetIps) == 0 { - allIps, err := gipv4.GetIpArray() - if err != nil { - s.Logger().Errorf(ctx, `error retrieving ip from current node: %+v`, err) - return nil - } - s.Logger().Noticef( - ctx, - `no intranet ip found, using internet ip to register service: %v`, - allIps, - ) - listenedIps = allIps + if len(intranetIps) != 0 { + listenedIps = intranetIps break } - listenedIps = intranetIps + // If no intranet ips found, it uses all ips that can be retrieved, + // it may include internet ip. + allIps, err := gipv4.GetIpArray() + if err != nil { + s.Logger().Errorf(ctx, `error retrieving ip from current node: %+v`, err) + return nil + } + s.Logger().Noticef( + ctx, + `no intranet ip found, using internet ip to register service: %v`, + allIps, + ) + listenedIps = allIps default: listenedIps = []string{addrArray[0]} }