领事健康检查(所有服务检查失败)

huangapple go评论106阅读模式
英文:

consul health check (All service checks failing)

问题

我已经阅读了与这个问题相关的类似问题。最终以沮丧告终。

  1. 我使用Docker安装了Consul。我运行了以下命令:docker run --name consul -d -p 8500:8500 consul,然后我使用Postman的PUT方法测试了服务注册是否正常工作,但没有启用健康检查,服务成功注册到Consul中,并在Consul的Web界面中显示。目前一切都按预期工作。
  2. 我有一个用Go编写的本地GRPC服务。本地服务的地址是127.0.0.1:8880。Consul服务器的地址是127.0.0.1:8500。然后服务被注册到Consul中,但健康检查不起作用,显示为"All service failing"。然后我尝试了以下方法:
    • 使用本地IP地址192.168.0.152:8500作为Consul服务器地址,192.168.0.152:8880作为gRPC服务地址。不起作用。
    • 使用本地IP地址192.168.0.152:8500作为Consul服务器地址,127.0.0.1:8880作为gRPC服务地址。不起作用。
    • 检查HTTP服务是否正常工作。有一个用GIN编写的本地Web服务,端口是1010。AgentServiceCheck如下所示。尝试了上述方法。不起作用。
check := &api.AgentServiceCheck{
      HTTP:                           "http://127.0.0.1:1010/health",
      Timeout:                        "5s",
      Interval:                       "5s",
      DeregisterCriticalServiceAfter: "15s",
   }
  • 运行consul members命令得到以下结果:
Node          Address         Status  Type    Build   Protocol  DC   Segment
aae2e6ac1ff8  127.0.0.1:8301  alive   server  1.10.3  2         dc1  <all>

更新:

  • 在以下配置中将"127.0.0.1:8880/health"更改为"127.0.0.1:8880"后,出人意料地起作用了。不知道为什么...
check := &api.AgentServiceCheck{
      GRPC:                           "127.0.0.1:8880",
      Timeout:                        "5s",
      Interval:                       "5s",
      DeregisterCriticalServiceAfter: "10s",
   }

以下是将gRPC服务注册到Consul服务器并启用健康检查的代码:

func main() {
   IP := flag.String("IP", "127.0.0.1", "IP address")
   Port := flag.Int("Port", 8880, "Port")
   flag.Parse()

   // 初始化日志记录器、配置文件和数据库
   initialize.InitLogger()
   initialize.InitConfig()
   initialize.InitDB()
   zap.S().Info(global.ServerConfig)
   zap.S().Info("IP: ", *IP)
   zap.S().Info("Port: ", *Port)

   server := grpc.NewServer()
   proto.RegisterUserServer(server, &handler.UserServer{})
   l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *IP, *Port))
   if err != nil {
      panic("failed to listen" + err.Error())
   }

   // 注册健康检查
   grpc_health_v1.RegisterHealthServer(server, health.NewServer())

   cfg := api.DefaultConfig()

   // 从配置文件中获取cfg.address,其中主机是127.0.0.1,端口是8880
   cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host,
      global.ServerConfig.ConsulInfo.Port)
   client, err := api.NewClient(cfg)
   if err != nil {
      panic(err)
   }

   // 生成健康检查实例
   check := &api.AgentServiceCheck{
      GRPC:                           "127.0.0.1:8880/health",
      Timeout:                        "5s",
      Interval:                       "5s",
      DeregisterCriticalServiceAfter: "15s",
   }
   registration := new(api.AgentServiceRegistration)
   registration.Name = global.ServerConfig.Name
   registration.ID = global.ServerConfig.Name
   registration.Address = "127.0.0.1"
   registration.Port = 8880
   registration.Tags = []string{"user-srv", "user"}
   registration.Check = check

   err = client.Agent().ServiceRegister(registration)
   if err != nil {
      panic(err)
   }

   err = server.Serve(l)
   if err != nil {
      panic("failed to start grpc" + err.Error())
   }
}
英文:

I have read the similar question related to this problem. It ended up with frustration.

  1. I installed consul with docker. I run the following command. docker run --name consul -d -p 8500:8500 consul, then I tested if the registration of service works using postman PUT method, but didn't enable health check, the service register into consul successfully in the consul web UI. For now everything works as expected.
  2. I have a local GRPC service written in go. The local service address is 127.0.0.1:8880. The address of consul server is 127.0.0.1:8500. Then the service is registered into consul, but health check didn't work, gets All service failing. Then I tried following:
    1. Use local IP address which is 192.168.0.152:8500 as address of consul server and 192.168.0.152:8880 as address of gprc service. Not working.
    1. Use local IP address which is 192.168.0.152:8500 as address of consul server and 127.0.0.1:8880 as address of gprc service. Not working.
    1. Check if HTTP service works. There is a local web service written in GIN. The port is 1010. AgentServiceCheck is following. Tried method above. Not working.
check := &amp;api.AgentServiceCheck{
HTTP:                           &quot;http://127.0.0.1:1010/health&quot;,
Timeout:                        &quot;5s&quot;,
Interval:                       &quot;5s&quot;,
DeregisterCriticalServiceAfter: &quot;15s&quot;,
}
    1. Run consul members gets
