Git Product home page Git Product logo

consul-distributed-lock's Introduction

#基于Consul的分布式锁工具

在构建分布式系统的时候,我们经常需要控制对共享资源的访问。这个时候我们就涉及到分布式锁(也称为全局锁)的实现,本项目将基于Consul的KV存储来实现一套Java的分布式锁小工具,以帮助简化分布式环境下的同步操作。

基本原理

本工具实现的基本原理可以参见下面两篇博客:

注意:这两篇博文仅描述了基本的实现流程,但未实现针对一些异常情况的锁和信号量清理,以解决出现死锁的情况,同时也为锁操作增加了一些其他情况的使用设置。

使用方法

目前该工具实现了两个小功能:分布式互斥锁和信号量。

分布式互斥锁

ConsulClient consulClient = new ConsulClient("localhost", 8500);	// 创建与Consul的连接
CheckTtl checkTtl = new CheckTtl("checkId", consulClient); // session的健康检查,用来清理失效session占用的锁
Lock lock = new Lock(consulClient, "lockKey", checkTtl);
try {
	// 获取分布式互斥锁
  	// 参数含义:是否使用阻塞模式、每次尝试获取锁的间隔500ms、尝试n次
    if (lock.lock(true, 500L, null)) {     	
        // TODO 处理业务逻辑
    } 
catch (Exception e) {
    e.printStackTrace();
} finally {
    lock.unlock();
}

分布式信号量

ConsulClient consulClient = new ConsulClient("localhost", 8500);	// 创建与Consul的连接
CheckTtl checkTtl = new CheckTtl("checkId", consulClient); // session的健康检查,用来清理失效session占用的锁
Semaphore semaphore = new Semaphore(consulClient, 3, "lockKey", checkTtl); // 3为信号量的值
try {
	if (semaphore.acquired(true)) {
    	// TODO 获取到信号量,执行业务逻辑
	}
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
		// 信号量释放
		semaphore.release();
	} catch (IOException e) {
    	e.printStackTrace();
    }
}

持续优化ing

我的博客:http://blog.didispace.com

我的公众号:

输入图片说明

我的新书:

输入图片说明

consul-distributed-lock's People

Contributors

dyc87112 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.