GithubHelp home page GithubHelp logo

lynshine / redisson-spring-boot-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liuzw-code/lock-spring-boot-starter

0.0 1.0 0.0 43 KB

基于SpringBoot + Redisson 实现分布式锁

Java 100.00%

redisson-spring-boot-starter's Introduction

redisson-spring-boot-starter

基于SpringBoot + Redisson 实现分布式锁

简介

  1. 使用SpringBoot框架搭建
  2. 分布式锁选用redis实现。选择官方提供的redisson
  3. 结合spring aop使用,使用方便。

配置

组件默认使用spring原生redis配置

  1. 单点模式:
spring:
    redis:
    port: 6379
    database: 0
    host: 127.0.0.1
    password: xxxxx
  1. 集群模式:
spring:
   redis:
    cluster:
      nodes: 192.168.14.221:19001,192.168.14.221:19002,192.168.14.221:19003
      max-redirects: 5
    password: xxxxx
  1. 哨兵模式:
spring:
   redis:
    sentinel:
      nodes: 192.168.14.221:19001,192.168.14.221:19002,192.168.14.221:19003
      master: xxxxx
    password: xxxxx

使用

  1. 使用的时候只需在方法上加入注解 @DistributedLock即可,注解具体参数自行设置
 @DistributedLock
 public void test() {
    // 执行业务代码
 }

如果需要从参数中获取锁的key

 @DistributedLock(lockName="#user.id")
 public void test(@RequestBody User user) {
    // 执行业务代码
 }
  1. 在类中注入IDistributedLock
 @Autowired
 private IDistributedLock distributedLock;

 public void test() {
    distributedLock.lock(lockName)
    try {
     // 执行业务代码
    } catch (Exception e) {
       log.error("执行方法报错:", e);
    } finally {
     //解锁
     distributedLock.unlock(lockName);
    }
}

或者

 public void test1() {
    try {
      if (distributedLock.tryLock(lockName)) {
          // 执行业务代码
       }
    } catch (Exception e) {
       log.error("执行方法报错:", e);
    } finally {
     //解锁
     distributedLock.unlock(lockName);
    }
}

redisson-spring-boot-starter's People

Watchers

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