Distributed Service Consul Introduction and C # and Consul distributed system coordination

Source: Internet
Author: User
Tags assert failover http request json message queue zip zookeeper etcd

A brief introduction of distributed service registration and Discovery Consul

Consul is an open source tool launched by Hashicorp company to realize the service discovery and configuration of distributed system. With other distributed services registration and Discovery Program, Consul's scheme is more "one-stop", built-in service registration and discovery Framework, distributed consistency Protocol implementation, health check, Key/value storage, multiple data center solution, no longer need to rely on other tools (such as zookeeper, etc.). It is also simpler to use. Consul is implemented with Golang and therefore has natural portability (support for Linux, Windows, and Mac OS X); the installation package contains only one executable file for easy deployment and can be seamlessly coordinated with lightweight containers such as Docker.

Consul Usage Scenarios

Registration and configuration sharing for Docker instances
Registration and configuration sharing for CoreOS instances
Vitess Cluster
Configuration Sharing for SaaS applications
Integrates with CONFD services to dynamically generate Nginx and Haproxy configuration files

Advantages of Consul

Using the Raft algorithm to ensure consistency is more straightforward than a complex paxos algorithm. In contrast, Zookeeper uses Paxos, and ETCD uses Raft.
Support for multiple data centers, internal and external network services using different ports for monitoring. Multi-data center cluster can avoid single point of failure of single data center, and its deployment needs to consider network delay, fragmentation and so on. Both zookeeper and ETCD do not provide support for multiple data center features.
Support for health checks. ETCD does not provide this functionality.
Supports HTTP and DNS protocol interfaces. Zookeeper integration is more complex, ETCD only supports HTTP protocol.
The Web admin interface is provided by the authorities, Etcd without this function.

Comprehensive comparison, consul as a new star of service registration and configuration management, is worthy of concern and research.

the role of consul

Client: Clients, stateless, forwarding HTTP and DNS interface requests to server-side clusters within the LAN.
Server: Servers, save configuration information, highly available clusters, communicate with local clients over a LAN, and communicate with other data centers over a wide area network. The number of servers per data center is recommended as 3 or 5.


. NET client https://github.com/PlayFab/consuldotnet/, Nuget https://www.nuget.org/packages/Consul/

Specific article Reference https://playfab.com/blog/2015/05/19/porting-consul-go-api-c



Distributed System Coordination using C # and Consul

In the coordination of distributed applications, the main encounter is the following scenarios:

Business Discovery (Service discovery)

Find the services and nodes that are available in the distributed system

First Name Services (name service)

Know the corresponding resource by the given name

Configuration management (Configuration management)

How to share a configuration file in a distributed node to ensure consistency.

Fault discovery and failover (failure detection and failover)

When a node fails, how to detect and notify other nodes, or transfer the service you want to other available nodes

Leadership election (leader election)

How to elect a leader in a multitude of nodes to coordinate all nodes

Distributed locks (distributed exclusive lock)

How to synchronize in a distributed service by locking

Messaging and Notification Services (message queue and notification)

How to deliver messages in distributed services and respond proactively to events in the form of notifications

Consul

Consul is a distributed service coordination management tool with go development, which provides service discovery, health checking, key/value storage, and support across data center capabilities. Some of the key features offered by Consul are:

Service Discovery:consul makes it easy to register and service discovery through DNS or HTTP interfaces, and some external services, such as those provided by SaaS, can be registered as well.
Health checking: Health checks enable Consul to quickly alert the operation in the cluster. Integration with service discovery prevents the service from forwarding to the failed service.
Key/value Storage: A system for storing dynamic configurations. Provides a simple HTTP interface that can be manipulated anywhere.
Multi-datacenter: Any number of zones can be supported without complex configuration.

Consul an HTTP based API can be easily bound with various languages, C # language binding Https://github.com/PlayFab/consuldotnet

Consul runs an agent on each node of the cluster, which can use either server or client mode. The client is responsible for efficient communication to the server and is relatively stateless. The server is responsible for including the Electoral Leadership node, maintaining cluster status, responding to all queries, communicating across the data center, and so on.

Consul official website has compiled a good binary package, supporting a variety of platforms: win, Linux and so on, download the package in line with your platform: here, download package: 0.5.2_windows_386.zip. There is only one consul file after the decompression is complete.

D:\github\consuldotnet\consul.test>consul

Usage:consul [--version] [--help] <command> [<args>]

Available commands are:

Agent runs a consul agent

Configtest Validate config file

Event Fire a new event

EXEC executes a command on consul nodes

Force-leave forces a member of the ' cluster ' to enter the ' left ' state

Info provides debugging information for operators

Join tell Consul agent to join cluster

Keygen generates a new encryption key

Keyring manages gossip layer encryption keys

