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 ...
随机推荐
- deb 和 rpm 后缀文件 区别和安装
https://blog.csdn.net/u010977122/article/details/52986217 下载一个ATOM 的deb的安装包
- python类和对象的底层实现
按照python中"一切皆对象的原理",所有创建的对象,都是一个已知存在的class实例化的结果;那么class又是被哪个"类"实例化的呢?先看下面的一段代码 ...
- ArcGIS案例学习笔记-点群密度统计
ArcGIS案例学习笔记-点群密度统计 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:对于点群,统计分布密度 数据: 方法: 1. 生成格网 2. 统计个数, ...
- MOSS 2007 错误0x80040E14解决
今天公司内网莫名的出现错误,只能新建列表条目,不能创建网站,到后来列表条目也不能创建了,一直报0x80040E14错误.于是Google一把,搜索这个错误号,然后在apearce 的Blog找到了原因 ...
- OpenCV SVM
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ope ...
- css-background-image 背景图片太大或太小
.zoomImage { background-image:url(images/yuantiao.jpg); background-rep ...
- backdoor-factory
启动backdoor-factory 寻找大于100字节的代码洞 执行的结果 查看适合的payload程序 iat_reverse_tcp_stager_threaded分片段注入方式 使用这种注入方 ...
- JSF web.xml的各类参数属性配置
出处:http://www.cnblogs.com/zxpgo/articles/2570175.html 感谢作者的分享!! ———————————————————————————————————— ...
- 用两个栈实现队列(python)
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. # -*- coding:utf-8 -*- class Solution: def __init__( ...
- Shell教程 之传递参数
1.Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字, 0 为执行的文件名,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数 ...