zookeeper 实现分布式锁

 

一、需求痛点

分布式框架中多个应用同时操作同一数据,容易造成脏数据。为解决多个应用对同一资源产生的竞争关系。利用zookeeper 的高性能一致性特点,控制多个应用对同一数字资源的造作。

二、Zookeeper 节点类型

Zookeeper 的节点是一个树形。节点主要分为两类四种,短暂(ephemeral) 和 持久的(persistent) .

      » PERSISTENT(持久的)
  » EPHEMERAL (暂时的)
  » PERSISTENT_SEQUENTIAL(持久化顺序编号目录节点)

  » EPHEMERAL_SEQUENTIAL(暂时化顺序编号目录节点)

三、zookeeper 实现分布式锁步骤

package com.zookeeper.curator;

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;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * Created by liang on 2018/11/6.
 */

@Component
//@Resource
public class DistributedLock {


    public void exeCurator() throws Exception {

        System.out.println("--nihao!!!-");
//        创建zookeeper的客户端
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.**.***:2181", retryPolicy);
        client.start();
        //创建分布式锁, 锁空间的根节点路径为/curator/lock
        InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock");
        mutex.acquire();
//        mutex.acquire(10, TimeUnit.SECONDS);

        Thread.sleep(10*1000);
//
        //获得了锁, 进行业务流程
        System.out.println("Enter mutex"+System.currentTimeMillis());
        //完成业务流程, 释放锁
        mutex.release();
        //关闭客户端
        client.close();

    }

    @PostConstruct
    public void exe(){
        for(int i=0;i<3;i++){
            new MainThread().start();
        }
    }


    /**
     *
     */
    public class MainThread extends Thread{

        @Override
        public void run() {

            System.out.println(Thread.currentThread().getName()+"==线程的名称");
            try {
                exeCurator();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }



    }




}

 

你可能感兴趣的:(zookeeper)