美文网首页
Zookeeper实现分布式锁

Zookeeper实现分布式锁

作者: 大数据ZRL | 来源:发表于2021-02-01 20:42 被阅读0次
    利用ZooKeeper实现分布式锁.png
    • pom.xml
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.0.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>4.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.6</version>
            </dependency>
    
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>16.0.1</version>
            </dependency>
    
        </dependencies>
    
    • 案例代码
    import org.apache.curator.RetryPolicy;
    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 TestDemo {
    
        //定义共享资源
        private static int NUMBER = 10;
        //定义业务逻辑
        private static void printNumber() {
            System.out.println("*******秒杀开始********");
            System.out.println("当前数字:" + NUMBER);
            
            NUMBER--;
            
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("*******秒杀结束********\n");
        }
            
        public static void main(String[] args) {
            //定义重试策略
            RetryPolicy policy = new ExponentialBackoffRetry(1000, //每次重试等待时间
                                                             10);//重试的次数
            //定义ZooKeeper的客户
            CuratorFramework client = CuratorFrameworkFactory.builder()
                                                             .connectString("192.168.157.111:2181")
                                                             .retryPolicy(policy)
                                                             .build();
            client.start();
            
            //在ZooKeeper中创建锁
            final InterProcessMutex lock = new InterProcessMutex(client, "/mylock");
            
            //客户端
            for(int i=0;i<10;i++) {
                new Thread(new Runnable() {
                    
                    public void run() {
                        try {
                            //请求得到锁
                            lock.acquire();
                            
                            //执行秒杀
                            printNumber();
                        }catch(Exception ex) {
                            ex.printStackTrace();
                        }finally {
                            try {
                                lock.release();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Zookeeper实现分布式锁

          本文链接:https://www.haomeiwen.com/subject/tgirtltx.html