In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Nacos to achieve Raft algorithm". In daily operation, I believe many people have doubts about how to use Nacos to achieve Raft algorithm. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use Nacos to achieve Raft algorithm". Next, please follow the editor to study!
In order to improve understanding, Raft divides the consistency algorithm into several parts, including leader selection (leader selection), log replication (log replication), and safety (security), and uses stronger consistency to reduce the states that must be considered.
Quick understanding of Raft algorithm
Raft is suitable for a protocol that manages log consistency, which is easier to understand and implement than the Paxos protocol Raft. To improve understanding, Raft divides the consistency algorithm into several parts, including leader selection (leader selection), log replication (log replication), and safety (security), and uses stronger consistency to reduce the state that must be considered.
Compared with Paxos,Raft algorithm, it is more intuitive to understand.
The Raft algorithm divides Server into three states, or roles:
Leader: responsible for Client interaction and log replication. At most one exists in the system at a time.
Follower: responds to the request RPC passively and never initiates the request RPC.
Candidate: a temporary role that exists only during the election phase of leader. If a node wants to become leader, it initiates a voting request and becomes candidate itself. If the election is successful, it becomes candidate, otherwise it is returned to follower.
The flow of status or roles is as follows:
In Raft, the problem is broken down into leadership selection, log replication, security, and membership change.
The replication status machine is achieved by copying the log:
Log: each machine keeps a log, which comes from a request from the client and contains a series of commands
State machine: the state machine executes these commands sequentially
Consistency model: in a distributed environment, it ensures that the logs of multiple machines are consistent, so that the state played back to the state machine is consistent.
Selection of main flow by Raft algorithm
There is the concept of Term in Raft. Term is analogous to the change of dynasties in Chinese history. Raft algorithm divides the time into term of any different length.
Election process
Follower adds the current term to candidate.
Candidate votes for itself and sends RequestVote RPC to other servers in the cluster.
Servers that receive RequestVote will only vote for at most one candidate in the same term on a first-come-first-served basis. And will only vote for log, which is at least as new as candidate.
CP consistency in Nacos
Spring Cloud Alibaba Nacos formally supports AP and CP consistency protocols in 1.0.0, and the implementation of CP consistency protocol is based on the simplified CP consistency of Raft.
How to implement Raft algorithm
When Nacos server starts, the RaftCore.init () method is called through the RunningConfig.onApplicationEvent () method.
Start the election
Public static void init () throws Exception {Loggers.RAFT.info ("initializing Raft sub-system"); / / start Notifier, poll Datums, notify RaftListener executor.submit (notifier); / / get Raft cluster node, update to PeerSet peers.add (NamingProxy.getServers ()); long start = System.currentTimeMillis (); / / load Datum and term data from disk for data recovery RaftStore.load () Loggers.RAFT.info ("cache loaded, peer count: {}, datum count: {}, current term: {}", peers.size (), datums.size (), peers.getTerm ()); while (true) {if (notifier.tasks.size () 0) {if (lock.tryLock (INIT_LOCK_TIME_SECONDS, TimeUnit.SECONDS)) {initialized = true; lock.unlock () } else {throw new Exception ("peers is empty.");} Loggers.RAFT.info ("timer started: leader timeout ms: {}, heart-beat timeout ms: {}", GlobalExecutor.LEADER_TIMEOUT_MS, GlobalExecutor.HEARTBEAT_INTERVAL_MS);}
The main things done in the init method are as follows:
Get Raft cluster node peers.add (NamingProxy.getServers ()); Raft cluster data recovery RaftStore.load (); Raft election GlobalExecutor.register (new MasterElection ()); Raft heartbeat GlobalExecutor.register (new HeartBeat ()); Raft publish content Raft to ensure content consistency
Election process
Among them, the internal nodes of the raft cluster are exposed through the Restful interface, and the code is in RaftController. The RaftController controller is used for communication between nodes within the raft cluster. The specific information is as follows:
POST HTTP:// {ip:port} / v1/ns/raft/vote: make a voting request POST HTTP:// {ip:port} / v1/ns/raft/beat: Leader sends heartbeat information GET HTTP:// {ip:port} / v1/ns/raft/peer: get the RaftPeer information of this node PUT HTTP:// {ip:port} / v1/ns/raft/datum/reload: reload a log message POST HTTP : / / {ip:port} / v1/ns/raft/datum: Leader receives the transmitted data and stores it in DELETE HTTP:// {ip:port} / v1/ns/raft/datum: Leader receives the incoming data deletion operation GET HTTP:// {ip:port} / v1/ns/raft/datum: get the data information stored by the node GET HTTP:// {ip:port} / v1/ns/raft/state: get the node's Status information {UP or DOWN} POST HTTP:// {ip:port} / v1/ns/raft/datum/commit: Follower node receives Leader to get data storage operation DELETE HTTP:// {ip:port} / v1/ns/raft/datum: Follower node receives data deletion operation GET HTTP:// {ip:port} / v1/ns/raft/leader: gets Leader node information GET HTTP:/ of the current cluster / {ip:port} / v1/ns/raft/listeners: get all event listeners RaftPeerSet of the current Raft cluster
Heartbeat mechanism
The heartbeat mechanism is used in Raft to trigger leader elections. Heartbeat timing task registers heartbeat timing task through GlobalExecutor.register (new HeartBeat ()) in GlobalExecutor. Specific operations include:
Reset the heart timeout, election timeout of the Leader node
SendBeat () sends heartbeat packet public class HeartBeat implements Runnable {@ Override public void run () {try {if (! peers.isReady ()) {return;} RaftPeer local = peers.local (); local.heartbeatDueMs-= GlobalExecutor.TICK_PERIOD_MS If (local.heartbeatDueMs > 0) {return;} local.resetHeartbeatDue (); sendBeat ();} catch (Exception e) {Loggers.RAFT.warn ("[RAFT] error while sending beat {}", e);}
A brief description of the implementation of Raft consistency in Nacos. For a more detailed process, you can download the source code and check RaftCore for understanding. The source code can be checked out at the following address:
Git clone https://github.com/alibaba/nacos.git
At this point, the study on "how to use Nacos to achieve Raft algorithm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.