場景:一家網(wǎng)上商城做商品限量秒殺。
將商品的數(shù)量存到Redis中。每個(gè)用戶搶購前都需要到Redis中查詢商品數(shù)量(代替mysql數(shù)據(jù)庫。不考慮事務(wù)),如果商品數(shù)量大于0,則證明商品有庫存。然后我們?cè)谶M(jìn)行庫存扣減和接下來的操作。因?yàn)槎嗑€程并發(fā)問題,我們不得不在get()方法內(nèi)部使用同步代碼塊。這樣可以保證查詢庫存和減庫存操作的原子性。
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisLock { @Autowired private RedisTemplateString, String> redisTemplate; @GetMapping(value = "buy") public String get() { synchronized (this) { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); }return ""; } } }
但是由于業(yè)務(wù)上升,并發(fā)數(shù)量變大。公司不得不將原有系統(tǒng)復(fù)制一份,放到新的服務(wù)器。然后使用nginx做負(fù)載均衡。為了模擬高并發(fā)環(huán)境這里使用了 Apache JMeter工具。
很明顯,現(xiàn)在的線程鎖不管用了。于是我們需要換一把鎖,這把鎖必須和兩套系統(tǒng)沒有任何的耦合度。
使用Redies的API如果key不存在,則設(shè)置一個(gè)key。這個(gè)key就是我們現(xiàn)在使用的一把鎖。每個(gè)線程到此處,先設(shè)置鎖,如果設(shè)置鎖失敗,則表明當(dāng)前有線程獲取到了鎖,就返回。最后我們?yōu)榱藴p庫存和其他業(yè)務(wù)拋出異常,而沒有釋放鎖。把釋放鎖的操作放到了finally代碼塊中。看起來是比較完美了。
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisLock { @Autowired private RedisTemplateString, String> redisTemplate; @GetMapping(value = "buy") public String get() { Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", ""); if (!phoneLock) { return ""; } try{ String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); } }finally { redisTemplate.delete("phoneLock"); } return ""; } }
如果try中拋出了異常,進(jìn)入finally,這把鎖依然會(huì)釋放,不會(huì)影響其他線程獲取鎖,那么如果在finally也拋出了異常,或者在finally中服務(wù)直接關(guān)閉了,那其他的服務(wù)再也獲取不到鎖。最終導(dǎo)致商品賣不出去。
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisLock { @Autowired private RedisTemplateString, String> redisTemplate; @GetMapping(value = "buy") public String get() { int i = 0; Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", ""); if (!phoneLock) { return ""; } try { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { i = count; redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); } } finally { if (i == 20) { System.exit(0); } redisTemplate.delete("phoneLock"); } return ""; } }
問題就出現(xiàn)在如果出現(xiàn)意外,這把鎖無法釋放。這里我們?cè)谝隦edis的API,對(duì)key進(jìn)行過期時(shí)間的設(shè)置。這樣如果拿到鎖的線程,在任何情況下沒有來得及釋放鎖,當(dāng)Redis的key時(shí)間到,也會(huì)自動(dòng)釋放鎖。但是這樣還是存在問題
如果在key過期后,鎖釋放了,但是當(dāng)前線程沒有執(zhí)行完畢。那么其他線程就會(huì)拿到鎖,繼續(xù)搶購商品,而這個(gè)較慢的線程則會(huì)在執(zhí)行完畢后,釋放別人的鎖。導(dǎo)致鎖失效!
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import javafx.concurrent.Task; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; @RestController public class RedisLock { @Autowired private RedisTemplateString, String> redisTemplate; @GetMapping(value = "buy") public String get() { Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "", 3, TimeUnit.SECONDS); if (!phoneLock) { return ""; } try { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { try { Thread.sleep(99999999999L); } catch (Exception e) { } redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); } } finally { redisTemplate.delete("phoneLock"); } return ""; } }
問題的出現(xiàn)就是,當(dāng)一條線程的key已經(jīng)過期,但是這個(gè)線程的任務(wù)確確實(shí)實(shí)沒有執(zhí)行完畢,這個(gè)交易沒有結(jié)束。但是鎖沒了?,F(xiàn)在我們必須對(duì)鎖的時(shí)間進(jìn)行延長。在判斷商品有庫存時(shí),第一時(shí)間創(chuàng)建一個(gè)線程不停的給key續(xù)命,
防止key過期。然后在交易結(jié)束后,停止定時(shí)器,釋放鎖。
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; @RestController public class RedisLock { @Autowired private RedisTemplateString, String> redisTemplate; @GetMapping(value = "buy") public String get() { Boolean phoneLock = redisTemplate.opsForValue().setIfAbsent("phoneLock", "", 3, TimeUnit.SECONDS); if (!phoneLock) { return ""; } Timer timer = null; try { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { redisTemplate.opsForValue().set("phoneLock", "", 3, TimeUnit.SECONDS); } }, 0, 1); redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); } } finally { if (timer != null) { timer.cancel(); } redisTemplate.delete("phoneLock"); } return ""; } }
在步驟5我們的代碼已經(jīng)很完善了,不會(huì)出現(xiàn)高并發(fā)問題。但是代碼確過于冗余,我們?yōu)榱耸褂肦edis鎖,我們需要設(shè)置一個(gè)定長的key,然后當(dāng)購買完成后,將key刪除。但為了防止key提前過期,我們不得不新增一個(gè)線程執(zhí)行定時(shí)任務(wù)。下面我們可以使用Redissson框架簡化代碼。getLock()方法代替了Redis的setIfAbsent(),lock()設(shè)置過期時(shí)間。最終我們?cè)诮灰捉Y(jié)束后釋放鎖。延長鎖的操作則有Redisson框架替我們完成,它會(huì)使用輪詢?nèi)ゲ榭磌ey是否過期,
在交易沒有完成時(shí),自動(dòng)重設(shè)Redis的key過期時(shí)間
package springbootdemo.demo.controller; /* * @auther 頂風(fēng)少年 * @mail dfsn19970313@foxmail.com * @date 2020-01-13 11:19 * @notify * @version 1.0 */ import org.redisson.Redisson; import org.redisson.api.RLock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; @RestController public class RedissonLock { @Autowired private RedisTemplateString, String> redisTemplate; @Autowired private Redisson redisson; @GetMapping(value = "buy2") public String get() { RLock phoneLock = redisson.getLock("phoneLock"); phoneLock.lock(3, TimeUnit.SECONDS); try { String phone = redisTemplate.opsForValue().get("phone"); Integer count = Integer.valueOf(phone); if (count > 0) { redisTemplate.opsForValue().set("phone", String.valueOf(count - 1)); System.out.println("搶到了" + count + "號(hào)商品"); } } finally { phoneLock.unlock(); } return ""; } }
到此這篇關(guān)于Redis鎖完美解決高并發(fā)秒殺問題的文章就介紹到這了,更多相關(guān)Redis鎖高并發(fā)秒殺內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:北京 臺(tái)州 果洛 吉安 朝陽 大慶 楊凌 江蘇
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis鎖完美解決高并發(fā)秒殺問題》,本文關(guān)鍵詞 Redis,鎖,完美,解決,高并發(fā),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。