關於redis主從服務器配置就略過了,如下基於你已經配置好好,而後在laravel環境下如何使用。php
laravel 使用的是 predis 擴展
composer require predis/predis
vi config/database.phplaravel
'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' => '\App\Services\Redis', ], ], ]; //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 App\Services; use \Predis\Command\CommandInterface; use \Predis\Connection\StreamConnection; 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' => '\App\Services\Redis', ], ]; $client = new \Predis\Client(config('database.redis.default'), $options); $client->get('redis:test'); print_r($client->getConnection()); } }
看文檔 https://github.com/nrk/predis 須要將 replication 設置爲truegit
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:github
//具體參數含義 vendor/predis/predis/src/Connection/ParametersInterface.php:16 $redis = new \Predis\Client([ '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