Leave gracefully leaves the consul cluster and shuts down

Lock Execute a command holding a lock

Maint Controls node or service maintenance mode

Members Lists the members of a consul cluster

Monitor Stream logs from a consul agent

Reload triggers the agent to reload configuration files

Version Prints the consul version

Watch watch for changes in consul

After the consul is installed, the agent can be started and the agent can run in server or client mode, with at least one agent running in server mode for each datacenter, and 3 or 5 servers are generally recommended. Deploying a single server is very bad because data loss is unavoidable in a failed scenario. This article covers the creation of a new datacenter, all other agents running in client mode, a very lightweight service registration process that runs health monitoring and forwards query results to services. The agent must be running on every node in the cluster.

Consul.exe Agent-config-file Test_config.json

We first run an agent in server mode:

d:\github\consuldotnet\consul.test> Consul.exe Agent-config-file Test_config.json

==> warning:bootstrap Mode enabled! Do not enable unless necessary

==> Warning:windows is not recommended as a consul server. Don't use in production.

==> Warning:it is highly recommended to set gomaxprocs higher than 1

==> Starting Consul Agent ...

==> starting Consul Agent RPC ...

==> Consul Agent running!

Node name: ' GEFFZHANG-NB '

Datacenter: ' DC1 '

Server:true (Bootstrap:true)

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

Cluster addr:192.168.1.4 (lan:8301, wan:8302)

Gossip Encrypt:false, Rpc-tls:false, Tls-incoming:false

Atlas: <disabled>


==> Log data would now stream in as it occurs:


2015/08/09 09:14:48 [INFO] SERF:EVENTMEMBERJOIN:GEFFZHANG-NB 192.168.1.4

2015/08/09 09:14:48 [INFO] SERF:EVENTMEMBERJOIN:GEFFZHANG-NB.DC1 192.168.1.4

2015/08/09 09:14:48 [INFO] raft:node at 192.168.1.4:8300 [Follower] entering Follower state

2015/08/09 09:14:48 [INFO] consul:adding server GEFFZHANG-NB (addr:192.168.1.4:8300) (DC:DC1)

2015/08/09 09:14:48 [INFO] consul:adding server GEFFZHANG-NB.DC1 (addr:192.168.1.4:8300) (DC:DC1)

2015/08/09 09:14:48 [ERR] agent:failed to sync remote state:no cluster

2015/08/09 09:14:50 [WARN] raft:heartbeat timeout reached, starting election

2015/08/09 09:14:50 [INFO] raft:node at 192.168.1.4:8300 [candidate] entering candidate state

2015/08/09 09:14:50 [DEBUG] raft:votes needed:1

2015/08/09 09:14:50 [DEBUG] raft:vote granted. Tally:1

2015/08/09 09:14:50 [INFO] Raft:election won. Tally:1

2015/08/09 09:14:50 [INFO] raft:node at 192.168.1.4:8300 [Leader] entering Leader state

2015/08/09 09:14:50 [INFO] consul:cluster leadership acquired

2015/08/09 09:14:50 [INFO] consul:new leader ELECTED:GEFFZHANG-NB

2015/08/09 09:14:50 [INFO] raft:disabling Enablesinglenode (Bootstrap)

2015/08/09 09:14:50 [DEBUG] Raft:node 192.168.1.4:8300 updated peer set (2): [192.168.1.4:8300]

2015/08/09 09:14:50 [DEBUG] consul:reset tombstone GC to index 2

2015/08/09 09:14:50 [INFO] consul:member ' GEFFZHANG-NB ' joined, marking health alive

2015/08/09 09:14:51 [INFO] agent:synced service ' consul '

2015/08/09 09:16:03 [DEBUG] Agent:service ' Consul ' in sync

2015/08/09 09:17:30 [DEBUG] Agent:service ' Consul ' in sync

2015/08/09 09:18:38 [DEBUG] Agent:service ' Consul ' in sync

2015/08/09 09:19:47 [DEBUG] http:request/v1/status/peers (0)

2015/08/09 09:19:52 [DEBUG] Agent:service ' Consul ' in sync

As you can see, the consul agent has been started, and some logs have been printed to the terminal, and it is possible to see from the log that our agent is already running in server mode and is already the leading node of the entire cluster. Also, local members have been marked as healthy members of the cluster. This is when you run the consul members in another terminal to see the entire cluster. At this time you can only see yourself, because our cluster has not joined the other members.



The output already shows your own node information, address information, health status, role in the cluster, and some version information, and if you want to see some metadata, you can add the-detailed tag

The information that the Consul members command outputs is based on the gossip protocol and is ultimately consistent.


KV Basic Operation

