Redis among the Sentinel architecture

 

Sentinel (Sentinel) is a high availability solutions the Redis: Sentinel Sentinel system consists of one or more instances of the master can monitor any number of servers, and all those under the master server from the server, and enters the primary server being monitored off-line mode and automatically off the assembly line under the main server upgrade from a server to a new primary server.

E.g:

 

After Server1 dropped:

Upgrade Server2 as the new primary server:

The first step : three machines modified configuration file Sentinel

Three machines execute the following command to modify the configuration file redis Sentinel

cd /export/servers/redis-3.2.8

vim sentinel.conf

# Set the monitor's main server, where representatives monitor sentinel monitor, on behalf of the server name mymaster, you can customize, 192.168.11.128 representatives to monitor the primary server, port 6379 on behalf of, on behalf of more than 2 only two or sentry think the main server unavailable time, will perform failover operations.

# Modify bind configuration, each station machine modified to own the corresponding hostname

Volume node01  

# Configure sentinel service running in the background

daemonize yes

# Modified three primary node machine monitor, is now the master node node01 server

sentinel monitor mymaster node01 6379 2

Password # sentinel author-pass-defined services, mymaster is the service name, Redis server password is 123456

# sentinel auth-pass <master-name> <password>

 

 

 

 

Step two : three machine start Sentinel Services

Three machines execute the following command to start the Sentinel Services

cd /export/servers/redis-3.2.8

src/redis-sentinel sentinel.conf

 

Note: At this point redis-server and two sentinel must start

 

The third step : node01 server kill redis service process

Kill -9 command to kill the process redis service, fault simulation redis downtime situation

After a period of time, will select a server in the server to switch node03 node02 with the primary node

 

Step Four : sentinel redis the model code development connection

/**
 * 测试Redis哨兵模式
 * @author liu
 */
    @Test
    public void testSentinel() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMinIdle(5);
        // 哨兵信息
        Set<String> sentinels = new HashSet<>(Arrays.asList("node01:26379",
                "node02:26379","node03:26379"));
        // 创建连接池
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,jedisPoolConfig);
        // 获取客户端
        Jedis jedis = pool.getResource();
        // 执行两个命令
        jedis.set("mykey", "myvalue");
        String value = jedis.get("mykey");
        System.out.println(value);
    }

 

Published 176 original articles · won praise 278 · views 80000 +

Guess you like

Origin blog.csdn.net/weixin_43893397/article/details/105032816