• Curator实现zookeeper分布式锁


    Curator实现zookeeper分布式锁简单案例
    解决了传统分布式锁开发的繁琐,以及不能并发的问题
    Curator官网:
    https://curator.apache.org/
    需要导入依赖:

    <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
    <dependency>
     **加粗样式**   <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>4.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-client -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-client</artifactId>
        <version>4.2.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    流程连接客户端,创建锁

    package com.example.zjh.zookeeper.CuratorFrame;
    
    import lombok.SneakyThrows;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessMutex;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    public class CuratorFrame {
    
        public static void main(String[] args) {
            // 分布式锁1
            // 参数:客户端连接,节点名
            InterProcessMutex lock1 = new InterProcessMutex(getLink(), "/locks");
            // 分布式锁2
    //        InterProcessMutex lock2 = new InterProcessMutex(getLink(), "/locks");
            // 线程
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    // lock1拿锁
                    lock1.acquire();
                    System.out.println("lock1拿到锁");
                    // lock1释放锁
                    lock1.release();
                    System.out.println("lock1解锁锁");
                }
            }).start();
        }
        private static CuratorFramework getLink() {
            // 失败尝试构建次数(3000ms,3次)
            final ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(3000, 3);
            // 构建连接
            // 地址
            final CuratorFramework linkZk = CuratorFrameworkFactory.builder().connectString("zookeeper集群地址")
                    // 连接超时时间,单位ms
                    .connectionTimeoutMs(20000)
                    // 会话超时时间,单位ms
                    .sessionTimeoutMs(20000)
                    // 重试
                    .retryPolicy(exponentialBackoffRetry).build();
            // 启动客户端
            linkZk.start();
            // linkZk也能取到客户端节点以及数据
            System.out.println("zookeeper连接成功");
            return linkZk;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    比例运算放大电路为什么要加平衡电阻
    多向思考者--高敏感人群的内心世界
    SpringBoot(七) - Redis 缓存
    leetcode的TreeNode工具类
    LeetCode2125
    Java程序员学习算法路线规划总结
    2022年最新四川机动车签字授权人模拟试题及答案
    WMS系统后端开发-用户权限
    C. Tree Infection(二分)
    LeeCode热题100(两数之和)
  • 原文地址:https://blog.csdn.net/qq_43421954/article/details/127654720