[笔记] consul用grpc做健康检查注意点

先上个代码片段开胃

	registration := &consulapi.AgentServiceRegistration{
		Address: address,
		Port:    port,
		ID:      id,
		Name:    serviceName,
		Check: &consulapi.AgentServiceCheck{
			GRPC:     fmt.Sprintf("%s:%d/%s", address, port, serviceName),
			Interval: "3s",
		},
	}
	err = cli.Agent().ServiceRegister(registration)
	

protobuf写一个Health服务: https://github.com/grpc/grpc/blob/master/doc/health-checking.md

package必须写成grpc.health.v1 ... consul源代码里写死了调用grpc的health check https://github.com/hashicorp/consul/blob/master/agent/checks/grpc.go#L65

HealthCheckRequest中的service字段并不是AgentServiceRegistrationn的Name, 更不是AgentServiceCheck中的Name。看了下源代码才发现,原来是在写GRPC参数时,在endpoint后加个/<service> https://github.com/hashicorp/consul/blob/master/agent/checks/grpc.go#L29

猜你喜欢

转载自my.oschina.net/tuxpy/blog/1798913