一,秒杀需要具备的功能:

秒杀通常是电商中用到的吸引流量的促销活动方式

搭建秒杀系统,需要具备以下几点:

1,限制每个用户购买的商品数量,(秒杀价格为吸引流量一般会订的很低,不能让一个用户全部抢购到手)

2,处理速度要快,避免在高并发的情况下发生堵塞

3,高并发情况下,不能出现库存超卖的情况

因为redis中对lua脚本执行的原子性,不会出现因高并发而导致数据查询的延迟

所以我们选择使用redis+lua来实现秒杀的功能

例子:如果同一个秒杀活动中有多件商品,而有人用软件刷接口的方式来下单,

这时就需要有针对当前活动的购买数量限制

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,本演示项目的相关信息

1,项目地址:

https://github.com/liuhongdi/seconddemo

2,项目原理:

在秒杀项目开始前,要把sku及其库存数同步到redis中,

有秒杀请求时,判断商品库存数,

判断用户已购买的同一sku数量,

判断用户已购买的同一秒杀活动中的商品数量,

如果以上两个数量大于0时,需要进行限制

如有问题时,返回秒杀失败

都没有问题时,减库存,返回秒杀成功

要注意的地方:

秒杀前要获取此活动中的对购买活动/sku的数量限制

秒杀成功后,如果用户未支付导致订单过期恢复库存时,redis中的库存数也要同步

3,项目结构:

三,lua代码说明

1,second.lua

local userId = KEYS[1]
local buyNum = tonumber(KEYS[2]) local skuId = KEYS[3]
local perSkuLim = tonumber(KEYS[4]) local actId = KEYS[5]
local perActLim = tonumber(KEYS[6]) local orderTime = KEYS[7] --用到的各个hash
local user_sku_hash = 'sec_'..actId..'_u_sku_hash'
local user_act_hash = 'sec_'..actId..'_u_act_hash'
local sku_amount_hash = 'sec_'..actId..'_sku_amount_hash'
local second_log_hash = 'sec_'..actId..'_log_hash' --当前sku是否还有库存
local skuAmountStr = redis.call('hget',sku_amount_hash,skuId)
if skuAmountStr == false then
--redis.log(redis.LOG_NOTICE,'skuAmountStr is nil ')
return '-3'
end;
local skuAmount = tonumber(skuAmountStr)
--redis.log(redis.LOG_NOTICE,'sku:'..skuId..';skuAmount:'..skuAmount)
if skuAmount <= 0 then
return '0'
end redis.log(redis.LOG_NOTICE,'perActLim:'..perActLim)
local userActKey = userId..'_'..actId
--当前用户已购买此活动多少件
if perActLim > 0 then
local userActNumInt = 0
local userActNum = redis.call('hget',user_act_hash,userActKey)
if userActNum == false then
--redis.log(redis.LOG_NOTICE,'userActKey:'..userActKey..' is nil')
userActNumInt = buyNum
else
--redis.log(redis.LOG_NOTICE,userActKey..':userActNum:'..userActNum..';perActLim:'..perActLim)
local curUserActNumInt = tonumber(userActNum)
userActNumInt = curUserActNumInt+buyNum
end
if userActNumInt > perActLim then
return '-2'
end
end local goodsUserKey = userId..'_'..skuId
--redis.log(redis.LOG_NOTICE,'perSkuLim:'..perSkuLim)
--当前用户已购买此sku多少件
if perSkuLim > 0 then
local goodsUserNum = redis.call('hget',user_sku_hash,goodsUserKey)
local goodsUserNumint = 0
if goodsUserNum == false then
--redis.log(redis.LOG_NOTICE,'goodsUserNum is nil')
goodsUserNumint = buyNum
else
--redis.log(redis.LOG_NOTICE,'goodsUserNum:'..goodsUserNum..';perSkuLim:'..perSkuLim)
local curSkuUserNumint = tonumber(goodsUserNum)
goodsUserNumint = curSkuUserNumint+buyNum
end --redis.log(redis.LOG_NOTICE,'------goodsUserNumint:'..goodsUserNumint..';perSkuLim:'..perSkuLim)
if goodsUserNumint > perSkuLim then
return '-1'
end
end --判断是否还有库存满足当前秒杀数量
if skuAmount >= buyNum then
local decrNum = 0-buyNum
redis.call('hincrby',sku_amount_hash,skuId,decrNum)
--redis.log(redis.LOG_NOTICE,'second success:'..skuId..'-'..buyNum) if perSkuLim > 0 then
redis.call('hincrby',user_sku_hash,goodsUserKey,buyNum)
end if perActLim > 0 then
redis.call('hincrby',user_act_hash,userActKey,buyNum)
end local orderKey = userId..'_'..skuId..'_'..buyNum..'_'..orderTime
local orderStr = '1'
redis.call('hset',second_log_hash,orderKey,orderStr) return orderKey
else
return '0'
end

