Browse Source

fix: NewClientWithTarget miss default config (#3358)

MarkJoyMa 1 năm trước cách đây
mục cha
commit
92f6c48349
2 tập tin đã thay đổi với 19 bổ sung7 xóa
  1. 7 7
      zrpc/client.go
  2. 12 0
      zrpc/client_test.go

+ 7 - 7
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.

+ 12 - 0
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)
+}