springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)
本文實(shí)例為大家分享了springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
1. 直接service,我們會(huì)介紹兩種秒殺模式
public interface GoodsService { /** * 通過lua腳本實(shí)現(xiàn)的秒殺 * @param skuCode 商品編碼 * @param buyNum 購買數(shù)量 * @return 購買數(shù)量 */ Long flashSellByLuaScript(String skuCode,int buyNum); /** * 通過redis 事務(wù) 實(shí)現(xiàn)的秒殺 * @param skuCode 商品編碼 * @param buyNum 購買數(shù)量 * @return 購買數(shù)量 */ Long flashSellByRedisWatch(String skuCode,int buyNum); }
2. service實(shí)現(xiàn)類
import org.springframework.dao.DataAccessException; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.SessionCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Collections; import java.util.List; @Service public class GoodsServiceImpl implements GoodsService { @Resource private StringRedisTemplate stringRedisTemplate; @Override public Long flashSellByLuaScript(String skuCode,int num) { //下面是lua腳本 String luaScript ="local buyNum = ARGV[1]\n" + "local goodsKey = KEYS[1] \n" + "local goodsNum = redis.call('get',goodsKey) \n" + "if goodsNum >= buyNum \n" + "then redis.call('decrby',goodsKey,buyNum) \n" + "return buyNum \n" + "else \n" + "return '0'\n" + "end\n" + "\n" ; DefaultRedisScript<String> re = new DefaultRedisScript<String>(); //設(shè)置腳本 re.setScriptText(luaScript); //定義返回值類型,注意,如果沒有這個(gè)定義,Spring不會(huì)返回結(jié)果 re.setResultType(String.class); RedisSerializer<String> stringRedisSerializer = stringRedisTemplate.getStringSerializer(); //執(zhí)行LUA腳本 String result = (String) stringRedisTemplate.execute(re, stringRedisSerializer, stringRedisSerializer, null); return Long.valueOf(result); } @Override public Long flashSellByRedisWatch(String skuCode,int num){ SessionCallback<Long> sessionCallback = new SessionCallback<Long>() { @Override public Long execute(RedisOperations operations) throws DataAccessException { int result = num; //redis 樂觀鎖 //我們觀察商品編碼是否發(fā)生改變 operations.watch(skuCode); ValueOperations<String, String> valueOperations = operations.opsForValue(); String goodsNumStr = valueOperations.get(skuCode); Integer goodsNum = Integer.valueOf(goodsNumStr); //標(biāo)記一個(gè)事務(wù)塊的開始。 //事務(wù)塊內(nèi)的多條命令會(huì)按照先后順序被放進(jìn)一個(gè)隊(duì)列當(dāng)中, //最后由 EXEC 命令原子性(atomic)地執(zhí)行。 operations.multi(); if (goodsNum >= num) { valueOperations.increment(skuCode, 0 - num); } else { result = 0; } //多條命令執(zhí)行的結(jié)果集合 List exec = operations.exec(); if(exec.size()>0){ System.out.println(exec); } return (long) result; } }; return stringRedisTemplate.execute(sessionCallback); } //省略 其他的方法 }
3. controller
但是首先要向你的redis里面仍一個(gè)數(shù)據(jù),key='xiaomi',value='100'
@ApiOperation(value = "用事務(wù)秒殺測(cè)試接口", notes = "用事務(wù)秒殺測(cè)試接口") @RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET) @ResponseBody public Long miaoTransaction() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); return res; } @ApiOperation(value = " 秒殺Lua測(cè)試接口", notes = "秒殺Lua測(cè)試接口") @RequestMapping(value = "/miaoLua", method = RequestMethod.GET) @ResponseBody public Long miaoLua() { Long res = goodsService.flashSellByRedisWatch("xiaomi", 1); System.out.println(res.toString()); return res; }
然后就可以用jemeter并發(fā)訪問了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot+redis+Vue實(shí)現(xiàn)秒殺的項(xiàng)目實(shí)踐
- springboot?+rabbitmq+redis實(shí)現(xiàn)秒殺示例
- SpringBoot+RabbitMQ+Redis實(shí)現(xiàn)商品秒殺的示例代碼
- 基于Redis結(jié)合SpringBoot的秒殺案例詳解
- SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- SpringBoot使用Redisson實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- SpringBoot+Redis隊(duì)列實(shí)現(xiàn)Java版秒殺的示例代碼
相關(guān)文章
SpringBoot應(yīng)用程序轉(zhuǎn)換成WAR文件詳解
其實(shí)一般使用SpringBoot使用打成jar包比較省事的,但也有很多童鞋是習(xí)慣使用WAR包的,下面這篇文章主要給大家介紹了關(guān)于SpringBoot轉(zhuǎn)換WAR的相關(guān)資料,需要的朋友可以參考下2022-11-11通過Java實(shí)現(xiàn)RSA加密與驗(yàn)證的方法詳解
RSA是一種非對(duì)稱加密算法,是目前廣泛應(yīng)用于加密和數(shù)字簽名領(lǐng)域的一種加密算法,本文主要講述如何通過Java實(shí)現(xiàn)RSA加密與驗(yàn)證,應(yīng)用場(chǎng)景為與其他平臺(tái)對(duì)接接口時(shí),通過RSA加密和解密驗(yàn)證請(qǐng)求的有效性,在對(duì)接時(shí)雙方互換公鑰,需要的朋友可以參考下2023-12-12springboot整合logback實(shí)現(xiàn)日志管理操作
本章節(jié)是記錄logback在springboot項(xiàng)目中的簡單使用,本文將會(huì)演示如何通過logback將日志記錄到日志文件或輸出到控制臺(tái)等管理操作,感興趣的朋友跟隨小編一起看看吧2024-02-02解決idea導(dǎo)入maven項(xiàng)目缺少jar包的問題方法
這篇文章主要介紹了解決idea導(dǎo)入maven項(xiàng)目缺少jar包的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了javaweb登錄驗(yàn)證碼的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11jenkins配置詳細(xì)指南(附j(luò)dk多個(gè)版本配置)
Jenkins是一款CICD(持續(xù)集成與持續(xù)交付)工具,Jenkins可以幫你在寫完代碼后,一鍵完成開發(fā)過程中的一系列自動(dòng)化部署的工作,這篇文章主要給大家介紹了關(guān)于jenkins配置的相關(guān)資料,文中還附j(luò)dk多個(gè)版本配置指南,需要的朋友可以參考下2024-02-02Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例
這篇文章主要介紹了Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例,涉及構(gòu)造函數(shù)私有化等相關(guān)內(nèi)容,需要的朋友可以了解下。2017-09-09