1 overview

Sentinel sentinel is a special Redis service, which is also an instance of Redis, but does not provide read and write services. Sentinel sentinel is mainly used to monitor nodes of Redis instances.

2 Redis Sentinel architecture

Under sentinel architecture, the client finds out the master node of Redis from sentinel for the first time, and then directly accesses the master node of Redis, instead of accessing the master node of Redis through Sentinel agent every time. When the master node of Redis changes, the sentinel will immediately perceive it. In addition, the new redis master node is notified to the client (here, the client of Redis generally implements the subscription function, subscribing to sentinel published node change messages).

2.1 Sentinel architecture construction

  1. Make a copy of the sentinel.conf file
cp sentinel.conf sentinel_26379.cof
Copy the code
  1. Modify related configurations as follows:
Port 26379 daemonize yes pidfile "/var/run/redis‐sentinel‐26379.pid" logfile "26379.log" dir "/usr/local/redis‐5.0.3/data" # sentinel monitor <master‐redis‐name> <master‐redis‐ IP > <master‐redis‐port> <quorum> # Quorum is a number specifying how many sentinels consider a master to have failed (the value is typically: Sentinel monitor myMaster 192.168.0.60 6379 2 # mymasterCopy the code
  1. Start the Sentinel instance
src/redis-sentinel sentinel_26379.conf
Copy the code
  1. View the sentinel info
Redis - cli - p 26379 127.0.0.1:26379 > infoCopy the code

You can see that the sentinel info has identified the redis master and slave

  1. Two more Sentinels can be configured

  2. After all sentinel clusters are started, sentinel metadata information will be written to all sentinel configuration files (appended to the end of the file) :

Sentinel known‐ Replica MyMaster 192.168.0.60 6380 # represents the secondary node information of the redis primary node sentinel Known ‐ Replica MyMaster 192.168.0.60 6381 # for redis master node from the sentinel node information known ‐ sentinel mymaster 192.168.0.60 26380 52 d0a5d70c1f90475b4fc03b6ce7c3c569 35760 f # for perceived other sentinel node sentinel known ‐ sentinel mymaster 192.168.0.60 26381 e9f530d3882f8043f76ebb8e1686438ba8 bd5ca6 # represents other sentry nodes that are perceivedCopy the code

If the primary node of Redis fails, the Sentinel cluster will re-elect a new primary node of Redis and modify the cluster metadata information of all sentinel node configuration files. For example, if the primary node of 6379 fails, assume that the new primary node elected is 6380. Then the cluster metadata information in the Sentinel file will be as follows:

Sentinel known‐replica MyMaster 192.168.0.60 6379 # Represents the secondary node information of the primary node sentinel known‐replica MyMaster 192.168.0.60 6381 # on behalf of the master node from the sentinel node information known ‐ sentinel mymaster 192.168.0.60 26380 52 d0a5d70c1f90475b4fc03b6ce7c3c569 35760 f # for perceived other sentinel node Sentinel known ‐ sentinel mymaster 192.168.0.60 26381 e9f530d3882f8043f76ebb8e1686438ba8 bd5ca6 # for perceived other sentinel nodeCopy the code

The sentinel file will also change the myMaster port 6379 to 6380

Sentinel Monitor MyMaster 192.168.0.60 6380 2Copy the code

When the redis instance of 6379 is started again, the Sentinel cluster can join the Redis node on port 6379 as a slave node based on the cluster metadata information

2.2 Redis sentinel example

Basic demo:

public class JedisSentinelTest { public static void main(String[] args) throws IOException { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(10); config.setMinIdle(5); String masterName = "mymaster"; Set<String> sentinels = new HashSet<String>(); Sentinels. Add (new HostAndPort (" 192.168.0.60 ", 26379). The toString ()); Sentinels. Add (new HostAndPort (" 192.168.0.60 ", 26380). The toString ()); Sentinels. Add (new HostAndPort (" 192.168.0.60 ", 26381). The toString ()); // The essence of JedisSentinelPool is similar to That of JedisPool, which is a connection pool made with the primary redis node. JedisSentinelPool = New JedisSentinelPool(masterName, Sentinels, config, 3000, null); Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); System.out.println(jedis.set("sentinel", "zhuge")); System.out.println(jedis.get("sentinel")); } catch (Exception e) { e.printStackTrace(); } finally {// Note that the connection is not closed. In JedisPool mode, Jedis is returned to the resource pool. if (jedis ! = null) jedis.close(); }}}Copy the code

With SpringBoot: Add dependencies:

< the dependency > < groupId > org. Springframework. Boot < / groupId > < artifactId > spring ‐ boot ‐ starter ‐ data ‐ redis < / artifactId > Commons ‐pool2</artifactId> </dependency> </dependency>Copy the code

Core configuration:

server: port: 9090 spring: application: name: redis-demo redis: database: 0 timeout: 3000 sentinel: master: Mymaster Nodes: -192.168.0.60:26379-192.168.0.60:26380-192.168.0.60:26381 Lettuce: Pool: max-idle: 50 min-idle: 10 max-active: 100 max-wait: 1000Copy the code

Related codes:

@RestController public class JedisSentinelIndex { @Autowired private StringRedisTemplate stringRedisTemplate; /** * When a new master node is elected, the client can dynamically sense that the new master node is elected, and the sentinel will publish the message. The client is immediately aware of the new master, To dynamically switch access masterIP * @interruptedexception */ @requestMapping ("/test_sentinel") public void testSentinel() throws InterruptedException { int i = 1; while (true) { try { stringRedisTemplate.opsForValue().set("zhuge" + i, i + ""); System.out.println(" set key: "+ "zhuge" + I); i++; Thread.sleep(1000); } catch (Exception e) { System.out.println(e); }}}}Copy the code