相關(guān)關(guān)鍵詞
關(guān)于我們
最新文章
- PHP中opcode緩存簡單用法分析
- thinkPHP控制器變量在模板中的顯示方法示例
- PHP move_uploaded_file() 函數(shù)(將上傳的文件移動(dòng)到新位置)
- dirname(__FILE__)的含義和應(yīng)用說明
- thinkPHP5框架實(shí)現(xiàn)分頁查詢功能的方法示例
- PHP中單雙號(hào)與變量
- PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
- Laravel ORM對(duì)Model::find方法進(jìn)行緩存示例詳解
- PHP讀寫文件高并發(fā)處理操作實(shí)例詳解
- 【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)
php redis實(shí)現(xiàn)對(duì)200w用戶的即時(shí)推送

怎么實(shí)現(xiàn)對(duì)200w用戶的即時(shí)推送,這個(gè)推送可以理解為調(diào)用第三方的接口,push,sms之類的東西。
當(dāng)時(shí)先寫了一個(gè)demo 直接讀取DB然后單個(gè)推送,結(jié)果。??上攵?br />
于是設(shè)計(jì)一套基于redis+php多進(jìn)程的方案,用著還不錯(cuò)而去擴(kuò)展性蠻高的,故分享之。
=============================================
具體的邏輯如下:(無視我的字體)
其實(shí)這里還可以優(yōu)化的,我的設(shè)想是如果用戶數(shù)據(jù)再多一些的話,可以在redis里對(duì)數(shù)據(jù)進(jìn)行分割采取多List,每一個(gè)List對(duì)應(yīng)多個(gè)php進(jìn)程這樣會(huì)更快。
下面是我實(shí)現(xiàn)的具體代碼:
主管理腳本:應(yīng)用時(shí)啟動(dòng)這個(gè)即可。
<?php //push推送配置 注:使用前請(qǐng)確認(rèn)log文件為空 2016-04-12 include_once(dirname (__FILE__)."/../../config.inc.php"); //if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check; import('push.class.php'); import('Redis.class.php'); $time =time(); $data = array("apikey"=>'xxxx',"secret"=>'xxxx'); $push = new Channel($data); $redis = new RedisCache($Credis['host'],$Credis['port']); if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check;//如果有推送任務(wù) 直接執(zhí)行監(jiān)控代碼 /*PUSH配置項(xiàng)*/ $config = array( "file"=>"test.txt", "Title"=>"sssss", "Content"=>"ssssssssssssssss", "OpenType"=>"0", //1是 0否 是否跳轉(zhuǎn)鏈接 "Url"=>"", //鏈接地址 "num"=>"500", //每次推送條數(shù) "s"=>"1" //睡眠時(shí)間 (單位:秒) ); $num = 15; //啟動(dòng)進(jìn)程數(shù)量 $a = $config['OpenType']==1 ? "是" : "否"; $c = json_encode($config); $info = <<<monkey ************ 請(qǐng)確認(rèn)信息是否有誤*10秒后啟動(dòng)push任務(wù)! ************* * 文件名稱 : {$config['file']}; * 推送標(biāo)題 : {$config['Title']}; * 推送內(nèi)容 : {$config['Content']}; * 是否跳轉(zhuǎn) : {$config['OpenType']}; * 進(jìn)程數(shù)量 : $num;(如果為單進(jìn)程無視此項(xiàng)) * 睡眠時(shí)間 : {$config['s']}; * 日志目錄 : /log; ***************************************************************\n monkey; echo $info; sleep(3); $n = 1; while($n<=10){ echo (10-$n++),"秒\n"; sleep(1); } echo "------------------------- 任務(wù)已啟動(dòng) -------------------------\n"; if($redis->Scount('push_getchannel_success')){ echo "隊(duì)列有未完成任務(wù)\n"; }else{ $res = exec("php redis_getchannel.php {$config['file']}");//寫入redis腳本 echo $res; } smtp_mail('xxxx@qq.com','推送任務(wù)已開啟','請(qǐng)實(shí)時(shí)監(jiān)測(cè),5秒后您的手機(jī)將接收到測(cè)試推送!');//推送監(jiān)控 實(shí)現(xiàn)定時(shí)全自動(dòng)推送 echo "\n---------------- 5秒后 test 將收到測(cè)試推送消息 ----------------\n"; sleep(5); $re = $push->BaiduPush('xxxx','xxxxx',$config['Content'],$config['Title'],'1',$config['OpenType'],$config['Url'],'xxxxx',$push); sleep(1); echo "\n---------------- 測(cè)試推送已發(fā)出!如未收到,請(qǐng)及時(shí)終止程序! 10秒后正式推送!!! ----------------\n"; $m = 1; while($m<=10){ echo (10-$m++),"秒\n"; sleep(1); } echo "\n---------------- 推送任務(wù)已經(jīng)開始!請(qǐng)耐心等待! ----------------\n"; //下面設(shè)置是否多進(jìn)程 for($i=1;$i<=$num;$i++){ exec("php redis_push.php '{$c}' > /dev/null 2>&1 &"); } check: while(1){ if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') == 0){ echo "push 發(fā)送完成 用時(shí)",time()-$time,"秒"; die(); } echo "當(dāng)前進(jìn)程數(shù):",exec('ps aux | grep redis_push.php | grep -v grep | wc -l'),"個(gè)","\n"; echo "當(dāng)前剩余推送數(shù)量:".$redis->Scount('push_getchannel_success')."\n"; sleep(10); }