SpringBoot实现Redisson分布式锁

1、加入依赖

<dependency>
	<groupId>org.redissongroupId>
	<artifactId>redissonartifactId>
	<version>3.13.6version>
dependency>

2、装配RedissonClient。

package com.yt.service.zutil;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 分布式锁
 * @author tyg
 * @date 2020-12-22 16:27
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private Integer database;

    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        config.useSingleServer().setAddress(String.format("redis://%s:%s", host, port)).setPassword(password).setDatabase(database);
        RedissonClient client = Redisson.create(config);
        RedissonLockUtil.redissonClient = client;
        return client;
    }
}

3、封装Redisson工具类。

package com.common.redis.service;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
 * 分布式锁
 * @author tyg
 * @date 2020-12-22 16:55
 */
public class RedissonLockUtil{

    public static RedissonClient redissonClient;

    /**
     * 加锁
     *
     * @param lockKey 锁的key
     * @author tyg
     * @date 2020-12-22 16:53
     * @return RLock
     */
    public static RLock lock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock();
        return lock;
    }

    /**
     * 加锁
     *
     * @param lockKey 锁的key
     * @author tyg
     * @date 2020-12-22 16:53
     * @return RLock
     */
    public static RLock tryLock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.tryLock();
        return lock;
    }

    /**
     * 加锁
     * 最多等待锁waitTime秒
     * @param lockKey 锁的key
     * @param waitTime 等待时间
     * @param unit 时间单位,默认为秒
     * @author tyg
     * @date 2020-12-22 16:53
     * @return RLock
     */
    public static RLock tryLock(String lockKey, long waitTime, TimeUnit unit) throws InterruptedException {
        unit = unit == null ? TimeUnit.SECONDS : unit;
        RLock lock = redissonClient.getLock(lockKey);
        lock.tryLock(waitTime, unit);
        return lock;
    }

    /**
     * 加锁
     * 最多等待锁waitTime秒,unlockTime秒钟以后自动解锁, 无需调用unlock方法手动解锁
     * @param lockKey 锁的key
     * @param waitTime    等待时间,默认为秒
     * @param unlockTime  自动解锁时间,默认为秒
     * @author tyg
     * @date 2020-12-22 16:53
     * @return RLock
     */
    public static RLock tryLock(String lockKey, long waitTime, long unlockTime) throws InterruptedException {
        RLock lock = redissonClient.getLock(lockKey);
        lock.tryLock(waitTime, unlockTime, TimeUnit.SECONDS);
        return lock;
    }

    /**
     * 加锁
     * 指定timeout秒后,自动解锁,不需要unlock
     * @param lockKey 锁的key
     * @param timeout 超时时间,单位:秒
     * @author tyg
     * @date 2020-12-22 16:53
     * @return RLock
     */
    public static RLock lock(String lockKey, int timeout) {
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(timeout, TimeUnit.SECONDS);
        return lock;
    }

    /**
     * 解锁
     * @param lockKey 锁的key
     * @author tyg
     * @date 2020-12-22 16:53
     */
    public static void unlock(String lockKey) {
        RLock lock = redissonClient.getLock(lockKey);
        // lock.isHeldByCurrentThread():必须是当前线程持有锁才可以解锁
        if (null != lock && lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }

}


4、直接使用RedissonLockUtil工具类就可以了。

    public void doSomeThing() {
        try {
            RedissonLockUtil.lock("redissonLock");
            // 处理业务 TODO
            
        } finally {
            RedissonLockUtil.unlock("redissonLock");
        }
    }

你可能感兴趣的:(SpringBoot,java,分布式锁,redisson,redis)