V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
wunonglin
V2EX  ›  Go 编程语言

Consul GRPC 健康检查失败问题

  •  
  •   wunonglin · 2020-11-14 22:19:24 +08:00 · 1640 次点击
    这是一个创建于 1258 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Consul 注册的信息

    reg := api.AgentServiceRegistration{
    	ID:      strconv.FormatInt(time.Now().UnixNano(), 10),
    	Name:    name,
    	Address: listen,
    	Port:    port,
    	Check:   &api.AgentServiceCheck{
    		GRPC:     fmt.Sprintf("%v:%v/%v", listen, port, name),
    		Interval: "5s",
    		Notes:    "Consul check service health status.",
    		DeregisterCriticalServiceAfter: "10s",
    	},
    }
    

    我按照 https://www.consul.io/docs/discovery/checks 里的 grpc 检查里,写了个 proto

    syntax = "proto3";
    
    package grpc_health_v1;
    option go_package = ".;grpc_health_v1";
    
    message HealthCheckRequest {
        string service = 1;
    }
    
    message HealthCheckResponse {
        enum ServingStatus {
            UNKNOWN = 0;
            SERVING = 1;
            NOT_SERVING = 2;
            SERVICE_UNKNOWN = 3;  // Used only by the Watch method.
        }
        ServingStatus status = 1;
    }
    
    service Health {
        rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
    
        rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
    }
    
    

    我用 BloomRPC 和自己写的 client 都能正常访问这个 Check 方法,但是注册上了 Consul 却显示失败

    7 条回复    2020-11-16 15:46:05 +08:00
    axex
        1
    axex  
       2020-11-16 14:18:39 +08:00
    grpc 的 health check 是不需要你实现的,grpc 的库中有 google.golang.org/grpc/health/grpc_health_v1,你直接 import 然后 RegisterHealthServer(grpcServer, health.NewServer())就行了
    wunonglin
        2
    wunonglin  
    OP
       2020-11-16 14:32:54 +08:00
    @axex #1
    我现在改了下代码引入了,但是&api.AgentServiceCheck 里的 GRPC 是要怎么填,

    是 ip:port 还是 ip:port/service_name 还是 ip:port//grpc_health_v1.Health ?我都试了都不行
    axex
        3
    axex  
       2020-11-16 14:52:25 +08:00
    @wunonglin ip:port 就行了,如果不行的检查一下 ip,看看 consul 能否正常访问
    wunonglin
        4
    wunonglin  
    OP
       2020-11-16 15:01:04 +08:00
    @axex #3

    grpc: addrConn.createTransport failed to connect to {127.0.0.1:8080 0 <nil>}. Err :connection error: desc = "transport: Error while dialing dial tcp 127.0.0.1:8080: connect: connection refused". Reconnecting...

    提示这个,但是我自己写了一个 client 测试的话是可以连接上来的
    axex
        5
    axex  
       2020-11-16 15:14:12 +08:00
    @wunonglin consul 和 grpc server 在同一台机器上面?不是的话 consul 是无法访问的,最好改成内网 ip 或者改用 ttl 检查
    wunonglin
        6
    wunonglin  
    OP
       2020-11-16 15:42:52 +08:00
    @axex #5

    OK 了,我 tm 人傻了,consul 是在 docker 上的,然后我填的 check 地址是 127,,我在本地部署了个 consul 可以了
    wunonglin
        7
    wunonglin  
    OP
       2020-11-16 15:46:05 +08:00
    @axex #5 consul 他这文档有坑吧,也没说直接引用官方的 func,还以为要自己写,搜了网上的教程,压根没人提过,我硬是研究了 2 天,浪费一个美好的周末 😑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4111 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:00 · PVG 09:00 · LAX 18:00 · JFK 21:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.