对于生产环境,高可用是避免不了要面对的问题,不管什么环境、服务,只要用于生产,就须要知足高可用;此文针对的是redis的高可用。html
接下来会有系列文章,该系列是对spring-session实现分布式集群session的共享的完整阐述,同时也引申出缓存的实现;而此篇是该系列的第一篇。java
github地址:https://github.com/youzhibing/redislinux
redis版本:redis-3.0.0git
linux:centos6.7github
ip:192.168.11.202, 一台服务器上搭建搭建所有redis实例,包括数据节点实例以及哨兵(sentinel)实例redis
客户端jedis,基于spring-bootspring
搭建一主二从的主从环境apache
安装很简单,网上资料不少,redis官网也有说明;主要就是3步:解压,make,make installcentos
redis解压后,redis home目录下有redis配置的样例文件,咱们不直接在此文件上就行修改,在redis home目录下新建文件夹master_slave,将配置文件都放于此目录下缓存
master配置文件:redis-6379.conf
port 6379 bind 192.168.11.202 requirepass "myredis" daemonize yes logfile "6379.log" dbfilename "dump-6379.rdb" dir "/opt/soft/redis/data" #如若master设置了认证密码,那么全部redis数据节点都配置上masterauth属性 masterauth "myredis"
slave-1配置文件:redis-6380.conf
port 6380 bind 192.168.11.202 requirepass "myredis" daemonize yes logfile "6380.log" dbfilename "dump-6380.rdb" dir "/opt/soft/redis/data" #如若master设置了认证密码,那么全部redis数据节点都配置上masterauth属性 masterauth "myredis" slaveof 192.168.11.202 6379
slave-2配置文件:redis-6381.conf
port 6381 bind 192.168.11.202 requirepass "myredis" daemonize yes logfile "6381.log" dbfilename "dump-6381.rdb" dir "/opt/soft/redis/data" #如若master设置了认证密码,那么全部redis数据节点都配置上masterauth属性 masterauth "myredis" slaveof 192.168.11.202 6379
以下相关路径须要根据本身的状况进行改动,可能和个人不同
[root@slave1 master_slave]# cd /opt/redis-3.0.0/master_slave/ [root@slave1 master_slave]# ./../src/redis-server redis-6379.conf [root@slave1 master_slave]# ./../src/redis-server redis-6380.conf [root@slave1 master_slave]# ./../src/redis-server redis-6381.conf
确认主从关系
[root@slave1 master_slave]# ./../src/redis-cli -h 192.168.11.202 -p 6379 -a myredis info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.11.202,port=6380,state=online,offset=393,lag=0 slave1:ip=192.168.11.202,port=6381,state=online,offset=393,lag=0 master_repl_offset:393 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:392
还能够从从节点视角来看:[root@slave1 master_slave]# ./../src/redis-cli -h 192.168.11.202 -p 6380 -a myredis info replication
如若进行顺利,按如上配置,一主二从环境搭建完毕
主要两个做用:一、做为主节点的一个备份,一旦主节点出现故障,从节点能够做为后备"顶"上来,而且保证数据尽可能不丢失(主从复制是最终一致性);二、从节点能够拓展主节点的能力,一旦主节点不能支撑大并发的读操做,从节点能够在必定程度上帮助主节点分担读压力
一、一旦主节点出现故障,须要手动将一个从节点晋升为主节点,同时须要修改应用方的主节点地址,还须要命令其余从节点去复制新的主节点,整个过程须要人工干预
二、主节点的写能力受到单机的限制
三、主节点的存储能力受到单机的限制
此片主要讲经过sentinel解决上述问题1,问题二、3则在下篇博客进行说明
sentinel-26379.conf
port 26379 daemonize yes logfile "26379.log" dir "/opt/soft/redis/data" sentinel monitor mymaster 192.168.11.202 6380 2 #redis数据master节点设置了认证,则须要以下配置 sentinel auth-pass mymaster myredis sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
sentinel-26380.conf
port 26380 daemonize yes logfile "26379.log" dir "/opt/soft/redis/data" sentinel monitor mymaster 192.168.11.202 6380 2 #redis数据master节点设置了认证,则须要以下配置 sentinel auth-pass mymaster myredis sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
sentinel-26381.conf
port 26381 daemonize yes logfile "26379.log" dir "/opt/soft/redis/data" sentinel monitor mymaster 192.168.11.202 6380 2 #redis数据master节点设置了认证,则须要以下配置 sentinel auth-pass mymaster myredis sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
[root@slave1 master_slave]# ./../src/redis-sentinel sentinel-26379.conf [root@slave1 master_slave]# ./../src/redis-sentinel sentinel-26380.conf [root@slave1 master_slave]# ./../src/redis-sentinel sentinel-26381.conf
[root@slave1 master_slave]# ./../src/redis-cli -h 192.168.11.202 -p 26379 info Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 master0:name=mymaster,status=ok,address=192.168.11.202:6379,slaves=2,sentinels=3
Redis Sentinel 最终拓扑结构
手动停掉6379实例或者kill掉6379实例
[root@slave1 master_slave]# ./../src/redis-cli -h 192.168.11.202 -p 6379 -a myredis
192.168.11.202:6379> shutdown
查看26379.log
master节点已经自动切换到192.168.11.202:6380了,重启6379,6379则是6378的从节点了。
[root@slave1 master_slave]# ./../src/redis-cli -h 192.168.11.202 -p 6380 -a myredis 192.168.11.202:6380> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.11.202,port=6381,state=online,offset=80802,lag=1 slave1:ip=192.168.11.202,port=6379,state=online,offset=80802,lag=1 master_repl_offset:80945 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:80944
基于spring-boot开发,spring-boot-test测试, 这二者本文不作说明,网上资料不少,不熟悉的自行去补充; 工程结构以下图
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lee</groupId> <artifactId>redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>redis</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 日志过滤器 --> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> </dependency> </dependencies> </project>
redis-sentinel.properties
redis.masterName=mymaster redis.sentinels=192.168.11.202:26379,192.168.11.202:26380,192.168.11.202:26381 redis.timeout=10000 #链接master须要用到的密码,若是redis数据节点开启了链接认证 redis.password=myredis # 链接池 # 链接池最大链接数(使用负值表示没有限制) redis.pool.maxActive=150 # 链接池中的最大空闲链接 redis.pool.maxIdle=10 # 链接池中的最小空闲链接 redis.pool.minIdle=1 # 获取链接时的最大等待毫秒数,小于零:阻塞不肯定的时间,默认-1 redis.pool.maxWaitMillis=3000 # 每次释放链接的最大数目 redis.pool.numTestsPerEvictionRun=50 # 释放链接的扫描间隔(毫秒) redis.pool.timeBetweenEvictionRunsMillis=3000 # 链接最小空闲时间(毫秒) redis.pool.minEvictableIdleTimeMillis=1800000 # 链接空闲多久后释放, 当空闲时间>该值 且 空闲链接>最大空闲链接数 时直接释放(毫秒) redis.pool.softMinEvictableIdleTimeMillis=10000 # 在获取链接的时候检查有效性, 默认false redis.pool.testOnBorrow=true # 在空闲时检查有效性, 默认false redis.pool.testWhileIdle=true # 在归还给pool时,是否提早进行validate操做 redis.pool.testOnReturn=true # 链接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true redis.pool.blockWhenExhausted=true
RedisConfig.java
package com.lee.redis.config; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisSentinelPool; @Configuration @PropertySource("redis/redis-sentinel.properties") public class RedisConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class); @Value("${redis.masterName}") private String masterName; @Value("${redis.sentinels}") private String sentinels; @Value("${redis.timeout}") private int timeout; @Value("${redis.password}") private String password; @Value("${redis.pool.maxActive}") private int maxTotal; @Value("${redis.pool.maxIdle}") private int maxIdle; @Value("${redis.pool.minIdle}") private int minIdle; @Value("${redis.pool.maxWaitMillis}") private long maxWaitMillis; @Value("${redis.pool.numTestsPerEvictionRun}") private int numTestsPerEvictionRun; @Value("${redis.pool.timeBetweenEvictionRunsMillis}") private long timeBetweenEvictionRunsMillis; @Value("${redis.pool.minEvictableIdleTimeMillis}") private long minEvictableIdleTimeMillis; @Value("${redis.pool.softMinEvictableIdleTimeMillis}") private long softMinEvictableIdleTimeMillis; @Value("${redis.pool.testOnBorrow}") private boolean testOnBorrow; @Value("${redis.pool.testWhileIdle}") private boolean testWhileIdle; @Value("${redis.pool.testOnReturn}") private boolean testOnReturn; @Value("${redis.pool.blockWhenExhausted}") private boolean blockWhenExhausted; @SuppressWarnings({ "unchecked", "rawtypes" }) @Bean public JedisSentinelPool jedisSentinelPool(JedisPoolConfig poolConfig) { LOGGER.info("sentinel set : {}", sentinels); Set sentinelSet = new HashSet(Arrays.asList(sentinels.split(","))); JedisSentinelPool jedisPool = new JedisSentinelPool(masterName, sentinelSet, poolConfig, timeout, password); return jedisPool; } @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(maxTotal); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMinIdle(minIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun); jedisPoolConfig .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); jedisPoolConfig .setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); jedisPoolConfig .setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis); jedisPoolConfig.setTestOnBorrow(testOnBorrow); jedisPoolConfig.setTestWhileIdle(testWhileIdle); jedisPoolConfig.setTestOnReturn(testOnReturn); jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted); return jedisPoolConfig; } }
Application.java
package com.lee.redis; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); // 是否打印banner // app.setApplicationContextClass(); // 指定spring应用上下文启动类 app.setWebEnvironment(false); app.run(args); } }
RedisTest.java
package com.lee.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisSentinelPool; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class RedisTest { private static final Logger LOGGER = LoggerFactory.getLogger(RedisTest.class); @Autowired private JedisSentinelPool sentinelPool; @Test public void getNameTest() { Jedis jedis = null; try { jedis = sentinelPool.getResource(); String name = jedis.get("name"); System.out.println("name is " + name); } catch(Exception e) { LOGGER.error(e.getMessage(), e); } finally { if (jedis != null) { jedis.close(); } } } }
更多详情请上个人github
运行RedisTest.java的getNameTest方法(name属性已经在redis中设置,没设置的须要提早设置),获得结果:
一、有人可能会有这样的疑问:为何经过sentinel来获取redis的链接,而不是直接链接master来获取redis链接呢?
试想一下,客户端直接经过master节点获取redis链接,若是master节点挂掉了,虽然Redis Sentinel能够完成故障转移,可是客户端没法获取这个变化,那么客户端就没法获取redis链接了;
最了解master节点信息的就是Sentinel节点集合,因此经过sentinel来获取redis链接就能知足高可用的要求了。
二、redis master的故障转移不影响客户端链接代码, 可是转移期间内,经过sentinel是获取不到主节点的链接的, 由于转移期间内master节点还没被选举出来;
《Redis开发与运维》
http://www.redis.cn/topics/sentinel.html