需求背景:有個
調(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?!九咳霂旌椭苯尤霂煨阅懿町?a target="_blank" rel="nofollow">參考文章】
問題二:批量入庫就需要有高并發(fā)的消息隊(duì)列,決定采用redis list 仿真實(shí)現(xiàn),而且方便回滾。
問題三:日志量畢竟大,保存最近30條足矣,決定用php寫個離線統(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ù)據(jù),數(shù)量可能巨大,
主鍵采用bigint,自增即可。考慮到log系統(tǒng)
以寫為主,統(tǒng)計(jì)采用離線計(jì)算,字段均不要出現(xiàn)索引,因?yàn)橐环矫婵赡軙绊懖迦霐?shù)據(jù)效率,另外讀時候會造成死鎖,影響寫數(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");
// 加上時間戳存入隊(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ù)定時批量入庫。
定時讀取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;
}
// 切割出時間和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ā),這個解決方案簡單有效:通過redis緩存抗壓,mysql批量入庫解決數(shù)據(jù)庫瓶頸,離線計(jì)算解決統(tǒng)計(jì)數(shù)據(jù),通過定期清理保證庫的大小。
【轉(zhuǎn)載請注明:高并發(fā)簡單解決方案 | 靠譜崔小拽 】