PHP并發(fā)場(chǎng)景的三種解決方案代碼實(shí)例
在秒殺,搶購等并發(fā)場(chǎng)景下,可能會(huì)出現(xiàn)超賣的現(xiàn)象,在 PHP 語言中并沒有原生提供并發(fā)的解決方案,因此就需要借助其他方式來實(shí)現(xiàn)并發(fā)控制,其實(shí)方案有很多種,今天只是舉個(gè)栗子拋磚引玉,有其他更好的方案你可以自己去玩一玩就好了。
列出常見的3個(gè)解決方案有:
使用隊(duì)列,額外起一個(gè)進(jìn)程處理隊(duì)列,并發(fā)請(qǐng)求都放到隊(duì)列中,由額外進(jìn)程串行處理,并發(fā)問題就不存在了,但是要額外進(jìn)程支持以及處理延遲嚴(yán)重,本文不先不討論這種方法。
利用數(shù)據(jù)庫事務(wù)特征,做原子更新,此方法需要依賴數(shù)據(jù)庫的事務(wù)特性。
借助文件排他鎖,在處理下單請(qǐng)求的時(shí)候,用 flock 鎖定一個(gè)文件,成功拿到鎖的才能處理訂單。
一、利用 Redis 事務(wù)特征
redis 事務(wù)是原子操作,可以保證訂單處理的過程中數(shù)據(jù)沒有被其它并發(fā)的進(jìn)程修改。
示例代碼:
<?php $http = new swoole_http_server("0.0.0.0", 9509); // 監(jiān)聽 9509 $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); // 模擬唯一用戶ID $redis = new Redis(); $redis->connect('127.0.0.1', 6379); // 連接 redis $redis->watch('rest_count'); // 監(jiān)測(cè) rest_count 是否被其它的進(jìn)程更改 $rest_count = intval($redis->get("rest_count")); // 模擬唯一訂單ID if ($rest_count > 0){ $value = "{$rest_count}-{$uniqid}"; // 表示當(dāng)前訂單,被當(dāng)前用戶搶到了 // do something ... 主要是模擬用戶搶到單后可能要進(jìn)行的一些密集運(yùn)算 $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} // redis 事務(wù) $redis->multi(); $redis->lPush('uniqids', $value); $redis->decr('rest_count'); $replies = $redis->exec(); // 執(zhí)行以上 redis 事務(wù) // 如果 rest_count 的值被其它的并發(fā)進(jìn)程更改了,以上事務(wù)將回滾 if (!$replies) { echo "訂單 {$value} 回滾" . PHP_EOL; } } $redis->unwatch(); }); $http->start();
使用 ab 測(cè)試
$ ab -t 20 -c 10 http://192.168.1.104:9509/
二、利用文件排他鎖 (阻塞模式)
阻塞模式下,如果進(jìn)程在獲取文件排他鎖時(shí),其它進(jìn)程正在占用鎖的話,此進(jìn)程會(huì)掛起等待其它進(jìn)程釋放鎖后,并自己獲取到鎖后,再往下執(zhí)行。
示例代碼:
<?php $http = new swoole_http_server("0.0.0.0", 9510); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // 阻塞(等待)模式, 要取得獨(dú)占鎖定(寫入的程序) if (flock($fp,LOCK_EX)) { //鎖定當(dāng)前指針 // 成功取得鎖后,放心處理訂單 $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if ($rest_count > 0) { // do something ... $rand = rand(100, 1000000); $sum = 0; for ($i = 0; $i < $rand; $i++) {$sum += $i;} $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // 訂單處理完成后,再釋放鎖 flock($fp, LOCK_UN); } fclose($fp); }); $http->start();
使用 ab 測(cè)試
$ ab -t 20 -c 10 http://192.168.1.104:9510/
三、利用文件排他鎖 (非阻塞模式)
非阻塞模式下,如果進(jìn)程在獲取文件排他鎖時(shí),其它進(jìn)程正在占用鎖的話,此進(jìn)程會(huì)馬上判斷獲取鎖失敗,并且繼續(xù)往下執(zhí)行。\
示例代碼:
<?php $http = new swoole_http_server("0.0.0.0", 9511); $http->set(array( 'reactor_num' => 2, //reactor thread num 'worker_num' => 4 //worker process num )); $http->on('request', function (swoole_http_request $request, swoole_http_response $response) { $uniqid = uniqid('uid-', TRUE); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $fp = fopen("lock.txt", "w+"); // 非阻塞模式, 如果不希望 flock() 在鎖定時(shí)堵塞,則給 lock 加上 LOCK_NB if(flock($fp,LOCK_EX | LOCK_NB)) //鎖定當(dāng)前指針 { // 成功取得鎖后,放心處理訂單 $rest_count = intval($redis->get("rest_count")); $value = "{$rest_count}-{$uniqid}"; if($rest_count > 0){ // do something ... $rand = rand(100, 1000000); $sum=0; for ($i=0;$i<$rand;$i++){ $sum+=$i; } $redis->lPush('uniqids', $value); $redis->decr('rest_count'); } // 訂單處理完成后,再釋放鎖 flock($fp,LOCK_UN); } else { // 如果獲取鎖失敗,馬上進(jìn)入這里執(zhí)行 echo "{$uniqid} - 系統(tǒng)繁忙,請(qǐng)稍后再試".PHP_EOL; } fclose($fp); }); $http->start();
使用 ab 測(cè)試
$ ab -t 20 -c 10 http://192.168.1.104:9511/
最后給出三種處理方式的測(cè)試結(jié)果比較
redis 事務(wù)方式:
Concurrency Level: 10 Time taken for tests: 20.005 seconds Complete requests: 17537 Failed requests: 0 Total transferred: 2578380 bytes HTML transferred: 0 bytes Requests per second: 876.62 [#/sec] (mean) Time per request: 11.407 [ms] (mean) Time per request: 1.141 [ms] (mean, across all concurrent requests) Transfer rate: 125.86 [Kbytes/sec] received
文件排他鎖(阻塞模式):
Concurrency Level: 10 Time taken for tests: 20.003 seconds Complete requests: 8205 Failed requests: 0 Total transferred: 1206282 bytes HTML transferred: 0 bytes Requests per second: 410.19 [#/sec] (mean) Time per request: 24.379 [ms] (mean) Time per request: 2.438 [ms] (mean, across all concurrent requests) Transfer rate: 58.89 [Kbytes/sec] received
文件排他鎖(非阻塞模式):
Concurrency Level: 10 Time taken for tests: 20.002 seconds Complete requests: 8616 Failed requests: 0 Total transferred: 1266846 bytes HTML transferred: 0 bytes Requests per second: 430.77 [#/sec] (mean) Time per request: 23.214 [ms] (mean) Time per request: 2.321 [ms] (mean, across all concurrent requests) Transfer rate: 61.85 [Kbytes/sec] received
經(jīng)測(cè)試結(jié)果對(duì)比,redis 事務(wù)方式優(yōu)于文件排他鎖方式,而文件排他鎖方式中,非阻塞模式優(yōu)于阻塞模式。
到此這篇關(guān)于PHP并發(fā)場(chǎng)景的三種解決方案代碼實(shí)例的文章就介紹到這了,更多相關(guān)PHP并發(fā)場(chǎng)景的三種解決方案內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP實(shí)現(xiàn)根據(jù)密碼長(zhǎng)度顯示安全條
本文通過實(shí)例代碼給大家分享php根據(jù)密碼長(zhǎng)度顯示安全條功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié)
這篇文章主要介紹了PHP抓取網(wǎng)頁、解析HTML常用的方法總結(jié),本文只是對(duì)可以實(shí)現(xiàn)這兩個(gè)需求的方法作了總結(jié),只介紹方法,不介紹如何實(shí)現(xiàn),需要的朋友可以參考下2015-07-07PHP各種異常和錯(cuò)誤的攔截方法及發(fā)生致命錯(cuò)誤時(shí)進(jìn)行報(bào)警
在日常開發(fā)中,大多數(shù)人的做法是在開發(fā)環(huán)境時(shí)開啟調(diào)試模式,在產(chǎn)品環(huán)境關(guān)閉調(diào)試模式。在開發(fā)的時(shí)候可以查看各種錯(cuò)誤、異常,但是在線上就把錯(cuò)誤顯示的關(guān)閉2016-01-01php安裝xdebug/php安裝pear/phpunit詳解步驟(圖)
PHP環(huán)境中安裝pear、phpunit以及xdebug全攻略,大家參考使用吧2013-12-12Thinkphp 框架擴(kuò)展之Widget擴(kuò)展實(shí)現(xiàn)方法分析
這篇文章主要介紹了Thinkphp 框架擴(kuò)展之Widget擴(kuò)展實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Thinkphp 框架Widget擴(kuò)展概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04Laravel框架執(zhí)行原生SQL語句及使用paginate分頁的方法
這篇文章主要介紹了Laravel框架執(zhí)行原生SQL語句及使用paginate分頁的方法,以類函數(shù)的形式給出了Laravel框架執(zhí)行原生SQL語句以及paginate分頁的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08