springboot中怎么利用redis實(shí)現(xiàn)一個(gè)秒殺系統(tǒng)

這篇文章將為大家詳細(xì)講解有關(guān)springboot中怎么利用redis實(shí)現(xiàn)一個(gè)秒殺系統(tǒng),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)是一家專業(yè)提供故城企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為故城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

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;@Servicepublic 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ù)秒殺測試接口", notes = "用事務(wù)秒殺測試接口")@RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET)@ResponseBody public Long miaoTransaction() {  Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);  return res; } @ApiOperation(value = " 秒殺Lua測試接口", notes = "秒殺Lua測試接口") @RequestMapping(value = "/miaoLua", method = RequestMethod.GET) @ResponseBody public Long miaoLua() {  Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);  System.out.println(res.toString());  return res; }

關(guān)于springboot中怎么利用redis實(shí)現(xiàn)一個(gè)秒殺系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章題目:springboot中怎么利用redis實(shí)現(xiàn)一個(gè)秒殺系統(tǒng)
本文鏈接:http://bm7419.com/article44/jdcehe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、微信小程序、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站建設(shè)用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站