2,功能说明:

--用到的各个参数

local userId  用户id

local buyNum 用户购买的数量

local skuId 用户购买的sku

local perSkuLim 每人购买此sku的数量限制

local actId 活动id

local perActLim 此活动中商品每人购买数量的限制

local orderTime 下订单的时间

--用到的各个hash
local user_sku_hash 每个用户购买的某一sku的数量
local user_act_hash 每个用户购买的某一活动中商品的数量
local sku_amount_hash sku的库存数
local second_log_hash 秒杀成功的记录

判断的流程:

判断商品库存数,

判断用户已购买的同一sku数量,

判断用户已购买的同一秒杀活动中的商品数量

四,java代码说明:

1,SecondServiceImpl.java

功能:传递参数,执行秒杀功能

 /*
* 秒杀功能,
* 调用second.lua脚本
* actId:活动id
* userId:用户id
* buyNum:购买数量
* skuId:sku的id
* perSkuLim:每个用户购买当前sku的个数限制
* perActLim:每个用户购买当前活动内所有sku的总数量限制
* 返回:
* 秒杀的结果
* * */
@Override
public String skuSecond(String actId,String userId,int buyNum,String skuId,int perSkuLim,int perActLim) { //时间字串,用来区分秒杀成功的订单
int START = 100000;
int END = 900000;
int rand_num = ThreadLocalRandom.current().nextInt(END - START + 1) + START;
String order_time = TimeUtil.getTimeNowStr()+"-"+rand_num; List<String> keyList = new ArrayList();
keyList.add(userId);
keyList.add(String.valueOf(buyNum));
keyList.add(skuId);
keyList.add(String.valueOf(perSkuLim));
keyList.add(actId);
keyList.add(String.valueOf(perActLim));
keyList.add(order_time); String result = redisLuaUtil.runLuaScript("second.lua",keyList);
System.out.println("------------------lua result:"+result);
return result;
}

2,RedisLuaUtil.java

功能:负责调用lua脚本的类

@Service
public class RedisLuaUtil {
@Resource
private StringRedisTemplate stringRedisTemplate; private static final Logger logger = LogManager.getLogger("bussniesslog");
/*
run a lua script
luaFileName: lua file name,no path
keyList: list for redis key
return other: fail
1: success
*/
public String runLuaScript(String luaFileName,List<String> keyList) {
DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();
redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/"+luaFileName)));
redisScript.setResultType(String.class);
String result = "";
String argsone = "none";
//logger.error("开始执行lua");
try {
result = stringRedisTemplate.execute(redisScript, keyList,argsone);
} catch (Exception e) {
logger.error("发生异常",e);
} return result;
}
}

五,测试秒杀的效果

1,访问:http://127.0.0.1:8080/second/index

添加库存

如图:

2,配置jmeter开始测试:

参见这一篇:

https://www.cnblogs.com/architectforest/p/13087798.html

定义测试用到的变量:

定义线程组数量为100

定义http请求:

在查看结果树中查看结果:

3,查看代码中的输出:

------------------lua result:u3_cpugreen_1_20200611162435-487367
------------------lua result:-2
------------------lua result:u1_cpugreen_2_20200611162435-644085
------------------lua result:u3_cpugreen_1_20200611162435-209653
------------------lua result:-1
------------------lua result:u2_cpugreen_1_20200611162434-333603
------------------lua result:-1
------------------lua result:-2
------------------lua result:-1
------------------lua result:u2_cpugreen_1_20200611162434-220636
------------------lua result:-2
------------------lua result:-1
...

每个用户的购买数量均未超过2单,秒杀的限制成功

六,查看spring boot的版本:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)

