Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the election law of zookeeper

Shulou Source: shulou.com Published: 2022-05-31 12:24:19 09月18日 Update

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!

Tags: Election electoral law code content that is effect method learning blocking complex practical deeper brother interest dynamic taste practicality practicality simple operation more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information MariaDB OPPO Reno Shulou Tech Info