實現一個秒殺系統,采用spring boot 2.x + mybatis+ redis + swagger2 + lombok實現。
先說說基本流程,就是提供一個秒殺接口,然后針對秒殺接口進行限流,限流的方式目前我實現了兩種,上次實現的是累計計數方式,這次還有這個功能,并且我增加了令牌桶方式的lua腳本進行限流。
然后不被限流的數據進來之后,加一把分布式鎖,獲取分布式鎖之后就可以對數據庫進行操作了。直接操作數據庫的方式可以,但是速度會比較慢,咱們直接通過一個初始化接口,將庫存數據放到緩存中,然后對緩存中的數據進行操作。
寫庫的操作采用異步方式,實現的方式就是將操作好的數據放入到隊列中,然后由另一個線程對隊列進行消費。當然,也可以將數據直接寫入mq中,由另一個線程進行消費,這樣也更穩妥。
好了,看一下項目的基本結構:
看一下入口controller類,入口類有兩個方法,一個是初始化訂單的方法,即秒殺開始的時候,秒殺接口才會有效,這個方法可以采用定時任務自動實現也可以。
初始化后就可以調用placeOrder的方法了。在placeOrder上面有個自定義的注解DistriLimitAnno,這個是我在上篇文章寫的,用作限流使用。
采用的方式目前有兩種,一種是使用計數方式限流,一種方式是令牌桶,上次使用了計數,咱們這次采用令牌桶方式實現。
package com.hqs.flashsales.controller;
import com.hqs.flashsales.annotation.DistriLimitAnno;import com.hqs.flashsales.aspect.LimitAspect;import com.hqs.flashsales.lock.DistributedLock;import com.hqs.flashsales.limit.DistributedLimit;import com.hqs.flashsales.service.OrderService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.RedisScript;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;import java.util.Collections;
/** * @author huangqingshi * @Date 2019-01-23 */@Slf4j@Controllerpublic class FlashSaleController {
@Autowired OrderService orderService; @Autowired DistributedLock distributedLock; @Autowired LimitAspect limitAspect; //注意RedisTemplate用的String,String,后續所有用到的key和value都是String的 @Autowired RedisTemplate
private static final String LOCK_PRE = "LOCK_ORDER";
@PostMapping("/initCatalog") @ResponseBody public String initCatalog() { try { orderService.initCatalog(); } catch (Exception e) { log.error("error", e); }
return "init is ok"; }
@PostMapping("/placeOrder") @ResponseBody @DistriLimitAnno(limitKey = "limit", limit = 100, seconds = "1") public Long placeOrder(Long orderId) { Long saleOrderId = 0L; boolean locked = false; String key = LOCK_PRE + orderId; String uuid = String.valueOf(orderId); try { locked = distributedLock.distributedLock(key, uuid, "10" ); if(locked) { //直接操作數據庫// saleOrderId = orderService.placeOrder(orderId); //操作緩存 異步操作數據庫 saleOrderId = orderService.placeOrderWithQueue(orderId); } log.info("saleOrderId:{}", saleOrderId); } catch (Exception e) { log.error(e.getMessage()); } finally { if(locked) { distributedLock.distributedUnlock(key, uuid); } } return saleOrderId; }
}
令牌桶的方式比直接計數更加平滑,直接計數可能會瞬間達到最高值,令牌桶則把最高峰給削掉了,令牌桶的基本原理就是有一個桶裝著令牌,然后又一隊人排隊領取令牌,領到令牌的人就可以去做做自己想做的事情了,沒有領到令牌的人直接就走了(也可以重新排隊)。
發令牌是按照一定的速度發放的,所以這樣在多人等令牌的時候,很多人是拿不到的。當桶里邊的令牌在一定時間內領完后,則沒有令牌可領,都直接走了。如果過了一定的時間之后可以再次把令牌桶裝滿供排隊的人領。
基本原理是這樣的,看一下腳本簡單了解一下,里邊有一個key和四個參數,第一個參數是獲取一個令牌桶的時間間隔,第二個參數是重新填裝令牌的時間(精確到毫秒),第三個是令牌桶的數量限制,第四個是隔多長時間重新填裝令牌桶。
-- bucket namelocal key = KEYS[1]-- token generate intervallocal intervalPerPermit = tonumber(ARGV[1])-- grant timestamplocal refillTime = tonumber(ARGV[2])-- limit token countlocal limit = tonumber(ARGV[3])-- ratelimit time periodlocal interval = tonumber(ARGV[4])
local counter = redis.call('hgetall', key)
if table.getn(counter) == 0 then -- first check if bucket not exists, if yes, create a new one with full capacity, then grant access redis.call('hmset', key, 'lastRefillTime', refillTime, 'tokensRemaining', limit - 1) -- expire will save memory redis.call('expire', key, interval) return 1elseif table.getn(counter) == 4 then -- if bucket exists, first we try to refill the token bucket local lastRefillTime, tokensRemaining = tonumber(counter[2]), tonumber(counter[4]) local currentTokens if refillTime > lastRefillTime then -- check if refillTime larger than lastRefillTime. -- if not, it means some other operation later than this call made the call first. -- there is no need to refill the tokens. local intervalSinceLast = refillTime - lastRefillTime if intervalSinceLast > interval then currentTokens = limit redis.call('hset', key, 'lastRefillTime', refillTime) else local grantedTokens = math.floor(intervalSinceLast / intervalPerPermit) if grantedTokens > 0 then -- ajust lastRefillTime, we want shift left the refill time. local padMillis = math.fmod(intervalSinceLast, intervalPerPermit) redis.call('hset', key, 'lastRefillTime', refillTime - padMillis) end currentTokens = math.min(grantedTokens + tokensRemaining, limit) end else -- if not, it means some other operation later than this call made the call first. -- there is no need to refill the tokens. currentTokens = tokensRemaining end
assert(currentTokens >= 0)
if currentTokens == 0 then -- we didn't consume any keys redis.call('hset', key, 'tokensRemaining', currentTokens) return 0 else -- we take 1 token from the bucket redis.call('hset', key, 'tokensRemaining', currentTokens - 1) return 1 endelse error("Size of counter is " .. table.getn(counter) .. ", Should Be 0 or 4.")end
看一下調用令牌桶lua的JAVA代碼,也比較簡單:
public Boolean distributedRateLimit(String key, String limit, String seconds) { Long id = 0L; long intervalInMills = Long.valueOf(seconds) * 1000; long limitInLong = Long.valueOf(limit); long intervalPerPermit = intervalInMills / limitInLong;// Long refillTime = System.currentTimeMillis();// log.info("調用redis執行lua腳本, {} {} {} {} {}", "ratelimit", intervalPerPermit, refillTime,// limit, intervalInMills); try { id = redisTemplate.execute(rateLimitScript, Collections.singletonList(key), String.valueOf(intervalPerPermit), String.valueOf(System.currentTimeMillis()), String.valueOf(limitInLong), String.valueOf(intervalInMills)); } catch (Exception e) { log.error("error", e); }
if(id == 0L) { return false; } else { return true; } }
創建兩張簡單表,一個庫存表,一個是銷售訂單表:
CREATE TABLE `catalog` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名稱', `total` int(11) NOT NULL COMMENT '庫存', `sold` int(11) NOT NULL COMMENT '已售', `version` int(11) NULL COMMENT '樂觀鎖,版本號', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `sales_order` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `cid` int(11) NOT NULL COMMENT '庫存ID', `name` varchar(30) NOT NULL DEFAULT '' COMMENT '商品名稱', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '創建時間', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
基本已經準備完畢,然后啟動程序,打開swagger(http://localhost:8080/swagger-ui.html#),執行初始化方法initCatalog:
日志里邊會輸出初始化的記錄內容,初始化庫存為1000:
初始化執行的方法,十分簡單,寫到緩存中。
@Override public void initCatalog() { Catalog catalog = new Catalog(); catalog.setName("mac"); catalog.setTotal(1000L); catalog.setSold(0L); catalogMapper.insertCatalog(catalog); log.info("catalog:{}", catalog); redisTemplate.opsForValue().set(CATALOG_TOTAL + catalog.getId(), catalog.getTotal().toString()); redisTemplate.opsForValue().set(CATALOG_SOLD + catalog.getId(), catalog.getSold().toString()); log.info("redis value:{}", redisTemplate.opsForValue().get(CATALOG_TOTAL + catalog.getId())); handleCatalog(); }
我寫了一個測試類,啟動3000個線程,然后去進行下單請求:
package com.hqs.flashsales;
import lombok.extern.slf4j.Slf4j;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.web.client.TestRestTemplate;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;
import java.util.concurrent.TimeUnit;
@Slf4j@RunWith(SpringRunner.class)@SpringBootTest(classes = FlashsalesApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class FlashSalesApplicationTests {
@Autowired private TestRestTemplate testRestTemplate;
@Test public void flashsaleTest() { String url = "http://localhost:8080/placeOrder"; for(int i = 0; i < 3000; i++) { ? ? ? ? ? ?try { ? ? ? ? ? ? ? ?TimeUnit.MILLISECONDS.sleep(20); ? ? ? ? ? ? ? ?new Thread(() -> { MultiValueMap
} }
@Test public void contextLoads() { }
}
然后開始運行測試代碼,查看一下測試日志和程序日志,均顯示賣了1000后直接顯示SOLD OUT了。分別看一下日志和數據庫:
商品庫存catalog表和訂單明細表sales_order表,都是1000條,沒有問題。
總結:
通過采用分布式鎖和分布式限流,即可實現秒殺流程,當然分布式限流也可以用到很多地方,比如限制某些IP在多久時間訪問接口多少次,都可以的。
令牌桶的限流方式使得請求可以得到更加平滑的處理,不至于瞬間把系統達到最高負載。在這其中其實還有一個小細節,就是Redis的鎖,單機情況下沒有任何問題,如果是集群的話需要注意,一個key被hash到同一個slot的時候沒有問題,如果說擴容或者縮容的話,如果key被hash到不同的slot,程序可能會出問題。
在寫代碼的過程中還出現了一個小問題,就是寫controller的方法的時候,方法一定要聲明成public的,否則自定義的注解用不了,其他service的注解直接變為空,這個問題也是找了很久才找到。
-
接口
+關注
關注
33文章
8712瀏覽量
152013 -
數據庫
+關注
關注
7文章
3852瀏覽量
64724 -
腳本
+關注
關注
1文章
392瀏覽量
14957
原文標題:用 IDEA 基于SpringBoot2+ mybatis+Redis實現一個秒殺系統(附上源碼)
文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
何氏手機維修秒殺絕殺技術----笫一集漏電故障的秒殺技術
秒殺到了個是德 U1242C
【1元秒殺】萬用表1元購,包郵!數量有限,先到先得~
如何去實現一種基于SpringMVC的電商高并發秒殺系統設計
一個嵌入式視頻監控系統的設計與實現
秒殺神券雙組合 vivo 618狂歡眾多炸裂福利勁爆來襲
一篇文章秒殺三道區間相關的問題
![<b class='flag-5'>一</b>篇文章<b class='flag-5'>秒殺</b>三道區間相關的問題](https://file.elecfans.com/web1/M00/CA/14/pIYBAF-D_JaAJwBnAACe9061zU8479.png)
如何實現一個微內核操作系統的設計
![如何<b class='flag-5'>實現</b><b class='flag-5'>一</b><b class='flag-5'>個</b>微內核操作<b class='flag-5'>系統</b>的設計](https://file.elecfans.com/web1/M00/CF/60/o4YBAF-uU9aAAO4hAABZedZRdhc235.png)
【源碼版】基于SpringMVC的電商高并發秒殺系統設計思路
![【源碼版】基于SpringMVC的電商高并發<b class='flag-5'>秒殺</b><b class='flag-5'>系統</b>設計思路](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
用Zookeeper怎么實現一個分布式鎖?
![用Zookeeper怎么<b class='flag-5'>實現</b><b class='flag-5'>一</b><b class='flag-5'>個</b>分布式鎖?](https://file1.elecfans.com//web2/M00/82/A9/wKgaomRcWrSAJlt8AAJAnxdVABA810.jpg)
如何控制秒殺商品頁面購買按鈕的點亮
![如何控制<b class='flag-5'>秒殺</b>商品頁面購買按鈕的點亮](https://file1.elecfans.com/web2/M00/8B/AA/wKgZomSc9o2ALsZ_AAAO9JE_36g414.jpg)
評論