springboot redission 分布式锁

Spring Boot中使用Redisson实现分布式锁的方法如下:

  1. 首先,需要在项目中引入Redisson依赖。在pom.xml文件中添加以下依赖:

```xml

<dependency>

<groupId>org.redisson</groupId>

<artifactId>redisson</artifactId>

<version>3.16.4</version>

</dependency>

```

  1. 在Spring Boot配置文件(如application.yml)中配置Redisson连接信息:

```yaml

redisson:

config:

singleServerConfig:

address: "redis://127.0.0.1:6379"

timeout: 10000

pingTimeout: 10000

```

  1. 创建一个Redisson配置类,用于初始化RedissonClient实例:

```java

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;

@Configuration

public class RedissonConfig {

@Value("${redisson.config.singleServerConfig.address}")

private String address;

@Value("${redisson.config.singleServerConfig.timeout}")

private int timeout;

@Value("${redisson.config.singleServerConfig.pingTimeout}")

private int pingTimeout;

@Bean

public RedissonClient redissonClient() {

Config config = new Config();

config.useSingleServer()

.setAddress(address)

.setTimeout(timeout)

.setPingTimeout(pingTimeout);

return Redisson.create(config);

}

}

```

  1. 使用RedissonClient实例获取分布式锁:

```java

import org.redisson.api.RLock;

import org.redisson.api.RedissonClient;

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

import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service

public class DistributedLockService {

@Autowired

private RedissonClient redissonClient;

public void lock(String lockKey, Runnable task, long waitTime, long leaseTime) {

RLock lock = redissonClient.getLock(lockKey);

try {

// 尝试获取锁,等待时间waitTime,锁过期时间leaseTime

if (lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS)) {

try {

task.run();

} finally {

lock.unlock();

}

} else {

System.out.println("获取锁失败");

}

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

if (lock.isHeldByCurrentThread()) {

lock.unlock();

}

}

}

}

```

  1. 在需要使用分布式锁的地方调用DistributedLockService的lock方法:

```java

@Autowired

private DistributedLockService distributedLockService;

public void someMethod() {

String lockKey = "someLockKey";

distributedLockService.lock(lockKey, () -> {

// 需要加锁执行的代码

}, 10, 60);

}

```

以上代码示例展示了如何在Spring Boot项目中使用Redisson实现分布式锁。

相关推荐
ruleslol12 分钟前
SpringBoot26-@Configuration + @Component
spring boot
薛定谔的猫198229 分钟前
LLaMA-Factory +DeepSpeed 分布式多卡训练实战全解
分布式·deepspeed·zero·多卡训练·多卡分布式训练
稚南城才子,乌衣巷风流1 小时前
RabbitMQ 消息队列:从入门到实战
分布式·rabbitmq
霸道流氓气质1 小时前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
风景的人生2 小时前
流式输出与springboot中的响应式编程
java·spring boot·ai编程
dkbnull2 小时前
Spring Boot依赖注入方式对比详解
spring boot·spring
__log3 小时前
幂等性设计:从“重复提交“到“稳如磐石“的系统防护
java·开发语言·spring boot
zhangjw343 小时前
第35篇:Spring Boot入门:自动配置+快速搭建,简化企业开发
java·spring boot·后端
czhc11400756633 小时前
7.23:Claude code:注册->封表
wpf
Database_Cool_15 小时前
单机 MySQL 迁移到分布式数据库方便吗?阿里云 PolarDB-X 100% MySQL 协议兼容零改造平滑迁移
数据库·分布式·mysql