12306架構(gòu)到底是不是國(guó)內(nèi)最牛逼的架構(gòu)
轉(zhuǎn)載自公眾號(hào)【碼農(nóng)突圍】
轉(zhuǎn)載自公眾號(hào)【碼農(nóng)突圍】
12306 搶票,極限并發(fā)帶來(lái)的思考
大型高并發(fā)系統(tǒng)架構(gòu)

負(fù)載均衡簡(jiǎn)介
輪詢
加權(quán)輪詢
IP?Hash?輪詢
Nginx 加權(quán)輪詢的演示
????upstream?load_rule?{
???????server?127.0.0.1:3001?weight=1;
???????server?127.0.0.1:3002?weight=2;
???????server?127.0.0.1:3003?weight=3;
???????server?127.0.0.1:3004?weight=4;
????}
????...
????server?{
????listen???????80;
????server_name??load_balance.com?www.load_balance.com;
????location?/?{
???????proxy_pass?http://load_rule;
????}
}
import?(
????"net/http"
????"os"
????"strings"
)
func?main()?{
????http.HandleFunc("/buy/ticket",?handleReq)
????http.ListenAndServe(":3001",?nil)
}
//處理請(qǐng)求函數(shù),根據(jù)請(qǐng)求將響應(yīng)結(jié)果信息寫入日志
func?handleReq(w?http.ResponseWriter,?r?*http.Request)?{
????failedMsg?:=??"handle?in?port:"
????writeLog(failedMsg,?"./stat.log")
}
//寫入日志
func?writeLog(msg?string,?logPath?string)?{
????fd,?_?:=?os.OpenFile(logPath,?os.O_RDWR|os.O_CREATE|os.O_APPEND,?0644)
????defer?fd.Close()
????content?:=?strings.Join([]string{msg,?"\r\n"},?"3001")
????buf?:=?[]byte(content)
????fd.Write(buf)
}
秒殺搶購(gòu)系統(tǒng)選型
下單減庫(kù)存

在極限并發(fā)情況下,任何一個(gè)內(nèi)存操作的細(xì)節(jié)都至關(guān)影響性能,尤其像創(chuàng)建訂單這種邏輯,一般都需要存儲(chǔ)到磁盤數(shù)據(jù)庫(kù)的,對(duì)數(shù)據(jù)庫(kù)的壓力是可想而知的。
如果用戶存在惡意下單的情況,只下單不支付這樣庫(kù)存就會(huì)變少,會(huì)少賣很多訂單,雖然服務(wù)端可以限制 IP 和用戶的購(gòu)買訂單數(shù)量,這也不算是一個(gè)好方法。
支付減庫(kù)存

預(yù)扣庫(kù)存

扣庫(kù)存的藝術(shù)




