当前位置:网站首页>Redis高并发分布式锁(学习总结)
Redis高并发分布式锁(学习总结)
2022-06-30 03:05:00 【bingtanghulu_6】
0. 为什么要设计分布式锁?
正常的业务操作在多线程情况下不能实现原子性,要么全部不执行,要么全部执行。在分布式场景下更是会出现这种多个线程对同一条数据操作的情况。
1. redis分布式锁实现
下面是redis官方推荐的三款客户端:

1.1 引入依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.6.5</version>
</dependency>1.2 代码实现reddison
package com.redisson;
import org.redisson.Redisson;
import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@RestController
public class IndexController {
@Autowired
private Redisson redisson;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/deduct_stock")
public String deductStock() {
String lockKey = "product_101";
RLock redissonLock = redisson.getLock(lockKey);
try {
redissonLock.lock(); //setIfAbsent(lockKey, clientId, 30, TimeUnit.SECONDS);
int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock")); // jedis.get("stock")
if (stock > 0) {
int realStock = stock - 1;
stringRedisTemplate.opsForValue().set("stock", realStock + ""); // jedis.set(key,value)
System.out.println("扣减成功,剩余库存:" + realStock);
} else {
System.out.println("扣减失败,库存不足");
}
} finally {
redissonLock.unlock();
}
return "end";
}
}1.3 redisson内部源码实现
1.3.1 reddsion分布式锁架构图

边栏推荐
- Mysql表数据比较大情况下怎么修改添加字段
- Prompt learning a blood case caused by a space
- Multi card server usage
- How do I enable assembly binding logging- How can I enable Assembly binding logging?
- O & M (20) make and start USB flash disk and install win10
- Mysqldump principle
- Auto.js学习笔记15:autojs的UI界面基础篇2
- How to use redis to realize the like function
- MySQL extracts strings from table fields
- Study diary: February 15, 2022
猜你喜欢

怎么利用Redis实现点赞功能

什么是外链和内链?

LeetCode 3. Longest substring without duplicate characters

简单自定义mvc

Cross domain, CORS, jsonp

hudi记录

Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging

Prompt learning a blood case caused by a space

Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0

uniapp 地址转换经纬度
随机推荐
How to prevent duplicate submission under concurrent requests
General paging (2)
什么是外链和内链?
&nbsp;与空格的区别
mysql 主从数据库同步失败的原因
Code for generating test and training sets
What are outer chain and inner chain?
Gulang bilibilibili Live Screen Jackie
C console format code
约瑟夫环 数学解法
How does the trading platform for speculation in spot gold ensure capital security?
Summary of interview and Employment Questions
*Write a program to initialize a string object with a vector < char> container*/
O & M (20) make and start USB flash disk and install win10
JS conversion of letters and numbers
GTK interface programming (I): Environment Construction
(graph theory) connected component (template) + strongly connected component (template)
Utf8 error in Oracle migration of Jincang Kingbase database
X书6.97版本shield-unidbg调用方式
Tp6 framework integrates JWT for token authentication