SpringBoot+Redis实现秒杀系统实战

一、单机版

SpringBootRedisApplication

package com.example.demo;

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootRedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootRedisApplication.class, args);
	}

	@Bean
	public Redisson redisson(){
		Config config = new Config();
		config.useSingleServer().setAddress("redis://*.*.*.*:6379").setDatabase(0);
		return (Redisson)Redisson.create(config);
	}

}

GoodControllerSimple

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodControllerSimple {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/deduct_stock")
    public String deduceStock(){
        try {
            synchronized (this){
                int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
                if (stock > 0) {
                    int realStock = stock -1;
                    stringRedisTemplate.opsForValue().set("stock",String.valueOf(realStock));
                    System.out.println("扣减成功,库存还剩下: " + realStock);
                }else{
                    System.out.println("扣减成功,库存不足!");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            return "商品已经售完/活动结束/调用超时,欢迎下次光临";
        }
    }
}

二、单机版超卖问题解决方案1

GoodControllerIndex

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class GoodControllerIndex {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/killGoodsIndex")
    public String killGoods(){
        String  lockKey = "product_001";
        String  clientId = UUID.randomUUID().toString();//定义唯一的当前线程标志,用于最后删除标志,防止误删别的锁
        Boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey,clientId,10L, TimeUnit.SECONDS);//setnx功能
        if(!result){
            return "商品抢购失败】";
        }
        try {
            String  stock  = stringRedisTemplate.opsForValue().get("stock");//获取库存量
            int amount = Integer.parseInt(stock);
            if (amount > 0) {
                int realStock = amount - 1;
                stringRedisTemplate.opsForValue().set("stock", String.valueOf(realStock));//扣减库存后,将真实剩余库存存入
                System.out.println("扣减成功,库存还剩下: " + realStock);
            }else{
                System.out.println("扣减成功,库存不足!");
            }
        }finally {
            if (clientId.equals(stringRedisTemplate.opsForValue().get(lockKey))){//只删除本线程锁lockKey
                stringRedisTemplate.delete(lockKey);
            }
        }

        return "商品已经售完/活动结束/调用超时,欢迎下次光临";
    }
}

三、超卖问题解决方案2

GoodRedissonController

package com.example.demo.controller;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class GoodRedissonController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private Redisson redisson;

    @GetMapping("/killGoodsRedisson")
    public String killGoods(){
        String  lockKey = "product_001";

        RLock redissonLock = redisson.getLock(lockKey);
        try {
            redissonLock.lock();
            String  stock  = stringRedisTemplate.opsForValue().get("stock");//获取库存量
            int amount = stock == null ? 0 : Integer.parseInt(stock);
            if (amount > 0) {
                int realAmount = amount - 1;
                stringRedisTemplate.opsForValue().set("stock", String.valueOf(realAmount));//扣减库存后,将真实剩余库存存入
                System.out.println("扣减成功,库存还剩下: " + realAmount);
            }else{
                System.out.println("扣减成功,库存不足!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            redissonLock.unlock();
        }

        return "商品已经售完/活动结束/调用超时,欢迎下次光临";
    }
}

四、Jmeter压测
首先在redis中创建库存stock为200
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
结果如下,最后扣减为0
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值