From 92f6c4834912e9857a2d88ad99f601be95461987 Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:29:52 +0800 Subject: [PATCH] fix: NewClientWithTarget miss default config (#3358) --- zrpc/client.go | 14 +++++++------- zrpc/client_test.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/zrpc/client.go b/zrpc/client.go index b0ad08a4..ab0f924a 100644 --- a/zrpc/client.go +++ b/zrpc/client.go @@ -3,6 +3,7 @@ package zrpc import ( "time" + "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/zrpc/internal" "github.com/zeromicro/go-zero/zrpc/internal/auth" @@ -85,15 +86,14 @@ func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) { // NewClientWithTarget returns a Client with connecting to given target. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) { - middlewares := ClientMiddlewaresConf{ - Trace: true, - Duration: true, - Prometheus: true, - Breaker: true, - Timeout: true, + var config RpcClientConf + if err := conf.FillDefault(&config); err != nil { + return nil, err } - return internal.NewClient(target, middlewares, opts...) + config.Target = target + + return NewClient(config, opts...) } // Conn returns the underlying grpc.ClientConn. diff --git a/zrpc/client_test.go b/zrpc/client_test.go index 808f97c0..eb232a82 100644 --- a/zrpc/client_test.go +++ b/zrpc/client_test.go @@ -215,3 +215,15 @@ func TestNewClientWithError(t *testing.T) { ) assert.NotNil(t, err) } + +func TestNewClientWithTarget(t *testing.T) { + _, err := NewClientWithTarget("", + WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), + WithDialOption(grpc.WithContextDialer(dialer())), + WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any, + cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { + return invoker(ctx, method, req, reply, cc, opts...) + })) + + assert.NotNil(t, err) +}