redis緩存隊(duì)列+mysql 批量入庫+php離線整合

redis緩存隊(duì)列+mysql 批量入庫+php離線整合


需求背景:有個(gè)調(diào)用統(tǒng)計(jì)日志存儲(chǔ)和統(tǒng)計(jì)需求,要求存儲(chǔ)到mysql中;存儲(chǔ)數(shù)據(jù)高峰能達(dá)到日均千萬,瓶頸在于直接入庫并發(fā)太高,可能會(huì)把mysql干垮。

問題分析

思考:應(yīng)用網(wǎng)站架構(gòu)的衍化過程中,應(yīng)用最新的框架和工具技術(shù)固然是最優(yōu)選擇;但是,如果能在現(xiàn)有的框架的基礎(chǔ)上提出簡單可依賴的解決方案,未嘗不是一種提升自我的嘗試。

解決:

問題一:要求日志最好入庫;但是,直接入庫mysql確實(shí)扛不住,批量入庫沒有問題,done。

問題二:批量入庫就需要有高并發(fā)的消息隊(duì)列,決定采用redis list 仿真實(shí)現(xiàn),而且方便回滾。

問題三:日志量畢竟大,保存最近30條足矣,決定用php寫個(gè)離線統(tǒng)計(jì)和清理腳本。

done,下面是小拽的簡單實(shí)現(xiàn)過程

一:設(shè)計(jì)數(shù)據(jù)庫表和存儲(chǔ)

考慮到log系統(tǒng)對(duì)數(shù)據(jù)庫的性能更多一些,穩(wěn)定性和安全性沒有那么高,存儲(chǔ)引擎自然是只支持select insert 沒有索引的archive。如果確實(shí)有update需求,也可以采用myISAM。

考慮到log是實(shí)時(shí)記錄的所有數(shù)據(jù),數(shù)量可能巨大,主鍵采用bigint,自增即可。

考慮到log系統(tǒng)以寫為主,統(tǒng)計(jì)采用離線計(jì)算,字段均不要出現(xiàn)索引,因?yàn)橐环矫婵赡軙?huì)影響插入數(shù)據(jù)效率,另外讀時(shí)候會(huì)造成死鎖,影響寫數(shù)據(jù)。

二:redis存儲(chǔ)數(shù)據(jù)形成消息隊(duì)列

由于高并發(fā),盡可能簡單,直接,上代碼。

connect('xx',6379);

$redis->auth("password");

//加上時(shí)間戳存入隊(duì)列

$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ù)定時(shí)批量入庫。

定時(shí)讀取redis消息隊(duì)列里面的數(shù)據(jù),批量入庫。

connect('ip',port);

$redis_xx->auth("password");

//獲取現(xiàn)有消息隊(duì)列的長度

$count=0;

$max=$redis_xx->lLen("call_log");

//獲取消息隊(duì)列的內(nèi)容,拼接sql

$insert_sql="insert into fb_call_log (`interface_name`, `createtime`) values ";

//回滾數(shù)組

$roll_back_arr=array();

while($countlPop("call_log");

$roll_back_arr=$log_info;

if($log_info=='nil'||!isset($log_info)){

$insert_sql.=";";

break;

}

//切割出時(shí)間和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();

?>

四:離線天級(jí)統(tǒng)計(jì)和清理數(shù)據(jù)腳本

?php

/**

* static log :每天離線統(tǒng)計(jì)代碼日志和刪除五天前的日志

*

* @Author:cuihuan@baidu.com

* 2015-11-06

* */

//離線統(tǒng)計(jì)

$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)計(jì)昨天的數(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="deletefromxxxwherecreatetime

五:代碼部署

主要是部署,批量入庫腳本的調(diào)用和天級(jí)統(tǒng)計(jì)腳本,crontab例行運(yùn)行。

# 批量入庫腳本

*/2****/home/cuihuan/xxx/lamp/php5/bin/php/home/cuihuan/xxx/batchLog.php>>/home/cuihuan/xxx/batchlog.log

# 天級(jí)統(tǒng)計(jì)腳本

05***/home/cuihuan/xxx/php5/bin/php/home/cuihuan/xxx/staticLog.php>>/home/cuihuan/xxx/staticLog.log

總結(jié):相對(duì)于其他復(fù)雜的方式處理高并發(fā),這個(gè)解決方案簡單有效:通過redis緩存抗壓,mysql批量入庫解決數(shù)據(jù)庫瓶頸,離線計(jì)算解決統(tǒng)計(jì)數(shù)據(jù),通過定期清理保證庫的大小。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容