【高併發簡單解決方案】redis佇列快取 + mysql 批量入庫 + php離線整合
問題分析
思考:應用網站架構的衍化過程中,應用最新的框架和工具技術固然是最優選擇;但是,如果能在現有的框架的基礎上提出簡單可依賴的解決方案,未嘗不是一種提升自我的嘗試。
解決:
問題一:要求日誌最好入庫;但是,直接入庫mysql確實扛不住,批量入庫沒有問題,done。
問題二:批量入庫就需要有高併發的訊息佇列,決定採用redis list 模擬實現,而且方便回滾。
問題三:日誌量畢竟大,儲存最近30條足矣,決定用php寫個離線統計和清理指令碼。
done,下面是小拽的簡單實現過程
一:設計資料庫表和儲存
考慮到log系統對資料庫的效能更多一些,穩定性和安全性沒有那麼高,儲存引擎自然是隻支援select insert 沒有索引的archive。如果確實有update需求,也可以採用myISAM。
考慮到log是實時記錄的所有資料,數量可能巨大,主鍵採用bigint,自增即可。
考慮到log系統以寫為主,統計採用離線計算,欄位均不要出現索引,因為一方面可能會影響插入資料效率,另外讀時候會造成死鎖,影響寫資料。
二:redis儲存資料形成訊息佇列
由於高併發,儘可能簡單,直接,上程式碼。
<?php
//獲取到的呼叫日誌,存入redis的佇列中.
// 獲取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();
?>
三:資料定時批量入庫。
定時讀取redis訊息佇列裡面的資料,批量入庫。
<?php
// 獲取redis訊息佇列中的指令碼,拼接sql,批量入庫。
// 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
//每天離線統計程式碼日誌和刪除五天前的日誌
// 離線統計
$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批量入庫解決資料庫瓶頸,離線計算解決統計資料,通過定期清理保證庫的大小。