Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the election based on zookeeper leader by Curator

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to achieve Curator election based on zookeeper leader, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

First, basic introduction

Curator Framework is a hierarchical API for zookeeper, which greatly simplifies the use of zookeeper. It builds many functions based on zookeeper, handles complex link management, and retries operations. Here are some of its features:

1, automatic connection management.

A), a potential error condition that requires the ZooKeeper client to create a connection and / or retry the operation. Curator handles these situations automatically and transparently (primarily).

B), monitor the NodeDataChanged event and call updateServerList () as needed.

C), Watches will be automatically removed by Curator recipes.

2, concise API

A) to provide a modern and concise interface

B), simplifies zookeeper native methods, events, etc.

3, realized a lot of functions

A), Leader election

B), shared lock

C), queue

D), Barriers

E), Counters

F), Caches

G), Nodes

This article is mainly about leader election.

Now in the distributed system, the basic architecture model architecture is master/slaves

Second, leader election

In distributed systems, it is generally master/slaves structure. In order to recover disasters, we usually want to start multiple master, one Leader, and a number of Followers,Leader outages. If there is a Followers, choose one to do Leader. The reason why Leader needs to be elected among multiple Master is actually to ensure strong consistency of data.

Zookeeper is a good framework for us to do Leader election and configuration.

CuratorFramework

This kind of instance is thread-safe, and it is better for an application to share one. You can create an instance through CuratorFrameworkFactory.

The main class of Leader election, two constructs

Public LeaderLatch (CuratorFramework client, String latchPath) Parameters: client-the client latchPath-the path for this leadership grouppublic LeaderLatch (CuratorFramework client, String latchPath, String id) Parameters: client-the client latchPath-the path for this leadership groupid-participant ID after building the LeaderLatch object, we need the object to call the start method to start, and we can then determine whether the current instance is Leader by using public boolean hasLeadership () Return true if leadership is currently held by this instance. The Leader election scheme adopted in this paper is the one adopted by spark's Master HA.

< Spark源码之Standalone模式下master持久化引擎讲解>

. That is, to implement LeaderLatchListener and then use it to listen for action master changes. 1Maven relies on 3.2.1 org.apache.curator curator-framework ${curator.version}

Org.apache.curator curator-recipes ${curator.version} org.apache.zookeeper zookeeper

2,LeaderLatchListenerImpl

Public class LeaderLatchListenerImpl implements LeaderLatchListener {

@ Override public void isLeader () {/ / TODO Auto-generated method stub System.out.println ("master");}

@ Override public void notLeader () {/ / TODO Auto-generated method stub System.out.println ("slave");}}

3,LeaderLatchListener

Public class LeaderLatchMain {private static final String PATH = "/ examples/leader"; public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory.newClient ("106.75.13.87 public class LeaderLatchMain 2181", new ExponentialBackoffRetry (1000, 3)); client.start (); LeaderLatch leaderLatch = new LeaderLatch (client, PATH,args [0]); leaderLatch.addListener (new LeaderLatchListenerImpl ())

Try {leaderLatch.start (); Thread.sleep (10000); System.out.println (leaderLatch.getLeader ()); Thread.sleep (Integer.MAX_VALUE);} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace (); leaderLatch.close (); client.close ();}

}} third, summarize that participant ID in this demo is started when the parameters are passed in. Startup instruction: java-jar LeaderLatchMain.jar 1java-jar LeaderLatchMain.jar 2

Leader the first time it starts

After killing Leader,

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report