標籤:version each max 緩衝 bre sse pop except link
需求背景:有個調用統計日誌儲存和統計需求,要求儲存到mysql中;儲存資料高峰能達到日均千萬,瓶頸在於直接入庫並發太高,可能會把mysql幹垮。
問題分析
思考:應用網站架構的衍化過程中,應用最新的架構和工具技術固然是最優選擇;但是,如果能在現有的架構的基礎上提出簡單可依賴的解決方案,未嘗不是一種提升自我的嘗試。
解決:
問題一:要求日誌最好入庫;但是,直接入庫mysql確實扛不住,批量入庫沒有問題,done。【批量入庫和直接入庫效能差異參考文章】
問題二:批量入庫就需要有高並發的訊息佇列,決定採用redis list 模擬實現,而且方便復原。
問題三:日誌量畢竟大,儲存最近30條足矣,決定用php寫個離線統計和清理指令碼。
done,下面是小拽的簡單實現過程
一:設計資料庫表和儲存
考慮到log系統對資料庫的效能更多一些,穩定性和安全性沒有那麼高,儲存引擎自然是只支援select insert 沒有索引的archive。如果確實有update需求,也可以採用myISAM。
考慮到log是即時記錄的所有資料,數量可能巨大,主鍵採用bigint,自增即可。
考慮到log系統以寫為主,統計採用離線計算,欄位均不要出現索引,因為一方面可能會影響插入資料效率,另外讀時候會造成死結,影響寫資料。
二:redis儲存資料形成訊息佇列
由於高並發,儘可能簡單,直接,上代碼。
<?php
/***************************************************************************
*
* 擷取到的調用日誌,存入redis的隊列中.
* $Id$
*
**************************************************************************/
/**
* @file saveLog.php
* @date 2015/11/06 20:47:13
* @author:cuihuan
* @version $Revision$
* @brief
*
**/
// 擷取info
$interface_info = $_GET[‘info‘];
// 存入redis隊列
$redis = new Redis();
$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 */
?>
三:資料定時批量入庫。
定時讀取redis訊息佇列裡面的資料,批量入庫。
<?php
/**
* 擷取redis訊息佇列中的指令碼,拼接sql,批量入庫。
* @update 2015-11-07 添加失敗訊息佇列復原機制
*
* @Author:cuihuan
* 2015-11-06
* */
// init redis
$redis_xx = new Redis();
$redis_xx->connect(‘ip‘, port);
$redis_xx->auth("password");
// 擷取現有訊息佇列的長度
$count = 0;
$max = $redis_xx->lLen("call_log");
// 擷取訊息佇列的內容,拼接sql
$insert_sql = "insert into fb_call_log (`interface_name`, `createtime`) values ";
// 復原數組
$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++;
}
// 判定存在資料,批量入庫
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和入庫結果;
echo date("Y-m-d H:i:s")."insert ".$count." log info result:";
echo json_encode($res);
echo "</br>\n";
// 資料庫插入失敗復原
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();
?>
四:離線天級統計和清理資料指令碼
?php
/**
* static log :每天離線統計代碼日誌和刪除五天前的日誌
*
* @Author:cuihuan
* 2015-11-06
* */
// 離線統計
$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);
// 統計昨天的資料
$day_time = date("Y-m-d", time() - 60 * 60 * 24 * 1);
$static_sql = "get sql";
$res = mysql_query($static_sql, $link_2004);
// 擷取結果入庫略
// 清理15天之前的資料
$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);
?>
五:代碼部署
主要是部署,批量入庫指令碼的調用和天級統計指令碼,crontab例行運行。
# 批量入庫指令碼
*/2 * * * * /home/cuihuan/xxx/lamp/php5/bin/php /home/cuihuan/xxx/batchLog.php >>/home/cuihuan/xxx/batchlog.log
# 天級統計指令碼
0 5 * * * /home/cuihuan/xxx/php5/bin/php /home/cuihuan/xxx/staticLog.php >>/home/cuihuan/xxx/staticLog.log
總結:相對於其他複雜的方式處理高並發,這個解決方案簡單有效:通過redis緩衝抗壓,mysql批量入庫解決資料庫瓶頸,離線計算解決統計資料,通過定期清理保證庫的大小。
轉載URL: 1190000004136250
轉載:【高並發簡單解決方案 | 靠譜崔小拽 】redis隊列緩衝 + mysql 批量入庫 + php離線整合