Golang CONSUL-GRPC Service Registration and discovery

Source: Internet
Author: User
Tags sprintf

In the microservices architecture, each small service is composed of a number of nodes, the addition of nodes to remove the failure to be transparent downstream, it is necessary to introduce a service automatic registration and discovery mechanism, and Consul provides a complete solution, and built-in support for GRPC and HTTP services

Overall architecture

Service Registration and Discovery
    • Service Invocation: Client Direct Connect Server call service
    • Service registration: The server registers the service information in the consul
    • Service discovery: The client discovers service information from Consul, mainly the address of the service
    • Health Check: Consul check the health status of the server

Service Registration

The server registers the service information in the consul, which can be completed when the service is started and service is available.

Complete code reference: Https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/internal/grpcsr/consul_register.go

Config: = API. Defaultconfig () config. Address = r.addressclient, err: = API. Newclient (config) if err! = Nil {panic (err)}agent: = client. Agent () IP: = Localip () Reg: = &api. agentserviceregistration{id:fmt. Sprintf ("%v-%v-%v", R.service, IP, R.port),//Service node name NAME:FMT. Sprintf ("Grpc.health.v1.%v", R.service),//service name Tags:r.tag,//Tag                                             , can be an empty port:r.port,//service port ADDRESS:IP, Service IP Check: &api.  agentservicecheck{//Health Check interval:r.interval.string (),//Health check interval//GRPC support, address for performing health check, service will be passed to grpc:fmt in the Health.check function. Sprintf ("%v:%v/%v", IP, R.port, R.service), DEREGISTERCRITICALSERVICEAFTER:R.DEREGISTERCRITICALSERVICEAFTER.S Tring (),//Logoff time, equivalent to expiry time},}if err: = Agent. Serviceregister (REG); Err! = Nil {panic (err)}

Service discovery

The client discovers the service information from the consul, mainly the address of the service

Complete code reference: Https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/internal/grpclb/consul_resolver.go

services, metainfo, err := w.client.Health().Service(w.service, "", true, &api.QueryOptions{    WaitIndex: w.lastIndex, // 同步点,这个调用将一直阻塞,直到有新的更新})if err != nil {    logrus.Warn("error retrieving instances from Consul: %v", err)}w.lastIndex = metainfo.LastIndexaddrs := map[string]struct{}{}for _, service := range services {    addrs[net.JoinHostPort(service.Service.Address, strconv.Itoa(service.Service.Port))] = struct{}{}}

Health Check

Consul check the health status of the server, consul with the google.golang.org/grpc/health/grpc_health_v1.HealthServer interface, to achieve the GRPC health check support, so we only need to implement this interface, Consul can use this interface for health check

Complete code reference: Https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/cmd/server/main.go

// HealthImpl 健康检查实现type HealthImpl struct{}// Check 实现健康检查接口,这里直接返回健康状态,这里也可以有更复杂的健康检查策略,比如根据服务器负载来返回func (h *HealthImpl) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {    return &grpc_health_v1.HealthCheckResponse{        Status: grpc_health_v1.HealthCheckResponse_SERVING,    }, nil}grpc_health_v1.RegisterHealthServer(server, &HealthImpl{})

Reference links

    • Complete Project code: Https://github.com/hatlonely/hellogolang/tree/master/sample/addservice
    • Consul Health Check Api:https://www.consul.io/api/agent/check.html
    • Consul Service Registration Api:https://www.consul.io/api/agent/service.html
    • GRPC Health Check: https://github.com/grpc/grpc/blob/master/doc/health-checking.md

Reprint Please specify the source
This article link: http://www.hatlonely.com/2018/06/23/golang-consul-grpc-%E6%9C%8D%E5%8A%A1%E6%B3 %a8%e5%86%8c%e4%b8%8e%e5%8f%91%e7%8e%b0/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.