Giter VIP home page Giter VIP logo

distributed-lock's Introduction

分布式锁

基于redisson的分布式锁

基于redisson的分布式锁(已实现)

基于redis的分布式锁

基于redis的分布式锁(未实现)

基于zookeeper的分布式锁

基于zookeeper的分布式锁(未实现)

使用方式

依赖
  • 使用redisson-lock-spring-boot-starter

    引入依赖

    dependencies {
        compile("com.jean:redisson-lock-spring-boot-starter:2.0-SNAPSHOT")
    }
    

    配置文件

    spring:
      profiles:
        active: dev
      application:
        name: order-service
      redis:
        host: 127.0.0.1
        port: 6379
        database: 0
        password: 123456
    

    min-idle必须大于0

  • 使用使用redis-lock-spring-boot-starter

    引入依赖

    dependencies {
        compile("com.jean:redis-lock-spring-boot-starter:2.0-SNAPSHOT")
    }
    

    配置文件

    spring:
       profiles:
         active: dev
       application:
         name: order-service
       redis:
         host: 127.0.0.1
         port: 6379
         database: 0
         password: 123456
    
  • 使用zookeeper-lock-spring-boot-starter 引入依赖

    dependencies {
        compile("com.jean:zookeeper-lock-spring-boot-starter:2.0-SNAPSHOT")
    }
    

    配置文件

    //TODO
    
使用方式
  • 使用@DistributedLock注解

    @Service
    public class OrderServiceImpl {
    
        @Override
        @Transactional
        @DistributedLock(name = "buy_product", nameSuffix = "#param.productId+'_'+#param.userId")
        //@DistributedLock(name = "buy_product", namePrefix = "lock", nameSuffix = "#param.productId+'_'+#param.userId", waitTime = 3L, timeout = 30L, timeUnit = TimeUnit.SECONDS, fallback = "buyProductFallback")
        public Order buyProduct(Object param) {
            //冻结用户账户余额
            //扣减商品库存
            //TODO
            return new Order()
        }
        
        public Order buyProductFallback(Object param) {
                //TODO 加锁失败
        }
    }
    

    注解@DistributedLock说明

    • name 锁的名称
    • namePrefix 锁的名称前缀,支持SpEL表达式。默认值"lock"
    • nameSuffix 锁的名称后缀,可以设置与业务相关的值,支持SpEL表达式。默认值""
    • separator 拼接namePrefix、name、nameSuffix的分隔符。默认值"."
    • waitTime 尝试加锁最长等待时间。waitTime等于0时,加锁失败不等待。当waitTime小于0时,会一直等待锁。默认值3
    • timeout 锁超时时间。锁超时后自动释放。建议尽量缩简需要加锁的逻辑。timeout等于0时不加锁。默认值30
    • timeUnit 时间单位。默认值
    • fallback 加锁失败回调方法,该回调方法的返回值类型和参数必须与当前被加锁的方法一致。默认值""
  • 编码方式

    @Service
    public class OrderServiceImpl {
        
        @Autowired
        private LockTemplate lockTemplate
    
        @Override
        @Transactional
        public Order buyProduct(Object param) {
            def worker = new LockWorker<Order>() {
                @Override
                Order work() throws Throwable {
                    //冻结用户账户余额
                    //扣减商品库存
                    //TODO
                    return new Order()
                }
            }
            return lockTemplate.tryLock("lock.buy_product.${param.productId}_${param.userId}", 3L, 30L, TimeUnit.SECONDS, worker)
        }
    }
    

使用注解方式,锁在事务外层执行,主意事务执行的时长不能超过锁的超时时间,防止锁提前释放导致数据不一致。

使用编码方式,锁在事务内部执行,尽量减少获取锁等待时间和锁定时间,防止锁耗时太长导致事务超时。

distributed-lock's People

Contributors

jinshubao avatar

Watchers

James Cloos 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.