1. 程式人生 > 其它 >Zookeeper Curator 框架實現分散式鎖

Zookeeper Curator 框架實現分散式鎖

官方文件:https://curator.apache.org/index.html

 

依賴

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.3.0</version>
</dependency>

  

案例

package com.syf.case3;

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 CuratorLockTest {
    public static void main(String[] args) {

        // 建立分散式鎖1
        InterProcessMutex lock1 = new InterProcessMutex(getCuratorFramework(), "/locks");
        // 建立分散式鎖2
        InterProcessMutex lock2 = new InterProcessMutex(getCuratorFramework(), "/locks");
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    lock1.acquire();
                    System.out.println("執行緒1 獲取到鎖");

                    lock1.acquire();
                    System.out.println("執行緒1 再次獲取到鎖");

                    Thread.sleep(5 * 1000);

                    lock1.release();
                    System.out.println("執行緒1 釋放鎖");
                    Thread.sleep(5 * 1000);
                    lock1.release();
                    System.out.println("執行緒1 再次釋放鎖");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    lock2.acquire();
                    System.out.println("執行緒2 獲取到鎖");

                    lock2.acquire();
                    System.out.println("執行緒2 再次獲取到鎖");

                    Thread.sleep(5 * 1000);

                    lock2.release();
                    System.out.println("執行緒2 釋放鎖");
                    Thread.sleep(5 * 1000);
                    lock2.release();
                    System.out.println("執行緒2 再次釋放鎖");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private static CuratorFramework getCuratorFramework() {

        ExponentialBackoffRetry policy = new ExponentialBackoffRetry(3000, 3);

        CuratorFramework client = CuratorFrameworkFactory.builder().connectString("hadoop102:2181,hadoop103:2181,hadoop104:2181")
                .connectionTimeoutMs(2000)
                .sessionTimeoutMs(2000)
                .retryPolicy(policy).build();
        // 啟動客戶端
        client.start();
        System.out.println("zookeeper 啟動成功");
        return client;
    }
}