redis的sentinel主从切换(failover)与Jedis线程池自动重连
本文介绍如何通过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();
}
}
redis的sentinel主从切换(failover)与Jedis线程池自动重连的更多相关文章
- redis - 主从复制与主从切换
redis2.8之前本身是不支持分布式管理的,一般建议使用redis3.0及以后版本 redis主从切换的方法 keepalive 或者 使用sentinel线程管理 说明如何使用sentinel实 ...
- sentinel主从切换技术
主从切换技术的方法是: 当主服务器宕机后,需要手动把一台从服务器切换为主服务器, 这就需要人工干预,费事费力,还会造成一段时间内服务不可用. 这不是一种推荐的方式,更多时候,我们优先考虑哨兵模式. 一 ...
- Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接
上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 一.步骤如下: 1.配 ...
- (转)基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案
转载自:http://warm-breeze.iteye.com/blog/2020413 本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedi ...
- 基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)
本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...
- 基于Redis Sentinel的Redis集群(主从&Sharding)高可用方案
本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...
- mycat(读写分离、负载均衡、主从切换)
博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 1.环境准备 1.1新增两台虚拟机 mycat01:192.168.247.81 mycat02:192.168.247 ...
随机推荐
- oop6 栈 界面
作业要求 本次作业要求实现核心算法,请将表达式生成的代码及相关的检验.计算表达式结果的代码贴在博客中,并对代码进行必要的解释. 发表一篇博客,博客内容为:提供本次作业的github链接,本次程序运行的 ...
- 201521123008《Java程序设计》第八周实验总结
1. 本周学习总结 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 1.删除元素的时候从最后一个元素开始,避免删除元素后位置发生变化而导致有些元素没有删 ...
- 201521123085《Java程序设计》第4周学习总结
1.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 2.书面作业 Q1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) Q2.面向对 ...
- 201521123037 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. java异常继承架构 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果( ...
- Mysql中的like模糊查询
MySql的like语句中的通配符:百分号.下划线和escape %代表任意多个字符 _代表一个字符 escape,转义字符后面的%或_,使其不作为通配符,而是普通字符匹配 数据库数据如下: 1. ...
- 嵌入系统squashfs挂载常见问题总结
由于squahsfs的一些优点,嵌入系统常常直接使用squashfs作为initrd挂载到/dev/ram,作为rootfs.这里对常见的一些问题进行一些分析. 1. kernel启动出现错误 RAM ...
- 关于Linux的虚拟内存管理
在linux中可以通过free指令查看当前内存,在后面加-m参数能让数字单位显示为MB. 一般机器,有一个实际内存和一个虚拟内存. swap就是虚拟内存,这个虚拟内存可以是文件,也可以是磁盘分区.通常 ...
- Laravel的Nginx重写规则完整代码
laravel基本重写规则 location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php?$qu ...
- jQuery中的常用内容总结(一)
jQuery中的常用内容总结(一) 前言 不好意思(✿◠‿◠),由于回家看病以及处理一些其它事情耽搁了,不然这篇博客本该上上周或者上周写的:同时闲谈几句:在这里建议各位开发的童鞋,如果有疾病尽快治 ...
- HTML 4.01+5基礎知識
HTML 4.01+5 1.Html結構:html>head+body 2.Html快捷鍵:!加Tab(在sublime中) 3.雙標籤: ①常用標籤 h1.h2.h3.h4.h5.h6 p.c ...