How to Use Redisson to Realize Distributed Lock in Springboot

  • 2021-11-24 01:38:50
  • OfStack

Directory Preface 1. Overview 2. Use of Redisson in Springboot 2.1 Introducing Dependencies 2.2 Configuring Redis 2.3 Demo Codes in Springboot Configuration 3. Overview

Preface

In order to ensure the final uniformity of data in distributed scenarios. In a single-process system, when multiple threads can change a variable (variable shared variable) at the same time, it is necessary to synchronize the variable or code block (lock-synchronized), so that it can perform linear modification to eliminate concurrent modification of variables when modifying such variables. However, the distributed system is multi-deployment and multi-process, so the concurrent processing API provided by the development language is powerless in this scenario.

1. Overview

As the old saying goes, there are no good goods at a low price, and some people will buy valuable goods even if they are expensive.

To get down to business, let's continue to discuss the topic of "locks" today. synchronized and ReentrantLock should be very familiar to everyone, but the scope of these two locks is limited to a single Tomcat. If Tomcat cluster is used, these two locks will not work.

At this time, we will introduce distributed locks. There are many ways to realize distributed locks, which can be realized by Mysql database or Zookeeper. Of course, Redis is commonly used.

Today, we will talk about the implementation based on Redis-Redisson.

Redisson is an Java in-memory data grid (In-Memory Data Grid) based on redis. Taking full advantage of 1 series of advantages provided by Redis key-value database, a series of common tool classes with distributed characteristics are provided for users based on the common interfaces in Java utility kit. The toolkit, which was originally used to coordinate single-machine multi-threaded concurrent programs, has the ability to coordinate distributed multi-machine multi-threaded concurrent systems, which greatly reduces the difficulty of designing and developing large-scale distributed systems. At the same time, combined with various characteristic distributed services, it further simplifies the cooperation between programs in distributed environment.

2. Use of Redisson in Springboot

2.1 Introducing dependencies


<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.3</version>
</dependency>

2.2 Configuring Redis in Springboot Configuration

Support Redis monomer, Redis sentinel mode and Redis cluster mode


spring:
  redis:
    host: 192.168.1.12
    port: 6379
    password: zhuifengren

2.3 Demo code


@Autowired
    private RedissonClient redissonClient;

    public void lock() {

        RLock rLock = redissonClient.getLock("myLock");
        log.info(" Enter the method ");

        try {
            //  Lock, 30 Automatically release the lock after seconds 
            rLock.lock(30, TimeUnit.SECONDS);
            log.info(" The lock was acquired ");

            Thread.sleep(15000);

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            //  Release lock 
            rLock.unlock();
            log.info(" Lock released ");
        }
    }

3. Overview


Related articles: