Introduction of Consul

Consul is an open source tool based on the GO language that provides service registration, service discovery, and configuration management for distributed, service-oriented systems. Consul offers useful features including: service registration/discovery, health check, Key/Value storage, multi-data center, and distributed consistency assurance. Consul is only a binary executable file, so it is easy to install and deploy Consul. You only need to download Consul from the official website and execute the corresponding startup script.

Consul features

Basic features

1. Service registration/discovery

Why is it necessary to do service registration and service discovery in microservices architecture? The goal of microservices is to break down a monolithic system architecture into fine-grained systems that are divided into functional responsibilities, so that many smaller systems emerge and the number of deployed nodes increases. If you think about it, it’s hard to implement a microservice architecture without a unified service component to manage lists between systems. Consul offers service registration/discovery capabilities that are very good for data consistency and partition fault tolerance, but are slightly worse for cluster availability (compared to Euerka).

2. Ensure data consistency

Consul uses the consistency algorithm Raft to ensure the consistency of service list data across servers in the DATA center. This allows you to obtain the latest service list data from other servers in the same data center when one Server is Down. The side effect of strong data consistency is that the cluster becomes unavailable when data is being synchronized or the Server is electing a Leader.

3. Multiple data centers

Consul supports multiple Data centers and synchronizes Data between multiple Data centers using the Gossip protocol. The advantage of multiple DATA centers is that when one data center fails, other data centers can continue to provide services, improving availability.

4. Health checkup

Consul supports checking basic hardware resources, such as cpus, memory, and hard disks

5. The Key/Value store

Consul supports the Key/Value storage function and can be used as a configuration center. You can configure common configuration information on Consul and obtain the Value of the corresponding Key using the HTTP API provided by Consul.

Advanced features

1.HTTP API

2.ACL

Consul operating mode



As you can see from the figure, Consul includes three different roles: Client, Server, and server-leader. There is another role Agent that is not marked in the figure. There are four roles in total, and their functions will be introduced one by one below.

Agent 1. Agent is a daemon thread. 2. Check and maintain node synchronization

Client 1. Forwards all requests to Server 2. Stateless and non-persistent data 3. Participate in the health check of LAN Gossip

Server 1. Persisting data 2. Forwarding requests to server-leader 3. Participate in the server-leader election. 4. Exchange data with other DCS through WAN Gossip

Server-leader 1. Responds to RPC requests. 2. Synchronizes service list data to the Server

Consul working Principle

How services are registered

You can register the Consul service using either of the following methods: HTTP API & JSON Configuration file Method 1: HTTP API

http://{ip}:8500/v1/agent/service/register/:service
Copy the code

Method 2: JSON configuration file

{
   "services": [{"id": "serverId"."name": "serverName"."tags": [
                           "primary"]."address": "127.0.0.1"."port": 9003."checks": [{"id": "api-servie"."name": "Service 'xx' check"."http": "http://127.0.0.1:9003/public/health"."method": "GET"."interval": "10s"."timeout": "1s"}]}]}Copy the code

Start Consul Adds the boot parameter -config-dir

nohup ./consul agent -dev -config-dir=/consul-conf/service.json &
Copy the code

How services are discovered

There are two methods for discovering services: HTTP API & DNS Agent Method 1: HTTP API

// Obtain the list of healthy services under a service
http://{ip}:8500/v1/health/service/:service
Copy the code

Method 2: DNS Agent

The working process