? ? ? ? 需求背景:有個調(diào)用統(tǒng)計日志存儲和統(tǒng)計需求热康,要求存儲到mysql中朝捆;存儲數(shù)據(jù)高峰能達到日均千萬废亭,瓶頸在于直接入庫并發(fā)太高国章,可能會把mysql干垮。
問題分析
思考:應用網(wǎng)站架構(gòu)的衍化過程中豆村,應用最新的框架和工具技術(shù)固然是最優(yōu)選擇;但是骂删,如果能在現(xiàn)有的框架的基礎(chǔ)上提出簡單可依賴的解決方案掌动,未嘗不是一種提升自我的嘗試。
解決:
問題一:要求日志最好入庫宁玫;但是粗恢,直接入庫mysql確實扛不住,批量入庫沒有問題欧瘪,done眷射。【批量入庫和直接入庫性能差異參考文章】
問題二:批量入庫就需要有高并發(fā)的消息隊列,決定采用redis list 仿真實現(xiàn)妖碉,而且方便回滾涌庭。
問題三:日志量畢竟大,保存最近30條足矣欧宜,決定用php寫個離線統(tǒng)計和清理腳本坐榆。
done,下面是小拽的簡單實現(xiàn)過程
一:設(shè)計數(shù)據(jù)庫表和存儲
考慮到log系統(tǒng)對數(shù)據(jù)庫的性能更多一些冗茸,穩(wěn)定性和安全性沒有那么高席镀,存儲引擎自然是只支持select insert 沒有索引的archive。如果確實有update需求夏漱,也可以采用myISAM豪诲。
考慮到log是實時記錄的所有數(shù)據(jù),數(shù)量可能巨大挂绰,主鍵采用bigint跛溉,自增即可。
考慮到log系統(tǒng)以寫為主扮授,統(tǒng)計采用離線計算芳室,字段均不要出現(xiàn)索引,因為一方面可能會影響插入數(shù)據(jù)效率刹勃,另外讀時候會造成死鎖堪侯,影響寫數(shù)據(jù)。
二:redis存儲數(shù)據(jù)形成消息隊列
由于高并發(fā)荔仁,盡可能簡單伍宦,直接,上代碼乏梁。
*
* 獲取到的調(diào)用日志次洼,存入redis的隊列中.
* $Id$
*
**************************************************************************//***@filesaveLog.php*@date2015/11/06 20:47:13*@author:cuihuan*@version$Revision$*@brief***/// 獲取info$interface_info=$_GET['info'];// 存入redis隊列$redis=newRedis();$redis->connect('xx',6379);$redis->auth("password");// 加上時間戳存入隊列$now_time= date("Y-m-d H:i:s");$redis->rPush("call_log",$interface_info."%".$now_time);$redis->close();/* vim: set ts=4 sw=4 sts=4 tw=100 */?>
三:數(shù)據(jù)定時批量入庫。
定時讀取redis消息隊列里面的數(shù)據(jù)遇骑,批量入庫卖毁。
connect('ip', port);$redis_xx->auth("password");// 獲取現(xiàn)有消息隊列的長度$count=0;$max=$redis_xx->lLen("call_log");// 獲取消息隊列的內(nèi)容,拼接sql$insert_sql="insert into fb_call_log (`interface_name`, `createtime`) values ";// 回滾數(shù)組$roll_back_arr=array();while($count<$max) {$log_info=$redis_cq01->lPop("call_log");$roll_back_arr=$log_info;if($log_info=='nil'|| !isset($log_info)) {$insert_sql.=";";break;? ? }// 切割出時間和info$log_info_arr= explode("%",$log_info);$insert_sql.=" ('".$log_info_arr[0]."','".$log_info_arr[1]."'),";$count++;}// 判定存在數(shù)據(jù)落萎,批量入庫if($count!=0) {$link_2004= mysql_connect('ip:port','user','password');if(!$link_2004) {die("Could not connect:". mysql_error());? ? }$crowd_db= mysql_select_db('fb_log',$link_2004);$insert_sql= rtrim($insert_sql,",").";";$res= mysql_query($insert_sql);// 輸出入庫log和入庫結(jié)果;echodate("Y-m-d H:i:s")."insert ".$count." log info result:";echojson_encode($res);echo"
\n";// 數(shù)據(jù)庫插入失敗回滾if(!$res){foreach($roll_back_arras$k){$redis_xx->rPush("call_log",$k);? ? ? }? ? }// 釋放連接mysql_free_result($res);? ? mysql_close($link_2004);}// 釋放redis$redis_cq01->close();?>
四:離線天級統(tǒng)計和清理數(shù)據(jù)腳本
?php/*** static log :每天離線統(tǒng)計代碼日志和刪除五天前的日志**@Author:cuihuan* 2015-11-06* */// 離線統(tǒng)計$link_2004= mysql_connect('ip:port','user','pwd');if(!$link_2004) {die("Could not connect:". mysql_error());}$crowd_db= mysql_select_db('fb_log',$link_2004);// 統(tǒng)計昨天的數(shù)據(jù)$day_time= date("Y-m-d", time() -60*60*24*1);$static_sql="get sql";$res= mysql_query($static_sql,$link_2004);// 獲取結(jié)果入庫略// 清理15天之前的數(shù)據(jù)$before_15_day= date("Y-m-d", time() -60*60*24*15);$delete_sql="delete from xxx where createtime < '".$before_15_day."'";try{$res= mysql_query($delete_sql);}catch(Exception$e){echojson_encode($e)."\n";echo"delete result:".json_encode($res)."\n";}mysql_close($link_2004);?>
五:代碼部署
主要是部署亥啦,批量入庫腳本的調(diào)用和天級統(tǒng)計腳本,crontab例行運行练链。
# 批量入庫腳本*/2 * * * * /home/cuihuan/xxx/lamp/php5/bin/php /home/cuihuan/xxx/batchLog.php>>/home/cuihuan/xxx/batchlog.log# 天級統(tǒng)計腳本05* * */home/cuihuan/xxx/php5/bin/php /home/cuihuan/xxx/staticLog.php>>/home/cuihuan/xxx/staticLog.log
總結(jié):相對于其他復雜的方式處理高并發(fā)翔脱,這個解決方案簡單有效:通過redis緩存抗壓,mysql批量入庫解決數(shù)據(jù)庫瓶頸媒鼓,離線計算解決統(tǒng)計數(shù)據(jù)届吁,通過定期清理保證庫的大小错妖。