Golang using the service discovery System consul

Source: Internet
Author: User
Tags sprintf value store
This is a creation in Article, where the information may have evolved or changed.

The complete code of this article is shown in https://github.com/changjixiong/goNotes/tree/master/consulnotes, if the link is not shown in the text link is sent to be killed, please search to find the original reading.

What is Consul?

"Consul is a distributed, highly available, Datacenter-aware, service discovery and configuration system. It can be used to present services and nodes in a flexible and powerful interface that allows clients -to-date view of the infrastructure they is a part of. "

Quoting an online translation of consul documents (Http://consul.la/intro/what-is-consul)

Consul has multiple components, but overall, it's a tool for discovering and configuring services in your infrastructure. It provides several key features: * Service discovery: Some clients of Consul can provide a service, such as API or MySQL, and other clients can use Consul to discover the provider of the service. With DNS or HTTP, applications can easily find the services they depend on. * Health Check: Consul the client can provide some health checks that can be associated to a specified service (whether the service returns a number of OK) or to a local node (memory usage is below 90%). This information can be used by an operator to monitor the health status of the cluster and be used by the service discovery component to route away from unhealthy hosts. * Key-Value storage: Applications can use the hierarchical key-value store provided by Consul for a number of purposes, including dynamic configuration, feature tagging, collaboration, leader elections, and more. This component can be easily used by a simple HTTP API. * Multi-Datacenter: Consul has very good support for multiple data centers, which means that consul users do not have to worry about multiple areas resulting from creating more layers of abstraction. Consul is designed to be friendly to the DevOps community and application developers, and he is well suited for modern, scalable infrastructure.

Example

There are a lot of documents and instructions about consul on the web, but there is a lack of examples of how to use them, and the next step is to use an example to show how to find a service node. Complete code See https://github.com/changjixiong/goNotes/tree/master/consulnotes.

Suppose that in a system, node A needs to access a service that has n nodes to serve, which are located in the service cluster GROUPB, and Node A only needs to connect to any node in the GROUPB to get the service.

Start consul

Consul provides a development mode for starting a single node service for development debugging, running the command consul Agent-dev start Consul, and a line in the output information

Client addr:127.0.0.1 (http:8500, HTTPS:-1, dns:8600, rpc:8400)

The consul run parameters are displayed and the node and service can be viewed via URL http://127.0.0.1:8500/ui/#/dc1/nodes

Registering a service and adding health checks

The following code will register a service with the consul

Import ("FMT" "Log" "Net/http" Consulapi "Github.com/hashicorp/consul/api") Func Consulcheck (w http. Responsewriter, R *http. Request) {fmt. Fprintln (W, "Consulcheck")}func RegisterServer () {config: = Consulapi. Defaultconfig () client, err: = Consulapi. Newclient (config) if err! = Nil {log. Fatal ("Consul Client Error:", err)} checkport: = 8080 Registration: = new (Consulapi. agentserviceregistration) registration.id = "Servernode_1" registration. Name = "Servernode" registration. Port = 9527 registration. Tags = []string{"Servernode"} registration. Address = "127.0.0.1" registration. Check = &consulapi.  agentservicecheck{HTTP:                          . Fmt. Sprintf ("http://%s:%d%s", registration. Address, Checkport, "/check"), Timeout:                     &NBSP  ; "3s", Interval:                      "5s", Deregistercriticalserviceafter: "30s",//check failed 30 seconds after deletion Except for this service} err = client. Agent (). Serviceregister (registration) If err! = Nil {log. Fatal ("Register Server Error:", err)} http. Handlefunc ("/check", Consulcheck) http. Listenandserve (FMT. Sprintf (":%d", checkport), nil)}

Consulapi. Defaultconfig () source code display by default is the HTTP way to connect "127.0.0.1:8500", the previous article shows that consul development mode is provided by default HTTP service is in the 127.0.0.1 : 8500, in actual use needs to be set to the actual parameters.

Consulapi. HTTP in Agentservicecheck specifies the interface address of the health check, the 127.0.0.1:8080/check,consulcheck function responds to this interface call, returns a 200 status code and a string of "Consulcheck", There are several other ways to check your health, and you can refer to the official documentation.

Consulapi. The Deregistercriticalserviceafter in Agentservicecheck specifies how long to log off the service after the check is not passed, set here to 30 seconds.

The service address registered with Consul is 127.0.0.1:9527 and the following is the Echo service provided on 127.0.0.1:9527.

ln, ERR: = Net. Listen ("TCP", "0.0.0.0:9527")    if nil! = Err {        Panic ("Error:" + Err. Error ())    }    for {        conn, err: = ln. Accept ()        if err! = Nil {            Panic ("Error:" + Err. Error ())        }        go echoserver (conn)    }

After the service is started, Access Http://127.0.0.1:8500/ui/#/dc1/nodes will find "2 services", which will see Servernode 127.0.0.1:9527 on the page, indicating that the service information has been registered. The following information shows the health check passed.

HTTP GET http://127.0.0.1:8080/check:200 OK Output:consulcheck

Using the service

The Service consumer client queries the consul for available services (ignoring error handling) through the following code

Client, Err: = Consulapi. Newclient (Consulapi. Defaultconfig ()///not by default the actual parameters need to be set ... services, err = client. Agent (). Services () ... if _, Found: = services["Servernode_1";!found {            log. Println ("Servernode_1 not Found")            continue}//Find a service named Servernode_1

Connect to service and send data after finding a service

Conn, Err: = Net. Dial ("TCP", FMT. Sprintf ("%s:%d", service. Address, service. Port) ...

The client starts first, the service starts, and then the service shuts down, running the log as follows:

Servernode_1 not Foundget:echoserverhello World, 001get:echoserverhello World, 002...get:echoserverhello World, 008Rea D Buffer error:eofdial tcp 127.0.0.1:9527:getsockopt:connection refuseddial TCP 127.0.0.1:9527:getsockopt:connection Refused...dial TCP 127.0.0.1:9527:getsockopt:connection Refusedservernode_1 not foundservernode_1 not found

Before the service started prompt servernode_1 not found, the service started after the data interaction is normal, after the service shutdown consul has not logged off service client prompt service cannot connect, later consul logout of the failed service, client display service did not find.

Use scenario Assumptions

Suppose a network game has n copies of service nodes to provide services, during production operation, some nodes may fail, some nodes may be too high, some nodes may fail after the self-response needs to be able to re-online service. The consul system allows the gateway server or logical server to obtain the available replica service nodes at any time and forward requests to that node, keeping the replica service efficient and available.

Other types of services can also be scaled horizontally in the same way. Further, it is possible to start a new node at high load, to close some nodes at low load, to implement these on the cloud server, and to reduce the cost by increasing or shutting down the nodes by the load by using the charge.

A little bit of a problem

Service Registration When set check failed after 30 seconds to log off the service, actually running about 80 seconds before logging out of the service, cause unknown origin.

2,948 Reads
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.