本文介绍如何通过sentinel监控redis主从集群,并通过jedis自动切换ip和端口。
1、配置redis主从实例
10.93.21.21:6379
10.93.21.21:6389
10.93.21.21:6399
主从同步关系
master:10.93.21.21:6379
slave:10.93.21.21:6389,10.93.21.21:6399
master配置如下:
# 实例ip和端口
bind 10.93.21.21
port
# pid文件
pidfile redis_6379.pid
# 日志文件
logfile "/home/data_monitor/redis-3.2.9/log/6379.log"
# 持久化数据文件dir
dir /home/data_monitor/redis-3.2./data
# rdb 文件地址
dbfilename .rdb
# aof 文件地址
appendfilename "6379.aof"
slave配置如下(以6389为例)
# 实例ip和端口
bind 10.93.21.21
port
# master节点配置
slaveof 10.93.21.21
# pid文件
pidfile redis_6389.pid
# 日志文件
logfile "/home/data_monitor/redis-3.2.9/log/6389.log"
# 持久化数据文件dir
dir /home/data_monitor/redis-3.2./data
# rdb 文件地址
dbfilename .rdb
# aof 文件地址
appendfilename "6389.aof"
启动redis
bin/redis-server conf/.conf
bin/redis-server conf/.conf
bin/redis-server conf/.conf
验证一下
master
$ bin/redis-cli -h 10.93.21.21 -p
10.93.21.21:> info replication
# Replication
role:master
connected_slaves:
slave0:ip=10.93.21.21,port=,state=online,offset=,lag=
slave1:ip=10.93.21.21,port=,state=online,offset=,lag=
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
slave
$ bin/redis-cli -h 10.93.21.21 -p
10.93.21.21:> info replication
# Replication
role:slave
master_host:10.93.21.21
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
2、配置sentinel集群
10.93.21.21:26379
10.93.21.21:26389
10.93.21.21:26399
启动所有的节点,只要监控同一个redis master,启动的话自动连接成集群
# sentinel注册的IP和端口
bind 10.93.21.21
port
# working目录
dir /home/data_monitor/redis-3.2./sentinel
# sentinel监控哪个主节点
sentinel monitor mymaster 10.93.21.21
# 主节点挂掉多长时间,判定为挂掉,开始failover
sentinel down-after-milliseconds mymaster
# failover交由几个sentinel执行
sentinel parallel-syncs mymaster
# failover多长时间没完成,超时失败
sentinel failover-timeout mymaster
启动sentinel集群
bin/redis-sentinel conf/s26379.conf
bin/redis-sentinel conf/s26389.conf
bin/redis-sentinel conf/s26399.conf
3、jedis自动切换ip和端口
先解决依赖:pom.xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
连接池代码
public class JedisPoolUtil { private static JedisSentinelPool pool = null; public static Properties getJedisProperties() { Properties config = new Properties();
InputStream is = null;
try {
is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("cacheConfig.properties");
config.load(is);
} catch (IOException e) {
logger.error("", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
return config;
} /**
* 创建连接池
*
*/
private static void createJedisPool() {
// 建立连接池配置参数
JedisPoolConfig config = new JedisPoolConfig();
Properties prop = getJedisProperties();
// 设置最大连接数
config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE")));
// 设置最大阻塞时间,记住是毫秒数milliseconds
config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT")));
// 设置空间连接
config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE")));
// jedis实例是否可用
boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true;
config.setTestOnBorrow(borrow);
// 创建连接池
// pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 线程数量限制,IP地址,端口,超时时间
//获取redis密码
String password = StringUtil.nullToString(prop.getProperty("PASSWORD")); String masterName = "mymaster";
Set<String> sentinels = new HashSet<String>();
sentinels.add("192.168.137.128:26379");
sentinels.add("192.168.137.128:26380");
sentinels.add("192.168.137.128:26381");
pool = new JedisSentinelPool(masterName, sentinels, config);
} /**
* 在多线程环境同步初始化
*/
private static synchronized void poolInit() {
if (pool == null)
createJedisPool();
} /**
* 获取一个jedis 对象
*
* @return
*/
public static Jedis getJedis() {
if (pool == null)
poolInit();
return pool.getResource();
} /**
* 释放一个连接
*
* @param jedis
*/
public static void returnRes(Jedis jedis) {
pool.returnResource(jedis);
} /**
* 销毁一个连接
*
* @param jedis
*/
public static void returnBrokenRes(Jedis jedis) {
pool.returnBrokenResource(jedis);
} public static void main(String[] args){
Jedis jedis=getJedis(); } }