概述
Redisson入门
第一步:引入依赖
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.13.6</version></dependency>
第二步:配置文件
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class RedissonConfig {@Beanpublic RedissonClient redissonClient(){//配置Config config = new Config();config.useSingleServer().setAddress("redis://192.168.136.132:6379").setPassword("pz030812...");//创建RedissonClient对象return Redisson.create(config);}
}
第三步:使用锁
Long id = UserHolder.getUser().getId();//创建锁对象RLock lock = redissonClient.getLock("lock:order" + id);//获取锁boolean trylock = lock.tryLock();//判断是否获得锁成功if (!trylock) {//获取锁失败return Result.fail("不允许重复下单!");}try {//获得锁进行操作//获得Spring的代理对象(事务)IVoucherOrderService proxy = (IVoucherOrderService) AopContext.currentProxy();return proxy.createVoucherOrder(voucherId);} finally {//释放锁lock.unlock();}
Redisson锁可重入原理
就是在Lock中利用hash数据加一个标识字段,标识有几个自己线程中获得锁的数量,每次获取锁的流程:①判断锁是否存在,如果不存在则说明没人用锁,则获取到锁,并且设置好标识以及线程名②如果存在则判断是不是自己线程中的锁③如果不是,就说明是别人的锁,则获取失败返回错误④如果判断是自己线程的锁,则把标识字段+1,并且获得锁⑤之后只要是自己线程中的人来获取锁就直接更改标识。
而释放锁的流程:①先判断这个锁是不是自己线程的②如果不是就说明锁已经被释放了③如果是的话,就把标识减1④再去看标识是否已经等于0,即到了最外层⑤如果是的话就直接释放锁⑥不是的话就重置锁的有限期,继续执行业务
Redisson锁可重试,超时释放(看门狗)原理
Redisson锁主从一致性
multiLock原理:
异步实现秒杀商品
执行流程:将判断资格和写进数据库分离开来,使得并发能力大大增强
代码执行:
①在添加秒杀商品的同时,将数据写进Redis当中
@Transactionalpublic void addSeckillVoucher(Voucher voucher) {// 保存优惠券save(voucher);// 保存秒杀信息SeckillVoucher seckillVoucher = new SeckillVoucher();seckillVoucher.setVoucherId(voucher.getId());seckillVoucher.setStock(voucher.getStock());seckillVoucher.setBeginTime(voucher.getBeginTime());seckillVoucher.setEndTime(voucher.getEndTime());seckillVoucherService.save(seckillVoucher);//保存秒杀劵到Redis中stringRedisTemplate.opsForValue().set("seckill:stock"+voucher.getId(),voucher.getStock().toString());}
②编写Lua脚本,实现资格判断
--1.参数列表
--1.1。优惠券id
local voucherId = ARGV[1]
--1.2.用户id
local userId = ARGV[2]--2.数据key
--2.1 库存key
local stockKey = 'seckill:stock' .. voucherId
--2.1 订单id
local orderKey = 'seckill:order' .. voucherId--3.脚本业务
--3.1,判断库存是否充足 get stockKey
if(tonumber(redis.call('get',stockKey)) <= 0) thenreturn 1end
--3.2 判断用户是否下单 SISMEMBER orderKey userID
if(redis.call('sismember',orderKey,userId) == 1) then--3.3 存在,说明是重复下单,返回2return 2
end
--3.4 扣库存
redis.call('incrby',stockKey,-1)
--3.5 下单
redis.call('sadd',orderKey,userId)
③编码
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {@Autowiredprivate ISeckillVoucherService iSeckillVoucherService;@Autowiredprivate RedisUUID redisUUID;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedissonClient redissonClient;private static final DefaultRedisScript<Long> SECKILL_SCRIPT;static {SECKILL_SCRIPT = new DefaultRedisScript<>();SECKILL_SCRIPT.setLocation(new ClassPathResource("seckill.lua"));SECKILL_SCRIPT.setResultType(Long.class);}//创建阻塞队列private BlockingDeque<VoucherOrder> orderTasks = new ArrayBlockingQueue<>(1024 * 1024);//创建线程池private static final ExecutorService SECKILL_ORDER_EXECUTOR = Executors.newSingleThreadExecutor();@PostConstructprivate void init(){SECKILL_ORDER_EXECUTOR.submit(new VoucherOrderHandler());}private class VoucherOrderHandler implements Runnable{public void run(){while (true){try {//1,获取队列中的订单信息VoucherOrder voucherOrder = orderTasks.take();//2.创建订单handleVoucherOrder(voucherOrder);} catch (InterruptedException e) {log.error("处理订单异常!",e);}}}}private void handleVoucherOrder(VoucherOrder voucherOrder) {Long userId = voucherOrder.getUserId();//创建锁对象RLock lock = redissonClient.getLock("lock:order" + userId);//获取锁boolean trylock = lock.tryLock();//判断是否获得锁成功if (!trylock) {//获取锁失败return;}try {proxy.createVoucherOrder(voucherOrder);} finally {//释放锁lock.unlock();}}private IVoucherOrderService proxy;@Overridepublic Result seckillVoucher(Long voucherId) {Long id = UserHolder.getUser().getId();//1.执行Lua脚本Long result = stringRedisTemplate.execute(SECKILL_SCRIPT,Collections.emptyList(),voucherId.toString(),id.toString());//2.判断结果为0int r = result.intValue();if (r != 0){//2.1 不为0,代表没有购买资格return Result.fail(r == 1 ? "库存不足" : "不能重复下单");}//2.2 为0,有购买资格,把下单信息保存到阻塞队列VoucherOrder voucherOrder = new VoucherOrder();//2.3,订单id----id生成器long order = redisUUID.nextid("order");voucherOrder.setVoucherId(order);//2.4,用户idvoucherOrder.setUserId(id);//2.5,商品idvoucherOrder.setVoucherId(voucherId);//2.6,放入阻塞队列orderTasks.add(voucherOrder);//获取代理对象proxy = (IVoucherOrderService) AopContext.currentProxy();//3.返回订单idreturn Result.ok(order);}@Transactionalpublic void createVoucherOrder(VoucherOrder voucherOrder) {//一人一单问题Long id = voucherOrder.getUserId();Integer count = query().eq("user_id", id).eq("voucher_id", voucherOrder.getVoucherId()).count();if(count > 0){return;}//4,如果有就减扣库存boolean sucess = iSeckillVoucherService.update().setSql("stock = stock - 1").eq("voucher_id", voucherOrder.getVoucherId()).gt("stock",0).update();if (!sucess) {return;}//6,保存进数据库save(voucherOrder);}
}