In The Sentinel mode of Redis, when the primary server fails, the corresponding secondary server will be switched to the primary server by election, so as to achieve high availability of the service.

At the service level, if the master and slave switch is performed, the IP address of the corresponding server may change, which brings about the normal operation of the program.

In order not to affect its services, it considers to use VIP to realize IP floating. However, in some cases, VMS do not support VIP, so services cannot be ensured. So in this case, the connection to the new master IP is implemented through the business itself.

This article mainly takes PHP as an example, the relevant code is as follows:

<? PHP class SRedis {/** ** var array * eg: [['host'= >'127.0.0.1' , 'port' => 26379 ] ]
     */
    private $_sentinelAddr = [];

    private $_sentinelConn = null;

    private $_timeout= 10; // Timeout duration private$_masterName = 'mymaster'; // Primary node name private static$_handle= []; // store redis connection instance publicfunction __construct(array $iplist, string $masterName = null)
    {
        $this->_sentinelAddr = $iplist;
        $masterName! == null &&$this->_masterName = $masterName;
        $this->_getSentinelConn(); } /** * get an instance of the primary redis node * @return bool|Redis
     * @throws Exception
     */
    public function getInstansOf()
    {
        $masterInfo = $this->getMasterInfo();
        if ($masterInfo) {
            $instansof = $this->_connection($masterInfo[0].$masterInfo[1].$this->_timeout);
            return $instansof;
        }
        return false; } /** * obtain the IP address of the primary node * @return array
     */
    public function getMasterInfo()
    {
        $masterInfo = [];
        if ($this->_sentinelConn ! = null) {$masterInfo = $this->_sentinelConn->rawcommand("sentinel".'get-master-addr-by-name'.$this->_masterName);
        }
        return $masterInfo; } /** * Sets the sentry connection handle */ privatefunction _getSentinelConn()
    {
        if (is_array($this->_sentinelAddr) && $this->_sentinelAddr) {
            $this->_sentinelConn = $this->_RConnect($this->_sentinelAddr); } /** * Get the redis handle (if multiple hosts are connected, make sure the sentinel server is available) * @param array$hosts
     * @return null|Redis
     */
    private function _RConnect(array $hosts)
    {
        $count = count($hosts);
        $redis = null;
        if ($count= = 1) {$this->_connection($hosts[0] ['host'].$hosts[0] ['port'].$this->_timeout);
        } else {
            $i = 0;
            while ($redis == null && $i < $count) {
                $redis = $this->_connection($hosts[$i] ['host'].$hosts[$i] ['port'].$this->_timeout);
                $i++;
            }
        }
        return $redis; } /** * redis connection handle * @param string$host
     * @param int $port
     * @param int $timeout
     * @return null|Redis
     */
    private function _connection(string $host, int $port, int $timeout)
    {
        if (isset(self::$_handle[$host . ':' . $port]) {return self::$_handle[$host . ':' . $port];
        }
        try {
            $redis = new Redis();
            $redis->connect($host.$port.$timeout);
            self::$_handle[$host . ':' . $port] = $redis;
        } catch (\Exception $e) {
            $redis = null;
        }
        return $redis; }}$hosts= [['host'= >'127.0.0.1'.'port' => 26381
    ],
    [
        'host'= >'127.0.0.1'.'port'= > 26380]];$masterName = 'mymaster';
$sredis = new SRedis($hosts.$masterName);
$masterRedis = $sredis->getInstansOf();
if ($masterRedis) {
    print_r($masterRedis->hgetall("iplist"));
} else {
    echo "Redis server connection failed";
}Copy the code