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())
}