redis 哨兵(sentinel)
redis哨兵
- 哨兵自动故障转移
- 自动通知应用最新master信息
- 无需担心,master挂了,程序不需要修改IP啥的,由哨兵自动完成
- 修改sentinel.conf
protected-mode no # 默认只允许本机访问sentinel服务
工具类
package redis.client.sentinel; import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool; import java.util.HashSet;
import java.util.Map;
import java.util.Set; /**
* Created by dailin on 2017/7/27.
*/
public class Redis_Sentinel { public static JedisSentinelPool sentinelPool = null;
private static JedisPoolConfig jedisPoolConfig = null; private Redis_Sentinel(){} private static void initSentinelPool(Set<String> sentinels){
synchronized (Redis_Sentinel.class){
if (sentinelPool == null) {
GenericObjectPoolConfig gPoolConfig=new GenericObjectPoolConfig();
gPoolConfig.setMaxIdle(10);
gPoolConfig.setMaxTotal(10);
gPoolConfig.setMinIdle(1);
gPoolConfig.setMaxWaitMillis(10);
gPoolConfig.setJmxEnabled(true);
sentinelPool = new JedisSentinelPool("mymaster",sentinels,gPoolConfig);
}
}
} public static Jedis getJedis(Set<String> sentinels){
initSentinelPool(sentinels);
return sentinelPool.getResource();
}
public static void returnJedis(Jedis jedis){
sentinelPool.returnResource(jedis);
}
}
测试类
import org.junit.Before;
import org.junit.Test;
import redis.client.sentinel.Redis_Sentinel;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis; import java.util.HashSet;
import java.util.List;
import java.util.Set; /**
* Created by dailin on 2017/7/27.
*/
public class TestSentinel {
Set<String> sentinel = null;
@Before
public void init()
{
sentinel = new HashSet<String>();
sentinel.add("192.168.56.130:26379");
sentinel.add("192.168.56.131:26379");
}
@Test
public void testSentinel(){
while(true) {
try{
Jedis jedis = Redis_Sentinel.getJedis(sentinel);
String result = jedis.get("dai");
HostAndPort currentHostMaster = Redis_Sentinel.sentinelPool.getCurrentHostMaster();
System.out.println("master:"+currentHostMaster.getHost()+"-port:"+currentHostMaster.getPort());
Redis_Sentinel.returnJedis(jedis);
System.out.println(result);
}catch (Exception e){
e.printStackTrace();
}
} }
}
这个测试类会一直访问redis,当master被停止后,程序抛出异常,过了一小会新的master被选出,程序又正常执行,所以使用jedis连接redis时,只需要传入sentinel的地址即可,自动在redis的master宕机后,自动更新连接新master信息。
redis 哨兵(sentinel)的更多相关文章
- redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用
目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...
- redis哨兵(Sentinel)、虚拟槽分区(cluster)和docker入门
一.Redis-Sentinel(哨兵) 1.介绍 Redis-Sentinel是redis官方推荐的高可用性解决方案,当用redis作master-slave的高可用时,如果master本身宕机,r ...
- Redis哨兵(sentinel)模式搭建
一.Sentinel介绍 之前骚了一波Redis的简介及应用场景,今天试了下他的哨兵模式: Sentinel是Redis的高可用性(HA)解决方案,由一个或多个Sentinel实例组成的Sentine ...
- Redis 哨兵 Sentinel
Redis Sentinel:redis集群应用,分布式系统. 多个Sentinal进程之间通过 gossip 协议来接收主服务器是否下线的信息,通过 Raft 一致性协议来决定故障转移及转移服务 ...
- redis哨兵sentinel.conf文件
关闭保护模式 //17行 protected-mode no 端口号 //21 port 26379 后台启动 //26 daemonize yes //84行 主机的ip加端口号 2 为票数 sen ...
- Redis容灾部署(哨兵Sentinel)
Redis容灾部署(哨兵Sentinel) 哨兵的作用 1. 监控:监控主从是否正常2. 通知:出现问题时,可以通知相关人员3. 故障迁移:自动主从切换4. 统一的配置管理:连接者询问sentinel ...
- SpringBoot进阶教程(三十)整合Redis之Sentinel哨兵模式
Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时,假如master宕机了,redis本身(以及其很多客户端)都没有实现自动进行主备切换,而 ...
- redis cluster + sentinel详细过程和错误处理三主三备三哨兵
redis cluster + sentinel详细过程和错误处理三主三备三哨兵1.基本架构192.168.70.215 7001 Master + sentinel 27001192.168.70. ...
- Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
随机推荐
- Numpy知识(一)
先了解ndarray(一个多维数组)的生成. 第一种生成方法就是np.random.randn(n,m),这回生成一个形状是n*m的ndarray.如下图. 第二种生成方法:传入一个列表listDem ...
- php常见排序
public function actionQuickSort(){ $arr = ['5', '4', '3', '2', '1', '0']; $quickRes = $this->quic ...
- redis 高级特性 不要太好用
Redis高级特性及应用场景 redis中键的生存时间(expire) redis中可以使用expire命令设置一个键的生存时间,到时间后redis会自动删除它. 过期时间可以设置为秒或者毫秒精度. ...
- spring分页
1.Brand 商品品牌类 public class Brand { private Integer id; private String name; private String descripti ...
- Archlinux 遇到的坑
1.系统更新之后pip炸了,解决方案是用官方的get-pip安装,同时配置文件,避免使用sudo安装 2.grub不如syslinux配置快捷,入了syslinux的坑 3.平铺式桌面搭配快捷键,Hi ...
- 缩点+最小路径覆盖 hdu 3861
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...
- Monkey 命令收集相关 --追加Monkey自动化测试开源工具
.1.环境配置 MONKEY测试使用的是ADB命令,因此只需要配置ADB环境即可. 2.测试准备与执行 在Monkey测试前,必须进行以下准备 Ø 手机屏幕超时设置为30分钟或者永不超时,防止手机进 ...
- 102. Binary Tree Level Order Traversal (Tree, Queue; BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Jmeter cookie不兼容问题
历史脚本,今天准备执行测试,报出这种错误 解决方案:HTTP Cookie Manager里的 Cookie Policy 由rfc2109设置为兼容模式(Compatibility) 参考:http ...
- 对于装office 365时,visio不兼容的解决
先将office 365装好,之后从这个网址下载visio安装即可,但是没有 激活码,需要自己激活: 网址:链接: https://pan.baidu.com/s/1OqONPuJ0eumrpts-X ...