This commit is contained in:
kzkzzzz
2025-03-23 20:29:29 +08:00
parent 7e0bf82418
commit 9fd0eaadb8
14 changed files with 661 additions and 78 deletions

View File

@@ -75,7 +75,7 @@ func New(grpcUrl string, opts ...Opt) (*grpc.ClientConn, error) {
PermitWithoutStream: true, // 如果没有active的stream,是否允许发送ping
}),
// 参考 https://github.com/grpc/grpc-go/tree/master/examples/features/load_balancing 设置轮训策略
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`), // This sets the initial balancing policy.
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`), // This sets the initial balancing policy.
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
@@ -87,13 +87,11 @@ func New(grpcUrl string, opts ...Opt) (*grpc.ClientConn, error) {
grpc.WithReadBufferSize(mygrpc.DefaultReadBufferSize),
grpc.WithWriteBufferSize(mygrpc.DefaultWriteBufferSize),
grpc.WithUnaryInterceptor(WrapRequestError()),
)
}
if len(c.unaryMiddlewares) > 0 {
grpc.WithChainUnaryInterceptor(c.unaryMiddlewares...)
dialOpts = append(dialOpts, grpc.WithChainUnaryInterceptor(c.unaryMiddlewares...))
}
if len(c.grpcOpts) > 0 {
@@ -123,12 +121,15 @@ func WrapRequestError() grpc.UnaryClientInterceptor {
err := invoker(ctx, method, req, reply, cc, opts...)
if err != nil {
st, ok := status.FromError(err)
if ok && serviceName != "" {
sp := st.Proto()
sp.Message = fmt.Sprintf("[%s] - %s", serviceName, sp.Message)
if serviceName != "" {
return fmt.Errorf("request grpc err: [%s - %s] %s", serviceName, method, err)
return status.ErrorProto(sp)
}
return fmt.Errorf("request grpc err: [%s] %s", method, err)
return err
}
return nil