kitex-contrib / registry-consul

Apache License 2.0
5 stars 11 forks source link

访问RPC服务返回service discovery error: no service found #12

Open wes-xia opened 5 months ago

wes-xia commented 5 months ago

问题描述

在线上RPC服务中(k8s pod中运行)调用PingMetricsClient返回service discovery error: no service found 单独写脚本在pod中执行返回正常

var (
    MetricsClient metricsservice.Client
)

func InitMetricsClient(ctx context.Context, consul_host string, consul_port string) (err error) {
    addr := fmt.Sprintf("%s:%s", consul_host, consul_port)
    r, err := consul.NewConsulResolver(addr)
    if err != nil {
        Debuger(ctx, "NewConsulResolver err:%s", err.Error())
        return
    }

    MetricsClient, err = metricsservice.NewClient(
        "ops.infra.metrics",
        client.WithMuxConnection(1),              // mux
        client.WithRPCTimeout(2*time.Second),     // rpc timeout
        client.WithConnectTimeout(2*time.Second), // conn timeout
        // client.WithFailureRetry(retry.NewFailurePolicy()), // retry
        client.WithResolver(r), // resolver
    )
    if err != nil {
        Debuger(ctx, "InitMetricsClient err:%s", err.Error())
        return
    }
    Debuger(ctx, "InitMetricsClient Success consul_host: %s consul_port: %s ", consul_host, consul_port)
    return nil
}

func PingMetricsClient(ctx context.Context) bool {
    req := &stock.Req{}
    option := callopt.WithRPCTimeout(1 * time.Second)
    _, err := MetricsClient.Ping(context.Background(), req, option)
    if err != nil {
        Debuger(ctx, "PingMetricsClient err:%s", err.Error()) //这里返回service discovery error: no service found
        return false
    }
    return true

}
wes-xia commented 5 months ago

pod单独执行

image

线上服务调用

image

baiyutang commented 5 months ago

针对报错,意思是 找不到可用实例。 这里最好参考下 服务提供者,在注册时的代码,比如注册的地址、或者服务实例的健康检查等等。