分布式锁-基于zookeeper客户端curator实现锁机制(一)锁基础

分布式锁基础:继承接口的锁实现,以统一的接口调用完成,封装错误处理

定义接口,实现此接口都拥有获取锁与释放锁

/*
 * Copyright 2016-2018. Individual ownership panw
 *
 * Tech blog at http://blog.csdn.net/hatsune_miku_.
 *
 * Contact Mailbox at panw.
 *
 */
package com.miku.common.util.lock;

import org.apache.curator.framework.recipes.locks.InterProcessLock;

/**
 * @author:panw.
 * @date:2018/1/16.
 */
public interface Lock {

    /**
     * 获取锁 - 根据接口子类推断出当前锁类型
     *
     * @see org.apache.curator.framework.recipes.locks.InterProcessMultiLock
     * @see org.apache.curator.framework.recipes.locks.InterProcessMutex
     * @see org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex
     * @param interProcessLock
     * @throws Exception
     */
    void lock(InterProcessLock interProcessLock) throws Exception;

    /**
     * 获取锁 - 阻止直到可用或给定的时间到期
     *
     * @param interProcessLock
     * @param milliseconds
     * @throws Exception
     */
    boolean lock(InterProcessLock interProcessLock,long milliseconds) throws Exception;


    /**
     * 释放锁 - 根据锁接口子类推断出当前锁类型
     *
     * @see org.apache.curator.framework.recipes.locks.InterProcessMultiLock
     * @see org.apache.curator.framework.recipes.locks.InterProcessMutex
     * @see org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex
     * @param interProcessLock
     * @throws Exception
     */
    void unlock(InterProcessLock interProcessLock) throws Exception;
}

锁抽象,获取zk上下文锁地址,zk链接地址,监听zk链接状态

/*
 * Copyright 2016-2018. Individual ownership panw
 *
 * Tech blog at http://blog.csdn.net/hatsune_miku_.
 *
 * Contact Mailbox at panw.
 *
 */
package com.miku.common.util.lock.support;

import com.miku.common.util.lock.Lock;
import com.miku.common.utils.spring.SpringUtils;
import com.miku.common.zookeeper.ZookeeperClient;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * zookeeper分布式锁实现,所有获取锁都是公平锁的实现
 *
 * @see org.apache.curator.framework.recipes.locks
 *
 * @author:panw.
 * @date:2018/1/17.
 */
public abstract class AbstractZookeeperLock<E extends InterProcessLock>  implements Lock{

    public static CuratorFramework curatorFramework;
    public final ConcurrentHashMap locks = new ConcurrentHashMap<>();

    public AbstractZookeeperLock(){
        ZookeeperClient zookeeperClient = SpringUtils.getBean(ZookeeperClient.class);
        curatorFramework = zookeeperClient.getClient();
    }

    @Override
    public void lock(InterProcessLock interProcessLock) throws Exception{
        try {
            interProcessLock.acquire();
            lockConnectionStateListenable();
        } catch (Exception e) {
            throw new IllegalStateException("锁丢失=>" + e);
        }
    }

    @Override
    public boolean lock(InterProcessLock interProcessLock,long time) throws Exception{
        boolean state;
        try {
            state = interProcessLock.acquire(time, TimeUnit.MILLISECONDS);
            lockConnectionStateListenable();
        } catch (Exception e) {
            throw new IllegalStateException("锁丢失=>" + e);
        }
        return state;
    }

    @Override
    public void unlock(InterProcessLock interProcessLock) throws Exception{
        try {
            interProcessLock.release();
        } catch (Exception e) {
            throw new IllegalStateException("锁丢失=>" + e);
        }
    }

    /**
     * 监听zk连接状态.
     */
    public void lockConnectionStateListenable() throws Exception{
        curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {
            @Override
            // ConnectionStateListener并监视SUSPENDED和LOST状态更改。如果报告“挂起”状态,则不能确定是否仍然保持锁定状态,
            // 除非您随后收到“重新连接”状态。如果报告失去状态,则确定您不再持有该锁
            public void stateChanged(CuratorFramework client, ConnectionState state) {
                if (state == ConnectionState.LOST) {
                    // TODO 考虑删除锁实例
                    throw new IllegalStateException("会话超时锁丢失=>");
                }
            }
        });
    }
}
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>${curator_version}version>
            dependency>

            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${curator_version}version>
            dependency>

博客原创:写作不易,转载请标明出处。文章地址:https://blog.csdn.net/hatsune_miku_/article/details/79233888(๑˃∀˂๑)♪阿里嘎多(๑˃∀˂๑)♪
分布式锁-基于zookeeper客户端curator实现锁机制(一)锁基础
分布式锁-zookeeper-SharedLock基于InterProcessSemaphoreMutex(二)分布式共享锁
分布式锁-zookeeper-SharedReentrantLock基于InterProcessMutex(三)分布式重入共享锁
分布式锁-zookeeper-SharedReentrantReadWriteLock基于InterProcessReadWriteLock(四)分布式重入读写共享锁

你可能感兴趣的:(zookeeper,curator,apache,分布式锁)