需求背景:有個(gè)
調(diào)用統(tǒng)計(jì)日志存儲和統(tǒng)計(jì)需求
置森,要求存儲到mysql中;存儲數(shù)據(jù)高峰能達(dá)到日均千萬符糊,瓶頸在于直接入庫并發(fā)太高凫海,可能會把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ù)庫表和存儲
考慮到log系統(tǒng)對數(shù)據(jù)庫的性能更多一些宴猾,穩(wěn)定性和安全性沒有那么高,
存儲引擎自然是只支持select insert 沒有索引的archive
叼旋。如果確實(shí)有update需求仇哆,也可以采用myISAM。考慮到log是實(shí)時(shí)記錄的所有數(shù)據(jù)夫植,數(shù)量可能巨大讹剔,
主鍵采用bigint油讯,自增即可
。考慮到log系統(tǒng)
以寫為主延欠,統(tǒng)計(jì)采用離線計(jì)算陌兑,字段均不要出現(xiàn)索引
,因?yàn)橐环矫婵赡軙绊懖迦霐?shù)據(jù)效率由捎,另外讀時(shí)候會造成死鎖兔综,影響寫數(shù)據(jù)。
二:redis存儲數(shù)據(jù)形成消息隊(duì)列
由于高并發(fā)狞玛,盡可能簡單软驰,直接,上代碼心肪。
<?php
/***************************************************************************
*
* 獲取到的調(diào)用日志锭亏,存入redis的隊(duì)列中.
* $Id$
*
**************************************************************************/
/**
* @file saveLog.php
* @date 2015/11/06 20:47:13
* @author:cuihuan
* @version $Revision$
* @brief
*
**/
// 獲取info
$interface_info = $_GET['info'];
// 存入redis隊(duì)列
$redis = new Redis();
$redis->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ù)硬鞍,批量入庫慧瘤。
<?php
/**
* 獲取redis消息隊(duì)列中的腳本,拼接sql膳凝,批量入庫碑隆。
* @update 2015-11-07 添加失敗消息隊(duì)列回滾機(jī)制
*
* @Author:cuihuan
* 2015-11-06
* */
// init redis
$redis_xx = new Redis();
$redis_xx->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 ($count < $max) {
$log_info = $redis_cq01->lPop("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é)果;
echo date("Y-m-d H:i:s")."insert ".$count." log info result:";
echo json_encode($res);
echo "</br>\n";
// 數(shù)據(jù)庫插入失敗回滾
if(!$res){
foreach($roll_back_arr as $k){
$redis_xx->rPush("call_log", $k);
}
}
// 釋放連接
mysql_free_result($res);
mysql_close($link_2004);
}
// 釋放redis
$redis_cq01->close();
?>
四:離線天級統(tǒng)計(jì)和清理數(shù)據(jù)腳本
?php
/**
* static log :每天離線統(tǒng)計(jì)代碼日志和刪除五天前的日志
*
* @Author:cuihuan
* 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 = "delete from xxx where createtime < '" . $before_15_day . "'";
try {
$res = mysql_query($delete_sql);
}catch(Exception $e){
echo json_encode($e)."\n";
echo "delete result:".json_encode($res)."\n";
}
mysql_close($link_2004);
?>
五:代碼部署
主要是部署上煤,批量入庫腳本的調(diào)用和天級統(tǒng)計(jì)腳本,crontab例行運(yùn)行著淆。
# 批量入庫腳本
*/2 * * * * /home/cuihuan/xxx/lamp/php5/bin/php /home/cuihuan/xxx/batchLog.php >>/home/cuihuan/xxx/batchlog.log
# 天級統(tǒng)計(jì)腳本
0 5 * * * /home/cuihuan/xxx/php5/bin/php /home/cuihuan/xxx/staticLog.php >>/home/cuihuan/xxx/staticLog.log
總結(jié):相對于其他復(fù)雜的方式處理高并發(fā)劫狠,這個(gè)解決方案簡單有效:通過redis緩存抗壓,mysql批量入庫解決數(shù)據(jù)庫瓶頸永部,離線計(jì)算解決統(tǒng)計(jì)數(shù)據(jù)独泞,通過定期清理保證庫的大小。