Consul provides a simple k/v storage system that can be used to dynamically acquire configurations, perform service coordination, master-node elections, build processes that other developers can think of, and so on.

var client = new Client ();

var kv = client. KV;

var key = Generatetestkeyname ();

var value = Encoding.UTF8.GetBytes ("test");

var getrequest = kv. Get (key);

Assert.isnull (Getrequest.response);

var pair = new Kvpair (key)

{

Flags = 42,

Value = value

};

var putrequest = kv. Put (pair);

Assert.istrue (Putrequest.response);

Getrequest = kv. Get (key);

var res = Getrequest.response;

Assert.isnotnull (RES);

Assert.istrue (StructuralComparisons.StructuralEqualityComparer.Equals) (value, Res. Value));

Assert.AreEqual (pair. Flags, Res. Flags);

Assert.istrue (Getrequest.lastindex > 0);

var del = kv. Delete (key);

Assert.istrue (Del. Response);

Getrequest = kv. Get (key);

Assert.isnull (Getrequest.response);


Services Discovery (Service Discovery) and Health Check (Health check)

Another major feature of consul is that it is used to manage distributed services, where users can register a service and provide health-testing capabilities for the service.

Service definition: A service can dynamically add, delete, and modify services by providing a service-defined profile or by invoking the HTTP API.

Service query: Once the agent is started and the service is synchronized, we can use DNS or HTTP APIs to query.

Service upgrades: Service definition upgrades can be done by first modifying the service definition profile and then sending an SIGHUP signal to the agent, allowing you to upgrade the service without the agent downtime or the service being unreachable. or through the HTTP API interface to dynamically add, delete, modify services.

The consul supports three check modes:

Calls an external script (script) in which consul invokes an external script that obtains the health status of the corresponding service through the return content of the script.
Call HTTP, in which case consul calls an HTTP request, returns 2XX, then health; 429 (Too many request) is a warning. All others are unhealthy
Proactive escalation, in which the service needs to proactively invoke a consul-provided HTTP put request to escalate the health status.

The C # API provides the corresponding interface

Client.Agent.Service
Client.Agent.Check

Consul's health check, which checks the state of the service by invoking a script, HTTP, or proactive escalation, is more flexible, can get more information, but it also needs to do more work.


fault detection (failure detection)

Consul provides the concept of session, using session to check whether the service survives. For each service we can create a session object, note here we set the Ttl,consul will take the TTL value as the interval time, continues to examine the sessions ' survival. Corresponding in the service, we need a continuous renew session to ensure that the session is legitimate.

var client = new Client ();

var sessionrequest = client. Session.create (New Sessionentry () {TTL = Timespan.fromseconds (10)});

var id = sessionrequest.response;

Assert.istrue (sessionRequest.RequestTime.TotalMilliseconds > 0);

Assert.isfalse (String. IsNullOrEmpty (Sessionrequest.response));

var tokensource = new CancellationTokenSource ();

var ct = Tokensource.token;

Client. Session.renewperiodic (Timespan.fromseconds (1), ID, writeoptions.empty, CT);

Tokensource.cancelafter (3000);

Task.delay (3000, CT). Wait (CT);

var inforequest = client. Session.info (ID);

Assert.istrue (Inforequest.lastindex > 0);

Assert.isnotnull (Inforequest.knownleader);

Assert.AreEqual (ID, InfoRequest.Response.ID);

Assert.istrue (client. Session.destroy (ID). Response);

Note here, because it is based on the TTL (minimum 10 seconds) detection, from the business interruption to be detected, at least 10 seconds of delay, corresponding to the need for real-time response scenarios, does not apply.


Leadership elections and distributed locks

This document describes how to use the consul KV storage to achieve leader election, using the consul KV function, can be very convenient to achieve leadership of the election and lock functions.


WEB UI

Consul also supports the Web interface, which can be used to view all services and nodes, all health detections and their current status, and to read the settings k/v system values. The UI automatically supports multiple datacenter by default. These UI are static HTML you do not need to run a single Web server, the consul agent itself can configure a Web service.

Downloading UI components: WEB UI
After the download is completed is a 0.5.2_web_ui.zip compressed file, after decompression is a dist directory. Then add the-ui-dir parameter and the-client parameter to restart the agent.

d:\github\consuldotnet\consul.test> Consul.exe Agent-config-file test_config.json-ui-dir=d:\github\ Consuldotnet\consul.test\0.5.2_web_ui\dist

Enter http://127.0.0.1:8500 in the browser to access the UI



Have services, nodes, k/v, ACL, Datacenter Management, a perfect system.


Summarize

Consul, as distributed coordination with the Go language development, provides good support for business Discovery Management, and his HTTP APIs are well aligned with different languages and support applications across data centers. The disadvantage is relatively new, suitable for people who like to try new things.

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.