springboot项目中RedissonRLock分布式锁的使用

编程

创建springboot redis项目
    springboot-redis-demo

添加依赖包

<dependency>

<groupId>org.redisson</groupId>

<artifactId>redisson-spring-boot-starter</artifactId>

<version>3.12.5</version>

</dependency>

添加redisson配置项redisson.yml

singleServerConfig:

password: 111111

address: "redis://127.0.0.1:6379"

database: 0

创建redisson配置类

import org.redisson.Redisson;

import org.redisson.api.RedissonClient;

import org.redisson.config.Config;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

@Configuration

public class RedssonConfig {

@Primary

@Bean(name = "redissonClient")

public RedissonClient redissonClient() throws IOException {

return Redisson.create(Config.fromYAML(new ClassPathResource("redisson-single.yml").getInputStream()));

}

@Bean(destroyMethod = "shutdown")

public RedissonClient shutdown(@Qualifier("redissonClient") RedissonClient redissonClient) {

return redissonClient;

}

}

创建RDistributedLockerUtil工具类

import org.redisson.api.RLock;

import org.redisson.api.RedissonClient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component

public class RedissonLockerUtil {

// RedissonClient已经由配置类生成,这里自动装配即可

private RedissonClient redissonClient;

@Autowired

private RedissonLockerUtil(@Qualifier("redissonClient") RedissonClient redissonClient) {

this.redissonClient = redissonClient;

}

/**

* lock(), 拿不到lock就不罢休,不然线程就一直block

*

* @param lockKey

* @return

*/

public RLock lock(String lockKey) {

RLock lock = redissonClient.getLock(lockKey);

lock.lock();

return lock;

}

/**

* leaseTime为加锁时间,单位为秒

*

* @param lockKey

* @param leaseTime

* @return

*/

public RLock lock(String lockKey, long leaseTime) {

RLock lock = redissonClient.getLock(lockKey);

lock.lock(leaseTime, TimeUnit.SECONDS);

return null;

}

/**

* timeout为加锁时间,时间单位由unit确定

*

* @param lockKey

* @param unit

* @param timeout

* @return

*/

public RLock lock(String lockKey, TimeUnit unit, long timeout) {

RLock lock = redissonClient.getLock(lockKey);

lock.lock(timeout, unit);

return lock;

}

public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) {

RLock lock = redissonClient.getLock(lockKey);

try {

return lock.tryLock(waitTime, leaseTime, unit);

} catch (InterruptedException e) {

return false;

}

}

public void unlock(String lockKey) {

RLock lock = redissonClient.getLock(lockKey);

lock.unlock();

}

public void unlock(RLock lock) {

lock.unlock();

}

public void shutdown() {

redissonClient.shutdown();

}

}

 

以上是 springboot项目中RedissonRLock分布式锁的使用 的全部内容, 来源链接: utcz.com/z/516018.html

回到顶部