redis集群使用
請查看安裝章節(jié)的redis集群相關(guān)配置使用
集群兼容方法
在正常情況下,有些方法是不能直接被集群客戶端調(diào)用成功的,比如mSet方法,它涉及了多個(gè)鍵名的操作,而多個(gè)鍵名是會分配給其他節(jié)點(diǎn)的
目前redis集群客戶端,實(shí)現(xiàn)了部分多鍵名操作方法的兼容,實(shí)現(xiàn)原理如下:
對多鍵名操作方法,進(jìn)行拆分成單鍵名,然后通過鍵名去獲取槽節(jié)點(diǎn),再通過槽節(jié)點(diǎn)分配的client去執(zhí)行,每次只會執(zhí)行一個(gè)鍵名
已經(jīng)實(shí)現(xiàn)了兼容的方法:
方法名稱 | 參數(shù) | 說明 | 備注 |
---|---|---|---|
mSet | $data | 設(shè)置多個(gè)鍵值對 | |
mGet | $keys | 獲取多個(gè)鍵名的值 | |
mSetNx | $data | 設(shè)置多個(gè)鍵值對 | 該方法將不能準(zhǔn)確的判斷"當(dāng)所有key不存在時(shí),設(shè)置多個(gè)key值" |
集群禁用方法
由于集群的特性,不同的key分配到了不同的槽位,當(dāng)你調(diào)用sUnion,sUnIonStore等涉及多個(gè)key操作的命令時(shí),將會返回false,同時(shí)錯(cuò)誤信息會在$redis->getErrorMsg()中顯示:
$redis = new \EasySwoole\Redis\RedisCluster(new \EasySwoole\Redis\Config\RedisClusterConfig([
['172.16.253.156', 9001],
['172.16.253.156', 9002],
['172.16.253.156', 9003],
['172.16.253.156', 9004],
], [
'auth' => '',
'serialize' => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_PHP
]));
$data = $redis->sUnIonStore('a','v','c');
var_dump($data,$redis->getErrorMsg());
將輸出:
bool(false)
string(53) "CROSSSLOT Keys in request don't hash to the same slot"
集群客戶端調(diào)度邏輯
客戶端默認(rèn)調(diào)度
集群客戶端在調(diào)用redis方法的時(shí)候,自動默認(rèn)一個(gè)客戶端進(jìn)行發(fā)送接收命令:
function sendCommand(array $com, ?ClusterClient $client = null): bool
{
$client = $client ?? $this->getDefaultClient();
$this->setDefaultClient($client);
return $this->sendCommandByClient($com, $client);
}
function recv($timeout = null, ?ClusterClient $client = null): ?Response
{
$client = $client ?? $this->getDefaultClient();
$this->setDefaultClient($client);
return $this->recvByClient($client, $timeout);
}
當(dāng)get,或者set的key值槽位不一致時(shí),會自動切換客戶端進(jìn)行發(fā)送接收命令:
//節(jié)點(diǎn)轉(zhuǎn)移客戶端處理
if ($result->getErrorType() == 'MOVED') {
$nodeId = $this->getMoveNodeId($result);
$client = $this->getClient($nodeId);
$this->clientConnect($client);
//只處理一次moved,如果出錯(cuò)則不再處理
$client->sendCommand($command);
$result = $client->recv($timeout ?? $this->config->getTimeout());
}
切換完成之后,下一次命令,依舊是默認(rèn)客戶端.
獲取集群的客戶端
集群操作方法列表:
方法名稱 | 參數(shù) | 說明 | 備注 |
---|---|---|---|
getNodeClientList | 獲取集群客戶端列表 | ||
getNodeList | 獲取集群節(jié)點(diǎn)信息數(shù)組 | ||
clientAuth | ClusterClient $client, $password | 集群客戶端auth驗(yàn)證 | |
setDefaultClient | ClusterClient $defaultClient | 設(shè)置一個(gè)默認(rèn)的客戶端 | |
getDefaultClient | 獲取一個(gè)默認(rèn)的客戶端(初始化會自動默認(rèn)一個(gè)) | ||
tryConnectServerList | 嘗試重新獲取客戶端列表 | 當(dāng)調(diào)用命令返回false,可嘗試重新獲取 | |
getClient | $nodeKey = null | 根據(jù)nodeKey獲取一個(gè)客戶端 | |
getMoveNodeId | Response $response | 根據(jù)recv返回的Move消息獲取一個(gè)nodeKey | |
getSlotNodeId | $slotId | 根據(jù)槽id獲取 nodeKey |
這些方法用于用戶自定義發(fā)送命令給redis服務(wù)端,或者是自己定義默認(rèn)客戶端進(jìn)行發(fā)送
集群兼容管道方法
由于管道的特性,開啟管道后,之后執(zhí)行的命令將會保存不會直接發(fā)送,直到最后執(zhí)行execPipe才會一次性發(fā)送
在集群中,只能選擇一個(gè)客戶端,進(jìn)行一次性發(fā)送命令:
方法名稱 | 參數(shù) | 說明 | 備注 |
---|---|---|---|
execPipe | ?ClusterClient $client = null | 一次性執(zhí)行管道中保存的方法 | 可通過獲取客戶端列表,自定義選擇一個(gè)客戶端進(jìn)行發(fā)送 |
discardPipe | 取消管道 | ||
startPipe | 管道開始記錄 |