|
|
@ -12,6 +12,7 @@ import ( |
|
|
|
"github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery" |
|
|
|
"github.com/go-kratos/kratos/v2/transport/grpc/resolver/discovery" |
|
|
|
|
|
|
|
|
|
|
|
"google.golang.org/grpc" |
|
|
|
"google.golang.org/grpc" |
|
|
|
|
|
|
|
"google.golang.org/grpc/balancer/roundrobin" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// ClientOption is gRPC client option.
|
|
|
|
// ClientOption is gRPC client option.
|
|
|
@ -41,7 +42,7 @@ func WithMiddleware(m middleware.Middleware) ClientOption { |
|
|
|
// WithDiscovery with client discovery.
|
|
|
|
// WithDiscovery with client discovery.
|
|
|
|
func WithDiscovery(d registry.Discovery) ClientOption { |
|
|
|
func WithDiscovery(d registry.Discovery) ClientOption { |
|
|
|
return func(o *clientOptions) { |
|
|
|
return func(o *clientOptions) { |
|
|
|
o.discoverer = d |
|
|
|
o.discovery = d |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -57,7 +58,7 @@ type clientOptions struct { |
|
|
|
endpoint string |
|
|
|
endpoint string |
|
|
|
timeout time.Duration |
|
|
|
timeout time.Duration |
|
|
|
middleware middleware.Middleware |
|
|
|
middleware middleware.Middleware |
|
|
|
discoverer registry.Discovery |
|
|
|
discovery registry.Discovery |
|
|
|
grpcOpts []grpc.DialOption |
|
|
|
grpcOpts []grpc.DialOption |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -83,10 +84,11 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien |
|
|
|
o(&options) |
|
|
|
o(&options) |
|
|
|
} |
|
|
|
} |
|
|
|
var grpcOpts = []grpc.DialOption{ |
|
|
|
var grpcOpts = []grpc.DialOption{ |
|
|
|
|
|
|
|
grpc.WithBalancerName(roundrobin.Name), |
|
|
|
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)), |
|
|
|
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)), |
|
|
|
} |
|
|
|
} |
|
|
|
if options.discoverer != nil { |
|
|
|
if options.discovery != nil { |
|
|
|
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discoverer))) |
|
|
|
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discovery))) |
|
|
|
} |
|
|
|
} |
|
|
|
if insecure { |
|
|
|
if insecure { |
|
|
|
grpcOpts = append(grpcOpts, grpc.WithInsecure()) |
|
|
|
grpcOpts = append(grpcOpts, grpc.WithInsecure()) |
|
|
|