Laravel framework implements redis cluster method analysis, laravelredis
This article describes how to implement a redis cluster using the Laravel framework. We will share this with you for your reference. The details are as follows:
Configure app/config/database. php as follows:
'redis' => array( 'cluster' => true, 'default' => array( 'host' => '172.21.107.247', 'port' => 6379, ), 'redis1' => array( 'host' => '172.21.107.248', 'port' => 6379, ),
Select "true" as the cluster, and then use it as the cluster;
If you set the session driver to redis, you can use the cluster function:
Let's take a look at the implementation of the session. When we write in the code like this:
Session::put('test', 124);
The actual execution process is as follows:
Illuminate \ Support \ Facades \ SessionIlluminate \ Support \ Facades \ FacadeIlluminate \ Session \ Facade: app ['session']-> putIlluminate \ session \ Facade :: app ['session'] Is Illuminate \ session \ SessionManagerIlluminate \ Support \ Manager :__ call
The Session creates a driver Based on the returned result.
$this->app['config']['session.driver']
Which is configured in the configuration file. Here we configure it as redis
Illuminate\Session\SessionManager::Illuminate\Session\SessionManager
Eventually, Illuminate \ Session \ Store is responsible for the put call.
The Store class stores Illuminate \ Session \ CacheBasedSessionHandler.
The latter forwards the request$this->app['cache']->driver($driver)
......
After a series of code tracing, the storage class is Predis \ Client \ Database. Check 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 it is set to a cluster, the createAggregateClient method is called.
protected function createAggregateClient(array $servers){ $servers = array_except($servers, array('cluster')); return array('default' => new Client(array_values($servers)));}
All servers are placed in the default group.
The actual data storage class is Predis \ Client. Here is the code for creating a server based on the configuration. You can check it for yourself;
The Predis \ Cluster \ PredisClusterHashStrategy class is responsible for calculating the key hash. key functions:
GetHash
GetKeyFromFirstArgument
Predis \ Cluster \ Distribution \ HashRing is responsible for maintaining the server ring and key functions.
AddNodeToRing
Get
Hash
The general principle is as follows: execute the following redis command
get ok
Uses OK as the crc32 operation to get a hash value.
All servers are put into an array with a default length of 128 according to a certain algorithm. The number of items that each server occupies is determined by the following:
Weight/total weight * Total number of servers * 128, referPredis\Cluster\Distribution\HashRing::addNodeToRing
Method
The hash value of each item is calculated by the server ip Address: PORT format for 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; }}
The hash value of the key is also available, and the server ring is well calculated. The rest is the search. The binary method can quickly find the corresponding server node.