Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize distributed Digital by Curator

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

This article mainly introduces "how to achieve distributed digital in Curator". In daily operation, I believe that many people have doubts about how to achieve distributed digital in Curator. The editor 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 about "how to achieve distributed digital in Curator". Next, please follow the editor to study!

Curator has two implementations:

The first one:

Package curator.counters;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.shared.SharedCount;import org.apache.curator.framework.recipes.shared.SharedCountListener;import org.apache.curator.framework.recipes.shared.SharedCountReader;import org.apache.curator.framework.state.ConnectionState;import org.apache.curator.retry.ExponentialBackoffRetry Public class SharedCounterDemo {public static void main (String [] args) throws Exception {RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3); final CuratorFramework client = CuratorFrameworkFactory.builder (). ConnectString ("127.0.0.1 String 2181"). SessionTimeoutMs (5000) .connectionTimeoutMs (10000) .retryPolicy (retryPolicy) .namespace ("test"). Build (); client.start () SharedCount count = new SharedCount (client, "/ cut", 0); count.start () Count.addListener (new SharedCountListener () {@ Override public void stateChanged (CuratorFramework client, ConnectionState newState) {} @ Override public void countHasChanged (SharedCountReader sharedCount) Int newCount) throws Exception {System.err.println ("sharedCount.getCount:" + sharedCount.getCount ()) System.err.println (newCount);}}); Thread.currentThread () .sleep (5000); count.setCount (5); System.in.read ();}}

Second:

Package curator.counters;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.atomic.AtomicValue;import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.retry.RetryNTimes;public class DistributedAtomicIntegerDemo {public static void main (String [] args) throws Exception {RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3) Final CuratorFramework client = CuratorFrameworkFactory.builder (). ConnectString ("127.0.0.1 autlog 2181"). SessionTimeoutMs (5000) .connectionTimeoutMs (10000) .retryPolicy (retryPolicy) .namespace ("test"). Build (); client.start (); DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger (client, "/ autlog", new RetryNTimes (32 Magneto 1000); AtomicValue rc = atomicInteger.add (8) System.out.println ("success:" + rc.succeeded () + "; before:" + rc.preValue () + "; after:" + rc.postValue ()); System.in.read ();}} at this point, the study on "how Curator implements distributed numbers" is over, hoping to solve everyone's 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!

Tags: Distributed digital learning more help practical next articles methods theories knowledge articles websites materials follow questions easy to use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Shulou Technology Linux Apple