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

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

一、单个服务

1.代码

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

java 复制代码
@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.解决办法:加锁

java 复制代码
@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配置负载均衡

xml 复制代码
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.依赖和配置文件

xml 复制代码
<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>
yaml 复制代码
spring:
redis:
  host:localhost
  port:6379

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

2.代码

注解

java 复制代码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {
    String lockName();
}

切面

java 复制代码
@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();
            }
        }
    }
}

接口

java 复制代码
@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.结果

实现了分布式锁,俩个服务依次接受了所有请求,并无重复值

相关推荐
码事漫谈4 小时前
多人共用一个 key,缓存命中率会不会因此降低?
后端
考虑考虑4 小时前
docker compose环境变量替换
运维·后端·自动化运维
武雄(小星Ai)5 小时前
飞书API上传26MB文件偶发失败:错误码9499与空响应体排坑实录
后端·api·排坑实录
徐小夕5 小时前
JitWord 4.0 万字分享:从协同工具到AI Word操作系统,聊聊3年产品创业史
前端·vue.js·后端
geovindu7 小时前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
Flynt7 小时前
把公司项目迁到 Spring Boot 4.0:编译通过只是开始
java·spring boot·后端
IT_陈寒8 小时前
Redis误用keys命令把生产环境搞崩了,血的教训
前端·人工智能·后端
码哥字节8 小时前
AI一天出百本书,写不出我被Redis坑的那三天三夜
redis·ai编程工具
她的男孩8 小时前
多租户和数据权限怎么共存?扒完拦截器注册链路,我找到 4 个隐蔽的坑
java·后端·架构
用户8356290780518 小时前
使用 Python 设置 Excel 页眉和页脚
后端·python