This article mainly introduces the method of Laravel framework to realize Redis cluster, briefly analyzes the function setting steps, related operation skill and matters needing attention of Redis database cluster in Laravel framework, and needs friends to refer to
This paper describes the method of implementing Redis cluster in Laravel framework. Share to everyone for your reference, as follows:
The configuration in app/config/database.php is as follows:
' Redis ' = = Array ( ' cluster ' = ' = ', ' default ' = = Array ( ' host ' = ' 172.21.107.247 '), ' Port ' = 6379, ' redis1 ' = = Array ( ' host ' = = ' 172.21.107.248 ' , ' port ' = > 6379, ),
Where cluster is selected as true, then it can be used as a cluster;
If you set the session's driver to Redis, you can use its cluster functionality:
Let's take a look at the implementation of the session when we write this in the code:
Session::p ut (' Test ', 124);
The actual execution process is this:
illuminate\support\facades\sessionilluminate\support\facades\facadeilluminate\session\facade::app[' Session '- >putilluminate\session\facade::app[' Session '] for Illuminate\session\sessionmanagerilluminate\support\manager:: __call
Session will create driver based on return
$this->app[' config ' [' session.driver ']
That is configured in the configuration file, where we are configured as Redis
Illuminate\session\sessionmanager::illuminate\session\sessionmanager
Finally, the Illuminate\session\store is responsible for the put call.
The store class that is responsible for storing is Illuminate\session\cachebasedsessionhandler
The latter also forwarded the request to $this->app['cache']->driver($driver)
...
After a series of code tracing, the storage class is predis\client\database, looking at its constructor:
Public function __construct (array $servers = Array ()) { if (isset ($servers [' cluster ')] && $servers [' Cluster ']) { $this->clients = $this->createaggregateclient ($servers); } else { $this->clients = $this->createsingleclients ($servers);} }
If set to cluster, call the Createaggregateclient method
protected function createaggregateclient (array $servers) { $servers = array_except ($servers, Array (' cluster ')); Return Array (' default ' = = new Client (array_values ($servers)));}
This will place all the servers in the default group.
The class that actually stored the data is predis\client, here has the code which creates the server according to the configuration, the concrete can look down;
The Predis\cluster\predisclusterhashstrategy class is responsible for calculating the hash of key, the key function:
Gethash
Getkeyfromfirstargument
While predis\cluster\distribution\hashring is responsible for server ring maintenance, the key function
Addnodetoring
Get
Hash
The approximate principle is this, such as performing the following Redis commands
get ok
Will get a hash value for the OK CRC32 operation.
All servers are placed in an array with a default length of 128, and each server occupies several of them, as determined by the following:
Weight/Total weight * Total number of servers *128, refer to Predis\Cluster\Distribution\HashRing::addNodeToRing
method
The hash value for each item is based on the server IP: Port format, which is calculated as Crc32
protected function addnodetoring (& $ring, $node, $totalNodes, $replicas, $weightRatio) { $nodeObject = $node [' Object ']; $nodeHash = $this->getnodehash ($nodeObject); $replicas = (int) round ($weightRatio * $totalNodes * $replicas); for ($i = 0; $i < $replicas; $i + +) { $key = CRC32 ("$nodeHash: $i"); $ring [$key] = $nodeObject; }}
Key hash value also has, the server loop is also calculated, the rest is to find, the dichotomy can quickly find the corresponding server node