意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Curator怎么实现分布式数字

来源:恒创科技 编辑:恒创科技编辑部
2024-02-01 16:45:59

这篇文章主要介绍“Curator怎么实现分布式数字”,在日常操作中,相信很多人在Curator怎么实现分布式数字问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Curator怎么实现分布式数字”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Curator有2个实现:

第一个:


Curator怎么实现分布式数字

packagecurator.counters;

importorg.apache.curator.RetryPolicy;
importorg.apache.curator.framework.CuratorFramework;
importorg.apache.curator.framework.CuratorFrameworkFactory;
importorg.apache.curator.framework.recipes.shared.SharedCount;
importorg.apache.curator.framework.recipes.shared.SharedCountListener;
importorg.apache.curator.framework.recipes.shared.SharedCountReader;
importorg.apache.curator.framework.state.ConnectionState;
importorg.apache.curator.retry.ExponentialBackoffRetry;

publicclassSharedCounterDemo{

	publicstaticvoidmain(String[]args)throwsException{
		RetryPolicyretryPolicy=newExponentialBackoffRetry(1000,3);
		finalCuratorFrameworkclient=CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").sessionTimeoutMs(5000).connectionTimeoutMs(10000).retryPolicy(retryPolicy).namespace("test").build();
		client.start();
		
		SharedCountcount=newSharedCount(client,"/cut",0);
		count.start();
		
		count.addListener(newSharedCountListener(){

			@Override
			publicvoidstateChanged(CuratorFrameworkclient,ConnectionStatenewState){
				
			}

			@Override
			publicvoidcountHasChanged(SharedCountReadersharedCount,intnewCount)throwsException{
				System.err.println("sharedCount.getCount:"+sharedCount.getCount());
				System.err.println(newCount);
			}});
		Thread.currentThread().sleep(5000);
		count.setCount(5);
		System.in.read();
	}

}

第二个:

packagecurator.counters;

importorg.apache.curator.RetryPolicy;
importorg.apache.curator.framework.CuratorFramework;
importorg.apache.curator.framework.CuratorFrameworkFactory;
importorg.apache.curator.framework.recipes.atomic.AtomicValue;
importorg.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
importorg.apache.curator.retry.ExponentialBackoffRetry;
importorg.apache.curator.retry.RetryNTimes;

publicclassDistributedAtomicIntegerDemo{

	publicstaticvoidmain(String[]args)throwsException{
		RetryPolicyretryPolicy=newExponentialBackoffRetry(1000,3);
		finalCuratorFrameworkclient=CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181").sessionTimeoutMs(5000).connectionTimeoutMs(10000).retryPolicy(retryPolicy).namespace("test").build();
		client.start();

		DistributedAtomicIntegeratomicInteger=newDistributedAtomicInteger(client,"/autlog",newRetryNTimes(32,1000));
		AtomicValue<Integer>rc=atomicInteger.add(8);
		System.out.println("success:"+rc.succeeded()+";before:"+rc.preValue()+";after:"+rc.postValue());
		System.in.read();
	}

}

到此,关于“Curator怎么实现分布式数字”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注恒创网站,小编会继续努力为大家带来更多实用的文章!

上一篇: zookeeper做集群的方法是什么 下一篇: 手机怎么远程登录云服务器?