当前位置: 首页 > news >正文

SpringBoot与Redisson整合,用注解方式解决分布式锁的使用问题

文章引用:https://mp.weixin.qq.com/s/XgdKE2rBKL0-nFk2NJPuyg

一、单个服务

1.代码

该接口的作用是累加一个值,访问一次该值加1

@RestController
public class LockController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/increment")
    public String increment() {
        int counter = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
        counter++;
        System.out.println("当前结果:" + counter);
        stringRedisTemplate.opsForValue().set("count", String.valueOf(counter));
        return "结果:" + counter;
    }
}

2.jmeter工具压测

用100个线程,每个线程访问100次去压测,理论上该值最后应该返回10000
在这里插入图片描述

3.结果

在这里插入图片描述
在这里插入图片描述

发生了线程安全问题,部分请求返回结果相同

4.解决办法:加锁

@RestController
public class LockController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/increment")
    public synchronized String increment() {
        int counter = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
        counter++;
        System.out.println("当前结果:" + counter);
        stringRedisTemplate.opsForValue().set("count", String.valueOf(counter));
        return "结果:" + counter;
    }
}

在这里插入图片描述
在非集群模式下,简单上锁能解决线程安全问题。

二、集群模式

代码相同,idea启动两个相同的服务

1.启动两个服务

在这里插入图片描述

2.nginx配置负载均衡

upstream backend {
        # 定义后端服务器列表
        server localhost:8081;
        server localhost:8082;
    }
	
	server {
        listen 8080;

        location / {
            # 将请求代理到后端服务器
            proxy_pass http://backend;
        }
    }

3.jmeter工具压测

用100个线程,每个线程访问100次去压测,理论上该值最后应该返回10000
在这里插入图片描述
在这里插入图片描述
还是出了线程安全问题,synchronized只能锁住单个服务

三、SpringBoot整合Redisson(注解方式)

1.依赖和配置文件

<dependencies>
        <!-- Spring Boot Starter for Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--         Redisson Dependency-->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.17.6</version><!-- 请根据需要调整版本 -->
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
    </dependencies>
spring:
redis:
  host:localhost
  port:6379

# Redisson configuration
redisson:
  single-server-address:redis://@localhost:6379

2.代码

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {
    String lockName();
}

切面

@Aspect
@Component
public class DistributedLockAspect {

    @Autowired
    private RedissonClient redissonClient;

    @Around("@annotation(DistributedLock)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        DistributedLock annotation = signature.getMethod().getAnnotation(DistributedLock.class);
        String lockName = annotation.lockName();

        RLock lock = redissonClient.getLock(lockName);

        try {
            // 尝试获取锁,最多等待10秒,持有锁的时间为30秒
            if (!lock.tryLock(10, 30, TimeUnit.SECONDS)) {
                throw new RuntimeException("Failed to acquire lock.");
            }

            return joinPoint.proceed();
        } finally {
            // 确保在任何情况下都能释放锁
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}

接口

@RestController
public class LockController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/increment")
    @DistributedLock(lockName = "counterLock")
    public String increment() {
        int counter = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
        counter++;
        System.out.println("当前结果:" + counter);
        stringRedisTemplate.opsForValue().set("count", String.valueOf(counter));
        return "结果:" + counter;
    }
}

3.结果

实现了分布式锁,俩个服务依次接受了所有请求,并无重复值
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 【菜鸟飞】AI多模态:vsCode下python访问阿里云通义文生图API
  • redis MISCONF Redis is configured to save RDB snapshots报错解决
  • 【电源专题】案例:因没有关注Stand-by mode中的两种类别导致选型电池组保护芯片错误
  • 鸿蒙NEXT项目实战-百得知识库01
  • 【Zephyr】【一】学习笔记
  • Linux驱动开发实战之SRIO驱动(一)
  • 江小南的题目讲解
  • 继承父类的实体对象没打印出来父级属性问题
  • YOLOv5部署全场景问题解决方案手册(2025版)
  • 2025年汽车加气站操作工考试精选题库
  • postman小白教程(从入门到实战,详细教学)
  • 【鸿蒙开发】Hi3861学习笔记- OLED示例
  • Execution failed for task ‘:path_provider_android:compileDebugJavaWithJavac‘.
  • 邮件祝福常见模版
  • #Hadoop全分布式安装 #mysql安装 #hive安装
  • 循环神经网络(Recurrent Neural Network, RNN)与 Transformer
  • 【嵌入式学习】补码-加减乘除电路
  • Netty源码—2.Reactor线程模型二
  • 强推 Maven多镜像源快速切换工具,GUI操作超便捷
  • 379_C++_通过小时、天、月、年的地址偏移,上告的图片数据存储在不同的时间粒度位置;提取的时候按照同样的小时、天、月、年偏移,提取数据
  • 国际金价下跌,中概股多数上涨,穆迪下调美国主权信用评级
  • 病重老人取钱在银行门口去世,家属:已协商一致
  • 首次采用“顶置主星+侧挂从星”布局,长二丁“1箭12星”发射成功
  • 日月谭天丨这轮中美关税会谈让台湾社会看清了什么?
  • 微软宣布将裁员3%
  • 学习教育期间违规吃喝,李献林、叶金广等人被通报