当前位置:网站首页>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分布式锁架构图

边栏推荐
- 如果辨别我现在交易的外盘股指期货交易平台是否正规安全?
- What is the concept of string in PHP
- ZABBIX trigger explanation
- *Write a program to initialize a string object with a vector < char> container*/
- Mysqldump principle
- How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
- F1C100S自制开发板调试过程
- Intel hex, Motorola S-Record format detailed analysis
- Mysql表数据比较大情况下怎么修改添加字段
- How does the trading platform for speculation in spot gold ensure capital security?
猜你喜欢

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load

Simple custom MVC

Intel hex, Motorola S-Record format detailed analysis

自定义MVC的使用

数据库的下一个变革方向——云原生数据库

Reasons for MySQL master-slave database synchronization failure

通用分页(2)

约瑟夫环 数学解法

Use of custom MVC

Prompt learning a blood case caused by a space
随机推荐
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...
DC/DC变换器轻载时三种工作模式的原理及优缺点
Reasons for MySQL master-slave database synchronization failure
&nbsp;与空格的区别
List of development tools
Study diary: February 15, 2022
正则全匹配:密码由8位以上数字,大小写字母,特殊字符组成
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
Code for generating test and training sets
How does the trading platform for speculation in spot gold ensure capital security?
Formal and actual parameters, value passing and address passing
设备驱动程序的原理
Auto.js学习笔记15:autojs的UI界面基础篇2
Use of Arthas
Distributed file storage system fastdfs hands on how to do it
Jvxetable增加自定义按钮
golang bilibili直播彈幕姬
Quick sort, cluster index, find the k-largest value in the data
怎么使用Vant实现数据分页和下拉加载
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)