英文:
consul health check (All service checks failing)
问题
我已经阅读了与这个问题相关的类似问题。最终以沮丧告终。
- 我使用Docker安装了Consul。我运行了以下命令:
docker run --name consul -d -p 8500:8500 consul
,然后我使用Postman的PUT方法测试了服务注册是否正常工作,但没有启用健康检查,服务成功注册到Consul中,并在Consul的Web界面中显示。目前一切都按预期工作。 - 我有一个用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如下所示。尝试了上述方法。不起作用。
- 使用本地IP地址
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.
- 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. - I have a local GRPC service written in go. The local service address is
127.0.0.1:8880
. The address of consul server is127.0.0.1:8500
. Then the service is registered into consul, but health check didn't work, getsAll service failing
. Then I tried following:
-
- Use local IP address which is
192.168.0.152:8500
as address of consul server and192.168.0.152:8880
as address of gprc service. Not working.
- Use local IP address which is
-
- Use local IP address which is
192.168.0.152:8500
as address of consul server and127.0.0.1:8880
as address of gprc service. Not working.
- Use local IP address which is
-
- 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 := &api.AgentServiceCheck{
HTTP: "http://127.0.0.1:1010/health",
Timeout: "5s",
Interval: "5s",
DeregisterCriticalServiceAfter: "15s",
}
-
- Run
consul members
gets
- Run
Node Address Status Type Build Protocol DC Segment
aae2e6ac1ff8 127.0.0.1:8301 alive server 1.10.3 2 dc1 <all>
Updates:
- 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..
check := &api.AgentServiceCheck{
GRPC: "127.0.0.1:8880",
Timeout: "5s",
Interval: "5s",
DeregisterCriticalServiceAfter: "10s",
}
The code register grpc service to consul server and enable health check is following:
func main() {
IP := flag.String("IP", "127.0.0.1", "IP address")
Port := flag.Int("Port", 8880, "Port")
flag.Parse()
// initialize logger, configuation file and database
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())
}
// 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("%s:%d", global.ServerConfig.ConsulInfo.Host,
global.ServerConfig.ConsulInfo.Port)
client, err := api.NewClient(cfg)
if err != nil {
panic(err)
}
// generate health check instance
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())
}
答案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
关于“正确”的代码有几个问题:
- 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)
- 为什么正确的代码显示
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:
- the address of consul is equal to grpc service? Isn't
cfg.Address
consul'sAddr
?
// get cfg.address from configuration file, in which host is 127.0.0.1 port 8880
cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host,
global.ServerConfig.ConsulInfo.Port)
client, err := api.NewClient(cfg)
- why the right code shows
GRPC:"127.0.0.1:8880/health"
, 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 !!!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论