代碼演示
初始化工作
//localSpike包結(jié)構(gòu)體定義
package?localSpike
type?LocalSpike?struct?{
????LocalInStock?????int64
????LocalSalesVolume?int64
}
...
//remoteSpike對(duì)hash結(jié)構(gòu)的定義和redis連接池
package?remoteSpike
//遠(yuǎn)程訂單存儲(chǔ)健值
type?RemoteSpikeKeys?struct?{
????SpikeOrderHashKey?string????//redis中秒殺訂單hash結(jié)構(gòu)key
????TotalInventoryKey?string????//hash結(jié)構(gòu)中總訂單庫(kù)存key
????QuantityOfOrderKey?string???//hash結(jié)構(gòu)中已有訂單數(shù)量key
}
//初始化redis連接池
func?NewPool()?*redis.Pool?{
????return?&redis.Pool{
????????MaxIdle:???10000,
????????MaxActive:?12000,?//?max?number?of?connections
????????Dial:?func()?(redis.Conn,?error)?{
????????????c,?err?:=?redis.Dial("tcp",?":6379")
????????????if?err?!=?nil?{
????????????????panic(err.Error())
????????????}
????????????return?c,?err
????????},
????}
}
...
func?init()?{
????localSpike?=?localSpike2.LocalSpike{
????????LocalInStock:?????150,
????????LocalSalesVolume:?0,
????}
????remoteSpike?=?remoteSpike2.RemoteSpikeKeys{
????????SpikeOrderHashKey:??"ticket_hash_key",
????????TotalInventoryKey:??"ticket_total_nums",
????????QuantityOfOrderKey:?"ticket_sold_nums",
????}
????redisPool?=?remoteSpike2.NewPool()
????done?=?make(chan?int,?1)
????done?<-?1
}
本地扣庫(kù)存和統(tǒng)一扣庫(kù)存
//本地扣庫(kù)存,返回bool值
func?(spike?*LocalSpike)?LocalDeductionStock()?bool{
????spike.LocalSalesVolume?=?spike.LocalSalesVolume?+?1
????return?spike.LocalSalesVolume?}
......
const?LuaScript?=?`
????????local?ticket_key?=?KEYS[1]
????????local?ticket_total_key?=?ARGV[1]
????????local?ticket_sold_key?=?ARGV[2]
????????local?ticket_total_nums?=?tonumber(redis.call('HGET',?ticket_key,?ticket_total_key))
????????local?ticket_sold_nums?=?tonumber(redis.call('HGET',?ticket_key,?ticket_sold_key))
????????--?查看是否還有余票,增加訂單數(shù)量,返回結(jié)果值
???????if(ticket_total_nums?>=?ticket_sold_nums)?then
????????????return?redis.call('HINCRBY',?ticket_key,?ticket_sold_key,?1)
????????end
????????return?0
`
//遠(yuǎn)端統(tǒng)一扣庫(kù)存
func?(RemoteSpikeKeys?*RemoteSpikeKeys)?RemoteDeductionStock(conn?redis.Conn)?bool?{
????lua?:=?redis.NewScript(1,?LuaScript)
????result,?err?:=?redis.Int(lua.Do(conn,?RemoteSpikeKeys.SpikeOrderHashKey,?RemoteSpikeKeys.TotalInventoryKey,?RemoteSpikeKeys.QuantityOfOrderKey))
????if?err?!=?nil?{
????????return?false
????}
????return?result?!=?0
}
響應(yīng)用戶信息
...
func?main()?{
????http.HandleFunc("/buy/ticket",?handleReq)
????http.ListenAndServe(":3005",?nil)
}
//處理請(qǐng)求函數(shù),根據(jù)請(qǐng)求將響應(yīng)結(jié)果信息寫入日志
func?handleReq(w?http.ResponseWriter,?r?*http.Request)?{
????redisConn?:=?redisPool.Get()
????LogMsg?:=?""
????<-done
????//全局讀寫鎖
????if?localSpike.LocalDeductionStock()?&&?remoteSpike.RemoteDeductionStock(redisConn)?{
????????util.RespJson(w,?1,??"搶票成功",?nil)
????????LogMsg?=?LogMsg?+?"result:1,localSales:"?+?strconv.FormatInt(localSpike.LocalSalesVolume,?10)
????}?else?{
????????util.RespJson(w,?-1,?"已售罄",?nil)
????????LogMsg?=?LogMsg?+?"result:0,localSales:"?+?strconv.FormatInt(localSpike.LocalSalesVolume,?10)
????}
????done?<-?1
????//將搶票狀態(tài)寫入到log中
????writeLog(LogMsg,?"./stat.log")
}
func?writeLog(msg?string,?logPath?string)?{
????fd,?_?:=?os.OpenFile(logPath,?os.O_RDWR|os.O_CREATE|os.O_APPEND,?0644)
????defer?fd.Close()
????content?:=?strings.Join([]string{msg,?"\r\n"},?"")
????buf?:=?[]byte(content)
????fd.Write(buf)
}
單機(jī)服務(wù)壓測(cè)
Copyright?1996?Adam?Twiss,?Zeus?Technology?Ltd,?http://www.zeustech.net/
Licensed?to?The?Apache?Software?Foundation,?http://www.apache.org/
Benchmarking?127.0.0.1?(be?patient)
Completed?1000?requests
Completed?2000?requests
Completed?3000?requests
Completed?4000?requests
Completed?5000?requests
Completed?6000?requests
Completed?7000?requests
Completed?8000?requests
Completed?9000?requests
Completed?10000?requests
Finished?10000?requests
Server?Software:
Server?Hostname:????????127.0.0.1
Server?Port:????????????3005
Document?Path:??????????/buy/ticket
Document?Length:????????29?bytes
Concurrency?Level:??????100
Time?taken?for?tests:???2.339?seconds
Complete?requests:??????10000
Failed?requests:????????0
Total?transferred:??????1370000?bytes
HTML?transferred:???????290000?bytes
Requests?per?second:????4275.96?[#/sec]?(mean)
Time?per?request:???????23.387?[ms]?(mean)
Time?per?request:???????0.234?[ms]?(mean,?across?all?concurrent?requests)
Transfer?rate:??????????572.08?[Kbytes/sec]?received
Connection?Times?(ms)
??????????????min??mean[+/-sd]?median???max
Connect:????????0????8??14.7??????6?????223
Processing:?????2???15??17.6?????11?????232
Waiting:????????1???11??13.5??????8?????225
Total:??????????7???23??22.8?????18?????239
Percentage?of?the?requests?served?within?a?certain?time?(ms)
??50%?????18
??66%?????24
??75%?????26
??80%?????28
??90%?????33
??95%?????39
??98%?????45
??99%?????54
?100%????239?(longest?request)
...
result:1,localSales:145
result:1,localSales:146
result:1,localSales:147
result:1,localSales:148
result:1,localSales:149
result:1,localSales:150
result:0,localSales:151
result:0,localSales:152
result:0,localSales:153
result:0,localSales:154
result:0,localSales:156
...
總結(jié)回顧
評(píng)論
圖片
表情