Node          Address         Status  Type    Build   Protocol  DC   Segment
aae2e6ac1ff8  127.0.0.1:8301  alive   server  1.10.3  2         dc1  &lt;all&gt;

Updates:

  • After changing &quot;127.0.0.1:8880/health&quot; to &quot;127.0.0.1:8880&quot; in the following config, it surprisingly works. Don't know why..
check := &amp;api.AgentServiceCheck{
GRPC:                           &quot;127.0.0.1:8880&quot;,
Timeout:                        &quot;5s&quot;,
Interval:                       &quot;5s&quot;,
DeregisterCriticalServiceAfter: &quot;10s&quot;,
}

The code register grpc service to consul server and enable health check is following:

func main() {
IP := flag.String(&quot;IP&quot;, &quot;127.0.0.1&quot;, &quot;IP address&quot;)
Port := flag.Int(&quot;Port&quot;, 8880, &quot;Port&quot;)
flag.Parse()
// initialize logger, configuation file and database
initialize.InitLogger()
initialize.InitConfig()
initialize.InitDB()
zap.S().Info(global.ServerConfig)
zap.S().Info(&quot;IP: &quot;, *IP)
zap.S().Info(&quot;Port: &quot;, *Port)
server := grpc.NewServer()
proto.RegisterUserServer(server, &amp;handler.UserServer{})
l, err := net.Listen(&quot;tcp&quot;, fmt.Sprintf(&quot;%s:%d&quot;, *IP, *Port))
if err != nil {
panic(&quot;failed to listen&quot; + err.Error())
}
// register health check
grpc_health_v1.RegisterHealthServer(server, health.NewServer())
cfg := api.DefaultConfig()
// get cfg.address from configuration file, in which host is 127.0.0.1 port 8880
cfg.Address = fmt.Sprintf(&quot;%s:%d&quot;, global.ServerConfig.ConsulInfo.Host,
global.ServerConfig.ConsulInfo.Port)
client, err := api.NewClient(cfg)
if err != nil {
panic(err)
}
// generate health check instance
check := &amp;api.AgentServiceCheck{
GRPC:                           &quot;127.0.0.1:8880/health&quot;,
Timeout:                        &quot;5s&quot;,
Interval:                       &quot;5s&quot;,
DeregisterCriticalServiceAfter: &quot;15s&quot;,
}
registration := new(api.AgentServiceRegistration)
registration.Name = global.ServerConfig.Name
registration.ID = global.ServerConfig.Name
registration.Address = &quot;127.0.0.1&quot;
registration.Port = 8880
registration.Tags = []string{&quot;user-srv&quot;, &quot;user&quot;}
registration.Check = check
err = client.Agent().ServiceRegister(registration)
if err != nil {
panic(err)
}
err = server.Serve(l)
if err != nil {
panic(&quot;failed to start grpc&quot; + err.Error())
}

答案1

得分: 1

根据Consul API的文档(https://www.consul.io/api-docs/agent/check#grpc),似乎你正在使用的GRPC字段支持标准的gRPC健康检查协议。

因此,你只需要在那里定义gRPC端点,协议将处理健康检查。

如果你想要使用HTTP端点进行健康检查,可以通过在代理服务检查配置中定义HTTP字段来选择退出。你可以在文档中找到更多相关信息(https://www.consul.io/api-docs/agent/check#http)。

英文:

Based on the documentation of the Consul API (https://www.consul.io/api-docs/agent/check#grpc), it seems that the GRPC field that you're using

> supports the standard gRPC health checking protocol.

Therefore, you only need to define the gRPC endpoint there, and the protocol will handle the health checks.

You can opt-out from this to use an HTTP endpoint for health checks by defining the HTTP field in your Agent Service Check config. You can find more information about that in the documentation (https://www.consul.io/api-docs/agent/check#http)

答案2

得分: 0

关于“正确”的代码有几个问题:

  1. consul的地址是否等于grpc服务?cfg.Address不是consul的Addr吗?
// 从配置文件中获取cfg.address,其中主机是127.0.0.1,端口是8880
   cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host,
      global.ServerConfig.ConsulInfo.Port)
   client, err := api.NewClient(cfg)
  1. 为什么正确的代码显示GRPC:"127.0.0.1:8880/health",结论是“在以下配置中将127.0.0.1:8880/health更改为127.0.0.1:8880后,它出奇地正常工作。不知道为什么..”。这是不一致的!!!
英文:

There are several problems about the "right" code:

  1. the address of consul is equal to grpc service? Isn't cfg.Address consul's Addr?
// get cfg.address from configuration file, in which host is 127.0.0.1 port 8880
   cfg.Address = fmt.Sprintf(&quot;%s:%d&quot;, global.ServerConfig.ConsulInfo.Host,
      global.ServerConfig.ConsulInfo.Port)
   client, err := api.NewClient(cfg)
  1. why the right code shows GRPC:&quot;127.0.0.1:8880/health&quot;, the conclusion is "After changing "127.0.0.1:8880/health" to "127.0.0.1:8880" in the following config, it surprisingly works. Don't know why..". Which is inconsistent !!!!

huangapple
  • 本文由 发表于 2021年11月4日 21:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69840140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定