摘要:關于主從服務器配置就略過了,以下基于你已經配置好好,然后在環境下如何使用。配置使用的是擴展主從從隊列用到命令參考注意看文檔需要將設置為使用具體參數含義查看日志可以看到使用的從服務器查看日志可以看到使用的主服務器
關于redis主從服務器配置就略過了,以下基于你已經配置好好,然后在laravel環境下如何使用。
配置laravel 使用的是 predis 擴展
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, ],//隊列用到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 設置為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:
使用//具體參數含義 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日志可以看到使用的從服務器 REDIS_DEFAULT_HOST_R dump($redis->get($key)); //查看Redis日志可以看到使用的主服務器 REDIS_DEFAULT_HOST
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29455.html
摘要:主從配置復制配置文件以開啟多個編輯配置文件,主要修改參數主從都保持一樣的密碼,且的配置也需要這一行,在執行切換的時候好像不會去添加這一行通過配置啟動哨兵配置復制哨兵配置,這兒開啟個哨兵編輯哨兵配置文件,主要修改參數如 主從配置(master-slave) 復制 redis 配置文件以開啟多個 slave sudo cp /etc/redis.conf /etc/redis-6381.c...
摘要:由一層函數調用進入下一層函數調用的遞推。此時,中的一個稱為孤兒的類就會收留這個對象。禁止訪問服務器拒絕請求服務器找不到請求的頁面服務器內部錯誤壞的網關一般是網關服務器請求后端服務時,后端服務沒有按照協議正確返回結果。 持續更新。。。。 php 1. 簡述 php 中的 autoload Autoload 的加載機制,當通過 new 來實例化一個類時,PHP 會通過定義的 autol...
閱讀 866·2021-11-15 11:37
閱讀 3604·2021-11-11 16:55
閱讀 3270·2021-11-11 11:01
閱讀 999·2019-08-30 15:43
閱讀 2743·2019-08-30 14:12
閱讀 681·2019-08-30 12:58
閱讀 3389·2019-08-29 15:19
閱讀 2025·2019-08-29 13:59