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 ...
随机推荐
- win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please install the Redistributable then run this installer again 的解决办法
本文地址:http://www.cnblogs.com/jying/p/7764147.html 转载请注明出处. 安装过程其实挺简单,基本上下一步下一步,可以参考我的另一篇mysql安装文章: ...
- ASP.NET 三级联动
三级联动就是用三个下拉列表框DropDownList,每个里面添加相应的东西,在第一个列表框中选择一个值,第二三个列表框都会根据第一个选择进行相应的变化,在第二个列表框中选择一个值,第三个列表框也会根 ...
- tomcat修改上下文path
server.xml <Host name="localhost" appBase="webapps" unpackWARs="true&quo ...
- whatweb工具
WhatWeb WhatWeb可以用来确定服务器使用的CMS.博客平台.统计分析软件包.JavaScript库等.这个工具有超过900个插件用来扫描目标. 使用方法: root@root:/pente ...
- Redis原子计数器incr
一.前言在一些对高并发请求有限制的系统或者功能里,比如说秒杀活动,或者一些网站返回的当前用户过多,请稍后尝试.这些都是通过对同一时刻请求数量进行了限制,一般用作对后台系统的保护,防止系统因为过大的流量 ...
- centos mongodb
cd到mongodb目录下的bin文件夹,执行命令./mongo 运行如下: [root@namenode mongodb]# ./bin/mongo MongoDB shell version: 1 ...
- 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)
Impala使用的端口 Impala使用下表中列出的TCP端口.在部署Impala之前,请确保在每个系统上打开这些端口. Component Service Port Access Requireme ...
- Choosing the Type at Runtime
[Choosing the Type at Runtime] You cannot use a general expression as the React element type. If you ...
- Javascript Iterator
[Javascript Iterator] 1.@@iterator Whenever an object needs to be iterated (such as at the beginning ...
- sql注入(一)
SELECT * FROM users WHERE user='uname' AND password='pass' SELECT * FROM users WHERE user='name' AND ...