consul的grpc健康检查

84 篇文章 3 订阅
31 篇文章 0 订阅

开始使用go-kit进行开发接口,我都会写一个health接口,用于consul的健康检查。
其实开始也想过,如果每一个人都对健康检查接口开发出一个不同的请求体,如果这样,consul怎样对health的接口进行调用呢,但是当时只是一闪而过的想法,并没有想太多,直到我看到注册的服务报红我才意识到,健康检查的接口不应该这么写。
这次我们进行健康检查的接口开发。
首先,我们需要实现consul封装的特定的接口

type HealthServer interface {
	// If the requested service is unknown, the call will fail with status
	// NOT_FOUND.
	Check(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error)
	// Performs a watch for the serving status of the requested service.
	// The server will immediately send back a message indicating the current
	// serving status.  It will then subsequently send a new message whenever
	// the service's serving status changes.
	//
	// If the requested service is unknown when the call is received, the
	// server will send a message setting the serving status to
	// SERVICE_UNKNOWN but will *not* terminate the call.  If at some
	// future point, the serving status of the service becomes known, the
	// server will send a new message with the service's serving status.
	//
	// If the call terminates with status UNIMPLEMENTED, then clients
	// should assume this method is not supported and should not retry the
	// call.  If the call terminates with any other status (including OK),
	// clients should retry the call with appropriate exponential backoff.
	Watch(*HealthCheckRequest, Health_WatchServer) error
}

我们将这两个接口进行简单的实现

type Service struct {}

func (h  Service) Check(context.Context, *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
	return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING} , nil
}

func (h Service) Watch(*grpc_health_v1.HealthCheckRequest, grpc_health_v1.Health_WatchServer) error{
	return nil
}

我们在进行服务注册和健康检查时的配置

func (c ConsulRegister) Register() {
	config := &api.AgentServiceRegistration{
		Address: "127.0.0.1",
		Port: 8085,
		Name: "healthname",
		ID: "healthid",
		Check: &api.AgentServiceCheck{
			GRPC: "127.0.0.1:8085/Health",
			Interval: "5s",
			Timeout: "5s",
		},
	}
	c.Client.Register(config)
}

这样,进行服务注册之后,就会进行健康检查。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据所提供的引用资料,没有明确提到如何指定Consul健康检查地址。引用提到了关于健康检查的一些概念和方法,但没有具体说明如何指定健康检查地址。引用提到了使用Consul的API接口来添加服务和设置健康检查,但同样没有提到如何指定健康检查地址。引用提到了通过命令行参数启动Consul时可以设置一些健康检查的相关参数,但也没有具体说明如何指定健康检查地址。因此,无法给出关于如何指定Consul健康检查地址的明确答案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [consul 服务健康检查](https://blog.csdn.net/xixihahalelehehe/article/details/108165840)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [【注册中心】Consul服务发现、健康检查——基于「Python」「Golang」「gRPC」](https://blog.csdn.net/the_shy_faker/article/details/127425125)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值