摘要:關(guān)于主從服務(wù)器配置就略過了,以下基于你已經(jīng)配置好好,然后在環(huán)境下如何使用。配置使用的是擴(kuò)展主從從隊(duì)列用到命令參考注意看文檔需要將設(shè)置為使用具體參數(shù)含義查看日志可以看到使用的從服務(wù)器查看日志可以看到使用的主服務(wù)器
關(guān)于redis主從服務(wù)器配置就略過了,以下基于你已經(jīng)配置好好,然后在laravel環(huán)境下如何使用。
配置laravel 使用的是 predis 擴(kuò)展
composer require predis/predis
vi config/database.php
"redis"=>[ "cluster" => false, "default" => [ "tcp://" . env("REDIS_DEFAULT_HOST") . ":" . env("REDIS_DEFAULT_PORT") . "?database=0&alias=master",//主 "tcp://" . env("REDIS_DEFAULT_HOST_R") . ":" . env("REDIS_DEFAULT_PORT_R") . "?database=0&alias=slave_1",//從 "tcp://" . env("REDIS_DEFAULT_HOST_R2") . ":" . env("REDIS_DEFAULT_PORT_R2") . "?database=0&alias=slave_2",//從 ], "test" => [ "host" => "127.0.0.1", "port" => 6379, "database" => 1, ],//隊(duì)列用到multi命令 "options" => [ "replication" => true, "connections" =>[ "tcp" => "AppServicesRedis", ], ], ]; //master slave vendor/predis/predis/src/Connection/Aggregate/SentinelReplication.php:189 public function add(NodeConnectionInterface $connection) { $alias = $connection->getParameters()->alias; if ($alias === "master") { $this->master = $connection; } else { $this->slaves[$alias ?: count($this->slaves)] = $connection; } $this->reset(); } vi app/services/redis.php //參考 https://github.com/nrk/predis/blob/v1.1/examples/debuggable_connection.php namespace AppServices; use PredisCommandCommandInterface; use PredisConnectionStreamConnection; class Redis extends StreamConnection { private $tstart = 0; private $debugBuffer = []; public function connect() { $this->tstart = microtime(true); parent::connect(); } private function storeDebug(CommandInterface $command, $direction) { $firtsArg = $command->getArguments(); $timestamp = (microtime(true) - $this->tstart) * 1000; $log = []; $log["cmd"] = $command->getId(); $log["key"] = isset($firtsArg) ? $firtsArg : " "; $log["server"] = "$direction $this"; $log["time"] = $timestamp; $data = ["server" => trim($log["server"]), "cmd" => $command->getId(), "key" => $log["key"],"time" => $timestamp, "msg" => ["host" => explode(":", trim($log["server"]))[0], "port" => explode(":", trim($log["server"]))[1]]]]; openlog("syslog",LOG_PID|LOG_ODELAY,LOG_LOCAL7); syslog(LOG_INFO,json_encode($data)); closelog(); dump($log); $this->debugBuffer[] = $log; } public function writeRequest(CommandInterface $command) { parent::writeRequest($command); // $this->storeDebug($command, "->"); } public function readResponse(CommandInterface $command) { $response = parent::readResponse($command); $this->storeDebug($command, ""); return $response; } public function getDebugBuffer() { return $this->debugBuffer; } public static function debug() { $options = [ "connections" =>[ "tcp" => "AppServicesRedis", ], ]; $client = new PredisClient(config("database.redis.default"), $options); $client->get("redis:test"); print_r($client->getConnection()); } }注意
看文檔 https://github.com/nrk/predis 需要將 replication 設(shè)置為true
The basic configuration needed to use the client in replication mode requires one Redis server to be identified as the master (this can be done via connection parameters using the alias parameter set to master) and one or more servers acting as slaves:
使用//具體參數(shù)含義 vendor/predis/predis/src/Connection/ParametersInterface.php:16 $redis = new PredisClient([ "scheme" => "tcp", "host" => "127.0.0.1", "port" => 6379, "read_write_timeout" => 0, ]); $redis = Redis::connection("default"); $key = "master:test"; $redis->set($key, 666);//tail -f /var/log/messages 查看Redis日志可以看到使用的從服務(wù)器 REDIS_DEFAULT_HOST_R dump($redis->get($key)); //查看Redis日志可以看到使用的主服務(wù)器 REDIS_DEFAULT_HOST
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/29455.html
摘要:主從配置復(fù)制配置文件以開啟多個編輯配置文件,主要修改參數(shù)主從都保持一樣的密碼,且的配置也需要這一行,在執(zhí)行切換的時候好像不會去添加這一行通過配置啟動哨兵配置復(fù)制哨兵配置,這兒開啟個哨兵編輯哨兵配置文件,主要修改參數(shù)如 主從配置(master-slave) 復(fù)制 redis 配置文件以開啟多個 slave sudo cp /etc/redis.conf /etc/redis-6381.c...
摘要:由一層函數(shù)調(diào)用進(jìn)入下一層函數(shù)調(diào)用的遞推。此時,中的一個稱為孤兒的類就會收留這個對象。禁止訪問服務(wù)器拒絕請求服務(wù)器找不到請求的頁面服務(wù)器內(nèi)部錯誤壞的網(wǎng)關(guān)一般是網(wǎng)關(guān)服務(wù)器請求后端服務(wù)時,后端服務(wù)沒有按照協(xié)議正確返回結(jié)果。 持續(xù)更新。。。。 php 1. 簡述 php 中的 autoload Autoload 的加載機(jī)制,當(dāng)通過 new 來實(shí)例化一個類時,PHP 會通過定義的 autol...
閱讀 881·2021-11-15 11:37
閱讀 3619·2021-11-11 16:55
閱讀 3284·2021-11-11 11:01
閱讀 1008·2019-08-30 15:43
閱讀 2755·2019-08-30 14:12
閱讀 695·2019-08-30 12:58
閱讀 3397·2019-08-29 15:19
閱讀 2037·2019-08-29 13:59