springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)
本文實(shí)例為大家分享了springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目是有地址的,我會(huì)放到文章的最后面
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í)有所幫助,也希望大家多多支持我們。
上一篇:Java PDF 添加數(shù)字簽名的實(shí)現(xiàn)方法
欄 目:Java
下一篇:SpringBoot路徑映射實(shí)現(xiàn)過程圖解
本文標(biāo)題:springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)
本文地址:http://mengdiqiu.com.cn/a1/Java/8857.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
- 01-10springboot 配置DRUID數(shù)據(jù)源的方法實(shí)例分析
- 01-10springboot2.0使用Hikari連接池的方法(替換druid)
- 01-10springboot單元測(cè)試兩種方法實(shí)例詳解


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置