当前位置: 首页> 汽车> 新车 > grpc NewClient 报错 name resolver error: produced zero addresses

grpc NewClient 报错 name resolver error: produced zero addresses

时间:2025/7/12 5:34:45来源:https://blog.csdn.net/wanmei002/article/details/139303489 浏览次数: 0次

场景

grpc版本: google.golang.org/grpc v1.64.0
连接客户端:

import("google.golang.org/grpc""net"
)
// 拿着设备ID 去获取连接
var connMap map[string]net.Conn
conn, err := grpc.NewClient("device_id",grpc.WithContextDialer(func(ctx context.Context, deviceID string) (net.Conn, error) {return connMap[deviceID]}),
)

上面的代码报错:name resolver error: produced zero addresses

解决方法

conn, err := grpc.NewClient("passthrough:device_id",// 加上 passthrough 协议grpc.WithContextDialer(func(ctx context.Context, deviceID string) (net.Conn, error) {return connMap[deviceID]}),
)

原因

1.64版本之前我们使用下面的代码建立连接

// Deprecated: use NewClient instead.  Will be supported throughout 1.x.
func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {// At the end of this method, we kick the channel out of idle, rather than// waiting for the first rpc.opts = append([]DialOption{withDefaultScheme("passthrough")}, opts...)cc, err := NewClient(target, opts...)if err != nil {return nil, err}......
}

可以看到默认使用的协议是 passthrough,
使用 NewClient 默认使用的协议是 dns

如果不是 passthrough 就会用 url.Parse 来解析地址,这样的话就会解析错误

关键字:grpc NewClient 报错 name resolver error: produced zero addresses

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: