php redis斷線重連,pconnect連接失敗問題
介紹
在swoole ,workerman等cli長連接模式下,遇到Redis異常斷開,后面又開啟的情況,一般得重新啟動程序才能正常使用,
本文介紹在不重啟服務,實現原來的Redis斷線重連
原理
Redis 斷開的情況下調用
$Redis->ping()會觸發Notice錯誤,Notice: Redis::ping(): send of 14 bytes failed with errno=10054
當獲取redis實例時,如果ping不通或者出現異常,就重新連接
實現1
因為try catch 捕捉不到notice異常,所以ping不通直接重新連接,catch捕捉新連接的實例沒有連接上,下次執行ping觸發
Redis server went away 異常
public static function getInstance( )
{
try {
if (!self::$_instance) {
new self();
} else {
if (!self::$_instance->ping())
new self();
}
} catch (\Exception $e) {
// 斷線重連
new self();
}
return self::$_instance;
}
實現2
1.調用ping之前先拋出個notice異常,
2.調用ping
3.用error_get_last獲取最后一個錯誤,如果錯誤信息跟我們拋出的一樣,說明ping通了,否則拋出個異常 ,讓catch捕捉到執行重連,
當重連一次沒連上再次調用$_instance->ping()會直接拋出Redis server went away異常讓catch捕捉到
public static function getInstance( )
{
if (!self::$_instance) {
new self();
}
else{
try {
@trigger_error('flag', E_USER_NOTICE);
self::$_instance->ping();
$error = error_get_last();
if($error['message'] != 'flag')
throw new \Exception('Redis server went away');
} catch (\Exception $e) {
// 斷線重連
new self();
}
}
return self::$_instance;
}
Redis類完整代碼
?php
namespace lib;
class Redis
{
private static $_instance; //存儲對象
private function __construct( ){
$config = Config::get('redis');
self::$_instance = new \Redis();
//從配置讀取
self::$_instance->pconnect($config['host'], $config['port']);
if ('' != $config['password']) {
self::$_instance->auth($config['password']);
}
}
public static function getInstance( )
{
if (!self::$_instance) {
new self();
}
else{
try {
@trigger_error('flag', E_USER_NOTICE);
self::$_instance->ping();
$error = error_get_last();
if($error['message'] != 'flag')
throw new \Exception('Redis server went away');
} catch (\Exception $e) {
// 斷線重連
new self();
}
}
return self::$_instance;
}
// public static function getInstance( )
// {
// try {
// if (!self::$_instance) {
// new self();
// } else {
// if (!self::$_instance->ping())
// new self();
// }
// } catch (\Exception $e) {
// // 斷線重連
// new self();
// }
// return self::$_instance;
// }
/**
* 禁止clone
*/
private function __clone(){}
/**
* 其他方法自動調用
* @param $method
* @param $args
* @return mixed
*/
public function __call($method,$args)
{
return call_user_func_array([self::$_instance, $method], $args);
}
/**
* 靜態調用
* @param $method
* @param $args
* @return mixed
*/
public static function __callStatic($method,$args)
{
self::getInstance();
return call_user_func_array([self::$_instance, $method], $args);
}
}
調用
$this->handler = Redis::getInstance();
$key = $this->getCacheKey($name);
$value = $this->handler->get($key);
補充
pconnect建立連接后重連失敗問題
經測試長連接下使用pconnect建立連接后,redis超時被動斷開了鏈接,
$res = self::$_instance->pconnect($config['host'], $config['port']);
$res 會返回true,但不是新建的鏈接,調用$res-get()會報錯
原因
研究發現
使用pconnect,鏈接在php進程的整個生命周期內被重用, close的作用僅是使當前php不能再進行redis請求,但無法真正關閉redis長連接,連接在后續請求中仍然會被重用,直至fpm進程生命周期結束。
長連接中只有進程被停止,連接才會斷開,所以連接斷開時new不起作用,返回連接成功,而事實上已經斷了,還是最早的那個連接,從而導致不能進行后續讀取數據操作
所以長連接中請使用connect
到此這篇關于php之redis短線重連案例講解的文章就介紹到這了,更多相關php之redis短線重連內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解PHP解決守護進程Redis假死
- thinkphp5redis緩存新增方法實例講解
- PHP使用Redis隊列執行定時任務實例講解
- php基于redis的分布式鎖實例詳解
- Thinkphp5+Redis實現商品秒殺代碼實例講解
- PHP操作Redis常用命令的實例詳解
- php在linux環境中如何使用redis詳解
- php操作redis命令及代碼實例大全