1. Overview
1.概述
In this tutorial, we’ll see how Leadership Election with Consul helps to ensure data stability. We’ll provide a practical example of how to manage distributed locking in concurrent applications.
在本教程中,我们将看到Leadership Election与Consul如何帮助确保数据的稳定性。我们将提供一个实际的例子,说明如何在并发应用程序中管理分布式锁定。
2. What Is Consul?
2.什么是领事?
Consul is an open-source tool that provides service registry and discovery based on health checking. Furthermore, it includes a Web Graphical User Interface (GUI) to view and easily interact with Consul. It also covers extra capabilities of session management and Key-Value (KV) store.
Consul是一个开源的工具,它提供了基于健康检查的服务注册和发现。此外,它还包括一个Web图形用户界面(GUI),用于查看和轻松地与Consul进行交互。它还包括会话管理和键值(KV)存储的额外功能。
In the next sections, we’ll focus on how we can use Consul’s session management and KV store to select the leader in applications with multiple instances.
在接下来的章节中,我们将重点讨论如何使用Consul的会话管理和KV存储来选择具有多个实例的应用程序中的领导者。
3. Consul Fundamentals
3.领事基础知识
The Consul agent is the most important component running on each node of a Consul cluster. It’s in charge of health checking; registering, discovering, and resolving services; storing configuration data; and much more.
Consul代理是在Consul集群的每个节点上运行的最重要的组件。它负责健康检查;注册、发现和解析服务;存储配置数据;以及更多。
Consul agent can run in two different modes — Server and Agent.
Consul代理可以以两种不同的模式运行–服务器和代理。
The main responsibilities of the Consul Server are to respond to the queries coming from the agents and to elect the leader. The leadership is selected using the consensus protocol to provide Consistency (as defined by CAP) based on the Raft algorithm.
Consul服务器的主要职责是响应来自代理的查询,并选举领导。使用共识协议选择领导,以提供一致性(由CAP定义),基于Raft算法。
It’s not in the scope of this article to go into detail on how the consensus works. Nevertheless, it’s worth mentioning that the nodes can be in one of three states: leader, candidate, or follower. It also stores the data and responds to queries coming from the agents.
这不是本文的范围,要详细介绍共识是如何工作的。然而,值得一提的是,节点可以处于三种状态之一:领导者、候选人或追随者。它还存储数据并响应来自代理的查询。
Agent is more lightweight than the Consul server. It’s responsible for running the health checking of the registered services and forwarding queries to the server. Let’s see a simple diagram of a Consul cluster:
代理比Consul服务器更轻。它负责运行注册服务的健康检查并将查询转发给服务器。让我们看看一个Consul集群的简单图示。
Consul can also help in other ways — for instance, in concurrent applications in which one instance must be the leader.
Consul还可以在其他方面提供帮助–例如,在并发应用程序中,其中一个实例必须是领导者。
Let’s see in the coming sections how Consul, through session management and KV store, can provide this important capability.
让我们在接下来的章节中看看Consul如何通过会话管理和KV存储来提供这种重要的能力。
4. Leadership Election With Consul
4.与领事的领导选举
In distributed deployments, the service holding the lock is the leader. Therefore, for highly available systems, it is critical to managing locks and leaders.
在分布式部署中,持有锁的服务是领导者。因此,对于高可用系统来说,管理锁和领导是至关重要的。
Consul provides an easy-to-use KV store and session management. Those functionalities serve to build leader election, so let’s learn the principles behind them.
Consul提供了一个易于使用的KV存储和会话管理。这些功能是为建立领导人选举服务的,所以让我们来学习它们背后的原理。
4.1. Leadership Contention
4.1.领导力的争夺
The first thing all the instances belonging to the distributed system do is compete for the leadership. The contention for being a leader includes a series of steps:
属于分布式系统的所有实例做的第一件事就是争夺领导权。争夺领导权包括一系列的步骤。
- All the instances must agree on a common key to contend.
- Next, the instance creates a session using the agreed key through Consul session management and KV capabilities.
- Third, they should acquire the session. If the return value is true, the lock belongs to the instance, and if false, the instance is a follower.
- The instances need to continually watch for the session to acquire the leadership again in case of failure or release.
- Finally, the leader can release the session, and the process begins again.
Once the leader is elected, the rest of the instances use Consul KV and session management to discover the leader by:
一旦领导者被选出,其余的实例就会使用Consul KV和会话管理来发现领导者的情况。
- Retrieving the agreed key
- Getting session information to know the leader
4.2. A Practical Example
4.2.一个实际的例子
We need to create the key and the value together with the session in Consul with multiple instances running. To help with this, we’ll use the Kinguin Digital Limited Leadership Consul open-source Java implementation.
我们需要在运行多个实例的情况下,在Consul中一起创建键和值以及会话。为了帮助解决这个问题,我们将使用Kinguin Digital Limited Leadership Consul开源的Java实现。
First, let’s include the dependency:
首先,让我们包括依赖性。
<dependency>
<groupId>com.github.kinguinltdhk</groupId>
<artifactId>leadership-consul</artifactId>
<version>${kinguinltdhk.version}</version>
<exclusions>
<exclusion>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
</exclusion>
</exclusions>
</dependency>
We excluded the consul-api dependency to avoid collisions on the different versions in Java.
我们排除了consul-api的依赖关系,以避免在Java中不同版本的碰撞。
For the common key, we’ll use:
对于通用钥匙,我们将使用。
services/%s/leader
Let’s test all of the process with a simple snippet:
让我们用一个简单的片段来测试所有的过程。
new SimpleConsulClusterFactory()
.mode(SimpleConsulClusterFactory.MODE_MULTI)
.debug(true)
.build()
.asObservable()
.subscribe(i -> System.out.println(i));
Then we create a cluster with multiple instances with asObservable() to facilitate access to events by subscribers. The leader creates a session in Consul, and all the instances verify the session to confirm leadership.
然后我们用asObservable()创建一个有多个实例的集群,以方便订阅者访问事件。领导在Consul中创建了一个会话,所有的实例都会验证这个会话以确认领导地位。
Finally, we customize the consul configuration and session management, and the agreed key between the instances to elect the leader:
最后,我们定制领事配置和会话管理,以及实例之间商定的密钥来选举领导者。
cluster:
leader:
serviceName: cluster
serviceId: node-1
consul:
host: localhost
port: 8500
discovery:
enabled: false
session:
ttl: 15
refresh: 7
election:
envelopeTemplate: services/%s/leader
4.3. How to Test It
4.3.如何测试
There are several options to install Consul and run an agent.
有几个选项可以安装Consul和运行代理。
One of the possibilities to deploy Consul is through containers. We’ll use the Consul Docker image available in Docker Hub, the world’s largest repository for container images.
部署Consul的可能性之一是通过containers。我们将使用Docker Hub中提供的Consul Docker镜像,这是世界上最大的容器镜像库。
We’ll deploy Consul using Docker by running the command:
我们将通过运行命令使用Docker部署Consul。
docker run -d --name consul -p 8500:8500 -e CONSUL_BIND_INTERFACE=eth0 consul
Consul is now running, and it should be available at localhost:8500.
Consul现在正在运行,它应该可以在localhost:8500上使用。
Let’s execute the snippet and check the steps done:
让我们执行这个片段并检查所做的步骤。
- The leader creates a session in Consul.
- Then it is elected (elected.first).
- The rest of the instances watch until the session is released:
INFO: multi mode active
INFO: Session created e11b6ace-9dc7-4e51-b673-033f8134a7d4
INFO: Session refresh scheduled on 7 seconds frequency
INFO: Vote frequency setup on 10 seconds frequency
ElectionMessage(status=elected, vote=Vote{sessionId='e11b6ace-9dc7-4e51-b673-033f8134a7d4', serviceName='cluster-app', serviceId='node-1'}, error=null)
ElectionMessage(status=elected.first, vote=Vote{sessionId='e11b6ace-9dc7-4e51-b673-033f8134a7d4', serviceName='cluster-app', serviceId='node-1'}, error=null)
ElectionMessage(status=elected, vote=Vote{sessionId='e11b6ace-9dc7-4e51-b673-033f8134a7d4', serviceName='cluster-app', serviceId='node-1'}, error=null)
Consul also provides a Web GUI available at http://localhost:8500/ui.
Consul还提供了一个Web GUI,可在http://localhost:8500/ui。
Let’s open a browser and click the key-value section to confirm that the session is created:
让我们打开浏览器,点击键值部分,确认会话已经创建。
Therefore, one of the concurrent instances created a session using the agreed key for the application. Only when the session is released can the process start over, and a new instance can become a leader.
因此,其中一个并发的实例使用约定的密钥为该应用程序创建了一个会话。只有当会话被释放时,这个过程才能重新开始,一个新的实例才能成为领导者。
5. Conclusion
5.总结
In this article, we showed the Leadership Election fundamentals in high-performance applications with multiple instances. We demonstrated how session management and KV store capabilities of Consul can help acquire the lock and select the leader.
在这篇文章中,我们展示了具有多个实例的高性能应用程序中的领导力选举基本原理。我们展示了Consul的会话管理和KV存储功能如何帮助获得锁和选择领导。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。