读《分布式一致性原理》CURATOR客户端3
Posted duan2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读《分布式一致性原理》CURATOR客户端3相关的知识,希望对你有一定的参考价值。
分布式锁
在分布式环境中,为了保证数据的一致性,经常在程序运行的某个运行点。需要进行同步控制。
package master; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.CountDownLatch; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessLock; import org.apache.curator.framework.recipes.locks.InterProcessMultiLock; import org.apache.curator.framework.recipes.locks.InterProcessMutex; import org.apache.curator.retry.ExponentialBackoffRetry; public class Recipes_Lock { static String lock_path="/lock_paht"; static CuratorFramework client = CuratorFrameworkFactory.builder(). connectString("192.168.64.60:2181") .connectionTimeoutMs(5000). retryPolicy(new ExponentialBackoffRetry(3000, 3)) .build(); public static void main(String[] args) { client.start(); final InterProcessMutex lock= new InterProcessMutex(client,lock_path); final CountDownLatch down = new CountDownLatch(1); for (int i = 0; i <30; i++) { new Thread(new Runnable() { public void run() { try { down.wait(); lock.acquire(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss|SSS"); String orderNo = dateFormat.format(new Date()); System.out.println("生成的订单号是:"+orderNo); try { lock.release(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start();; } down.countDown(); } }
上面这个程序借助Curator来实现了一个简单的分布式锁。其核心接口如下:
工具
curator也提供了了很多的工具类,其中用的最多的就是zkPath和EnsurePath。
ZKPaths
ZKPaths提供了一些简单的API来构建ZNode路径,递归创建和删除节点等,
package utils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.ZKPaths;
import org.apache.curator.utils.ZKPaths.PathAndNode;
import org.apache.zookeeper.ZooKeeper;
public class ZKPaths_sample {
static String path="/zkpath_path";
static CuratorFramework client = CuratorFrameworkFactory.builder().
connectString("192.168.64.60:2181")
.connectionTimeoutMs(5000).
retryPolicy(new ExponentialBackoffRetry(3000, 3))
.build();
public static void main(String[] args) throws Exception {
client.start();
ZooKeeper zooKeeper = client.getZookeeperClient().getZooKeeper();
System.out.println(ZKPaths.fixForNamespace(path, "sub"));
System.out.println(ZKPaths.makePath(path, "sub"));
System.out.println(ZKPaths.getNodeFromPath("zkapth_path/sub1"));
PathAndNode pn = ZKPaths.getPathAndNode("/zkpath_path/sub1");
System.out.println(pn.getPath());
System.out.println(pn.getNode());
ZKPaths.mkdirs(zooKeeper, path+"/child1");
ZKPaths.mkdirs(zooKeeper, path+"/child2");
System.out.println(ZKPaths.getSortedChildren(zooKeeper, path));
}
}
EnsurePath
提供了一种能够确保数据及诶单存在的机制。
TestingServer
为了便于开发人员进行zookeeper的开发与测试,curator提供了一种启动简易的zookeeper服务器服务的方法
——TestingServer。TestingServer允许开发人员非常方便的启动一个标准的zookeeper服务器。并进行一系列的单元测试。
TestingCluster
TestingCluster是一个可以模拟zookeeper集群环境的curator工具类,能够便于开发人员在本地模拟由
n台机器组成的集群环境。下面我们模拟一个有3台机器构成的zookeeper集群场景。
以上是关于读《分布式一致性原理》CURATOR客户端3的主要内容,如果未能解决你的问题,请参考以下文章