spring boot:redis+lua实现生产环境中可用的秒杀功能(spring boot 2.2.0)的更多相关文章

  1. spring boot:redis+lua实现顺序自增的唯一id发号器(spring boot 2.3.1)

    一,为什么需要生成唯一id(发号器)? 1,在分布式和微服务系统中, 生成唯一id相对困难, 常用的方式: uuid不具备可读性,作为主键存储时性能也不够好, mysql的主键,在分库时使用不够方便, ...

  2. Spring Boot 利用 nginx 实现生产环境的伪热更新

    当我们在服务器部署Java程序,特别是使用了 Spring Boot 生成单一 Jar 文件部署的时候,单一文件为我们开发单来的极大的便利性,保障程序的完整性.但同时对我们修改程序中的任何一处都带来重 ...

  3. 13.生产环境中的 redis 是怎么部署的?

    作者:中华石杉 面试题 生产环境中的 redis 是怎么部署的? 面试官心理分析 看看你了解不了解你们公司的 redis 生产集群的部署架构,如果你不了解,那么确实你就很失职了,你的 redis 是主 ...

  4. Kubernetes 在生产环境中常用架构

    Kubernetes 在生产环境中常用架构 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下图所示 在该架构中,我们可以将其分为四层,如下: Client层:即Kuber ...

  5. 明白生产环境中的jvm参数

    明白生产环境中的jvm参数 写代码的时候,程序写完了,发到线上去运行,跑一段时间后,程序变慢了,cpu负载高了--一堆问题出来了,所以了解一下生产环境的机器上的jvm配置是有必要的.比如说: JDK版 ...

  6. 生产环境中 Ngx_lua 使用技巧和应用的范例

    生产环境中 Ngx_lua 使用技巧和应用的范例 时间 -- :: 51CTO技术博客 原文 http://rfyiamcool.blog.51cto.com/1030776/1252501 主题 L ...

  7. 生产环境中使用Docker Swarm的一些建议

    译者按: 实践中会发现,生产环境中使用单个Docker节点是远远不够的,搭建Docker集群势在必行.然而,面对Kubernetes, Mesos以及Swarm等众多容器集群系统,我们该如何选择呢?它 ...

  8. 生产环境中tomcat的配置

    生产环境中要以daemon方式运行tomcat 通常在开发环境中,我们使用$CATALINA_HOME/bin/startup.sh来启动tomcat, 使用$CATALINA_HOME/bin/sh ...

  9. Kubernetes用户指南(三)--在生产环境中使用Pod来工作、管理部署

    一.在生产环境中使用Pod来工作 本节将介绍一些在生产环境中运行应用非常有用的功能. 1.持久化存储 容器的文件系统只有当容器正常运行时有效,一旦容器奔溃或者重启,所有对文件系统的修改将会丢失,从一个 ...

随机推荐

  1. Charles的几个用途

    1.拦截请求,篡改请求和响应 拦截请求,修改请求可以测试网站中一些异常的情况,检查服务端是否有校验的情况 检查是否存在漏洞,就看拦截之后修改过的数据是否写进了数据库 使用方法: 举例一:上传文件 1. ...

  2. oracle之二实例管理及数据库的启动/关闭

    实例管理及数据库的启动/关闭   2.1 实例和参数文件 1.instance 功能:用于管理和访问database.instance在启动阶段读取初始化参数文件(init parameter fil ...

  3. [剑指Offer]55-题目一:二叉树的深度 题目二:平衡二叉树

    题目一 题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题解 递归. 代码 class TreeNode { int v ...

  4. 使用Mysql分区表对数据库进行优化

    早期工作中没有做好足够的设计,目前记录表单表数据2000w且无有效索引,表现是分页缓慢,模糊查询拉闸. 当前业务中,写操作会多于读操作,时不时会遇到慢SQL占用过多的数据连接,导致写操作无法正常进行. ...

  5. hystrix文档翻译之配置

    Hystrix使用Archaius作为配置的默认实现,下面介绍的是HystrixPropertiesStrategy的默认实现,你也可以通过插件方式重新实现. 每一个配置有四个级别: 全局默认 当下面 ...

  6. C# 9.0 新特性预览 - 顶级语句

    C# 9.0 新特性预览 - 顶级语句 前言 随着 .NET 5 发布日期的日益临近,其对应的 C# 新版本已确定为 C# 9.0,其中新增加的特性(或语法糖)也已基本锁定,本系列文章将向大家展示它们 ...

  7. kali学习wiki

    https://github.com/Keybird0/Kali-learning-notes/wiki

  8. google chrome安装非官方市场插件方法

    1. 下载 Chrome组策略管理模板"chrome.adm"(下载地址):2. Win+R 打开运行,输入 gpedit.msc,确定:3. 依次展开 本地计算机策略 > ...

  9. zabbix_server.conf配置文件参数

    NodeID: 在amster-child 的分布式架构中,这个ID是唯一标识zabbix node的号码 ListenPort:Trapper 类型Item监听的端口, SourceIP: 在连接其 ...

  10. 使用VMware虚拟机安装RHEL7(RedHat Enterprise Linux7)步骤

    准备工具: 1.VMware Workstation 14 2.RedHat Enterprise Linux 7.0镜像文件 在虚拟机内设置操作系统的硬件标准 单击"创建新的虚拟机&quo ...