Master-slave method of switching technology is: When the primary server goes down, you need to manually switch to a primary server from the server, which requires manual intervention, protracted and painstaking, but also cause some time service is unavailable. This is not a recommended way, more often, we give priority to sentry mode .
I. Overview mode Sentinel
Sentinel mode is a special mode, first Redis Sentinel provides command, Sentinel is a separate process, as a process, it will operate independently. The principle is Sentinel by sending a command, the server waits for a response Redis to monitor multiple instances running Redis.
Sentinel has two roles here
-
By sending a command, so that Redis server returns monitor its operational status, including primary and secondary servers.
-
When the sentinel surveillance to master is down, it will automatically switch to the slave master, then publish subscribe model notice from the other server, modify the configuration file, so that they switch hosts.
However, a sentry process of Redis server monitoring, problems may occur, for which we can use multiple sentinel monitoring. But also between the various monitoring sentinel, thus forming a multi-Sentinel mode.
Used here to describe the failover (failover) process. Assume that the primary server goes down, Sentinel 1 to detect this result, the system does not perform failover process immediately, just 1 Sentinel subjective think the primary server is unavailable, this phenomenon is becoming subjective offline . When the latter also detected in sentinel primary server is unavailable, and the number reaches a certain value, it will be a vote between sentinel, the voting result is initiated by a sentinel, a failover operation. After the successful handover, will be released through a subscription model that allows each sentinel to monitor the realization of their own to switch from the host server, this process is called objective offline . For such clients, everything is transparent.
Two, Redis Sentinel configuration mode
Configuration 3 and 1 sentinel from the master server 2 Redis to demonstrate the process.
Service type | Whether it is the primary server | IP addresses | port |
---|---|---|---|
Redis | Yes | 192.168.11.128 | 6379 |
Redis | no | 192.168.11.129 | 6379 |
Redis | no | 192.168.11.130 | 6379 |
Sentinel | - | 192.168.11.128 | 26379 |
Sentinel | - | 192.168.11.129 | 26379 |
Sentinel | - | 192.168.11.130 | 26379 |
First, the main configuration Redis from the server, the file below modify redis.conf
# Makes Redis server across the network access the bind 0.0 . 0.0 # password requirepass " 123456 " # specify the master server Note: For configuration of slaveof only configuration from the server, the primary server does not need to configure slaveof 192.168 . 11.128 6379 # master password, Note: For configuration of slaveof only configuration from the server, the master server does not require configuration masterauth 123456
Redis is disposed above the main server, a plurality slaveof configuration and password from the server than the primary server.
Configuration 3 Sentry, Sentry configuration for each is the same. There is a sentinel.conf Redis file in the installation directory, copy a modification
# Disable protected mode protected - the MODE NO # configure the listening master server, here on behalf of sentinel monitor monitor, on behalf of the server name mymaster, you can customize, 192.168 . 11 .128 representatives to monitor the primary server, 6379 on behalf of the port, 2 for only two two or more sentinel that when the primary server is unavailable, the operation will be failover. Monitor mymaster Sentinel 192.168 . 11.128 6379 2 # Sentinel author - password pass-defined services, mymaster is the service name, Redis server password is 123456 # Sentinel auth -pass <Master-name> <password> Sentinel auth -pass mymaster 123456
Close the protected mode described above, to facilitate testing.
With the above changes, we can enter the src directory of the installation directory Redis, start the server and Sentinel by the following command
# Start the Redis server process . / Redis-Server ../ redis.conf # sentry start the process . / Redis-Sentinel ../sentinel.conf
Note the order to start. The first is the host (192.168.11.128) Redis service of process, and then start the machine from service process, the service process starts last three sentries.
Three, Java is used Sentry mode
/** * 测试Redis哨兵模式 * @author liu */ public class TestSentinels { @SuppressWarnings("resource") @Test public void testSentinel() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(10); jedisPoolConfig.setMaxIdle(5); jedisPoolConfig.setMinIdle(5); // 哨兵信息 Set<String> sentinels = new HashSet<>(Arrays.asList("192.168.11.128:26379", "192.168.11.129:26379","192.168.11.130:26379" )); // create a connection pool JedisSentinelPool the pool = new new JedisSentinelPool ( "mymaster", Sentinels, jedisPoolConfig, "123456" ); // get the client Jedis jedis = pool.getResource (); // execute two command jedis.set ( "MyKey", "MyValue" ); String value = jedis.get ( "MyKey" ); System.out.println (value); } }
The above is through the use of Jedis, it can also be used to configure Spring RedisTemplate use.
<bean id = "poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲数 --> <property name="maxIdle" value="50"></property> <!-- 最大连接数 --> <property name="maxTotal" value="100"></property> <!-- 最大等待时间 --> <property name="maxWaitMillis" value="20000"></property> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> <constructor-arg name="sentinelConfig" ref="sentinelConfig"></constructor-arg> <property name="password" value="123456"></property> </ The JDK serializer<-!>The bean --> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean> <!-- String序列化器 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="keySerializer" ref="stringRedisSerializer"></property> <property name="defaultSerializer" ref="stringRedisSerializer"></property> <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property> </bean> <!-- 哨兵配置 --> <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <!-- 服务名称 --> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="mymaster"></property> </bean> </property> <!-- 哨兵服务IP和端口 --> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.11.128"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.11.129"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.11.130"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> </set> </property> </bean>
Other configuration items IV Sentinel mode
Configuration Item | Parameter Type | effect |
---|---|---|
port | Integer | Sentinel started the process of port |
to you | Folder Contents | Sentinel Process Services temporary folder, the default is / tmp, to ensure that permissions have writable |
sentinel down-after-milliseconds | <Service name> <milliseconds (integer)> | When sentinel surveillance Redis specified service, when a default Redis service can not answer a few milliseconds, a single offline Sentinel that subjective time, the default is 30,000 (30 seconds) |
sentinel parallel-syncs | <Service name> <server number (integer)> | Redis can specify how many new host synchronization service, in general, the smaller the number, the longer the synchronization time, and larger, the higher the requirements for network resources |
sentinel failover-timeout | <Service name> <milliseconds (integer)> | Of failover allowed number of milliseconds, over this time, failover is considered a failure, the default is 3 minutes |
sentinel notification-script | <Service name> <script path> | Examples designated sentinel detected redis examples of the abnormality monitoring points, the script calls the police. The optional configuration items, more commonly |
sentinel down-after-milliseconds configuration items only after a sentinel exceeds the allotted time still did not get a response, I think the host will not be available. For other sentry, I do not think so. Sentinel will record the news, when has considered subjective offline Sentinel reached the configured number of sentinel monitor, will launch a poll, conducted failover, this time will override the Redis Sentinel Sentinel profile to adapt to the new scene
Transfer: https: //www.jianshu.com/p/06ab9daf921d