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 use the election law of zookeeper

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the electoral law of zookeeper". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the election law of zookeeper.

There are two implementations of Curator:

LeaderLatch:

This is blocked, that is, everyone goes together, and whoever goes first will block until the execution of the method is completed. If the execution is over, then the other brothers will choose one. I think this is suitable for active and standby, such as opening 2 job, one hanging up and the other on.

Code, this kind of code is best to open 2 eclipse to see the effect, open it sequentially, see the effect, and then understand:

Package curator.selector;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.leader.LeaderLatch;import org.apache.curator.retry.ExponentialBackoffRetry;public class LeaderLatchDemo {public static void main (String [] args) throws Exception {RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3) CuratorFramework client = CuratorFrameworkFactory.builder () .connectString ("127.0.0.1 sessionTimeoutMs 2181"). SessionTimeoutMs (2000) .connectionTimeoutMs (10000) .retryPolicy (retryPolicy) .namespace ("text") .build (); client.start (); / / Election Leader launch LeaderLatch latch = new LeaderLatch (client, "/ path"); latch.start () Latch.await (); System.err.println ("I started"); Thread.currentThread (). Sleep (1000000); latch.close (); client.close ();}}

LeaderSelector:

This kind of complication, he has a leaderSelector.autoRequeue (); it is automatic grab, for example, print a helloworld, the first print, the second print, and then the third print. Then print the first one, and let's grab it together. It's a bit of a dynamic election.

Code:

Package curator.selector;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.leader.LeaderSelector;import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;import org.apache.curator.retry.ExponentialBackoffRetry;public class LeaderSelectorDemo {public static void main (String [] args) throws Exception {RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3) Final CuratorFramework client = CuratorFrameworkFactory.builder (). ConnectString ("127.0.0.1 sessionTimeoutMs 2181"). SessionTimeoutMs (5000) .connectionTimeoutMs (10000) .retryPolicy (retryPolicy) .namespace ("text"). Build (); client.start () Final LeaderSelector leaderSelector = new LeaderSelector (client, "/ led", new LeaderSelectorListenerAdapter () {@ Override public void takeLeadership (CuratorFramework client) throws Exception {System.err.println ("work ing..."); Thread.currentThread () .sleep (3000) System.err.println ("end");}}); leaderSelector.autoRequeue (); leaderSelector.start (); System.in.read () }} at this point, I believe you have a deeper understanding of "how to use the electoral law of zookeeper". You might as well put it into practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Servers

Wechat

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

12
Report