• 读《分布式一致性原理》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集群场景。

     

     

     

     

     

  • 相关阅读:
    Codeforces Round #527 (Div. 3) B. Teams Forming
    Train Problem I (栈的基本运用)
    浙江中医药大学第十二届大学生程序设计竞赛 J. Jhadgre爬楼梯
    判断二进制半整数
    简易连连看
    Codeforces Round #527 (Div. 3) A. Uniform String
    求字符串中出现次数最多的字符 JAVA
    母猪的故事 (递推题)
    C#判断文件和文件夹是否存在 不存在则创建
    C# 数据库备份与还原 小妹做了一个winform系统,需要对sql2000数据库备份和还原(小妹妹你太狠了)
  • 原文地址:https://www.cnblogs.com/duan2/p/9094095.html
Copyright © 2020-2023  润新知