redis2.8之前本身是不支持分布式管理的,一般建议使用redis3.0及以后版本

redis主从切换的方法 keepalive  或者 使用sentinel线程管理

说明如何使用sentinel实现主从管理(在已经配置好主从并启动的前提下)

1、sentinel.conf 配置文件

 # Example sentinel.conf

 # port <sentinel-port>
# The port that this sentinel instance will run on
port
protected-mode no # sentinel announce-ip <ip>
# sentinel announce-port <port>
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4 # dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir "/tmp" # sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z - and the three characters ".-_".
sentinel myid 920ad7dac87a4c853bbdf6417578e53ce261bdec # sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
sentinel monitor mymaster 192.168.91.233 # sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is seconds.
sentinel failover-timeout mymaster sentinel auth-pass mymaster
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
#sentinel config-epoch mymaster #sentinel leader-epoch mymaster
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
# already tried against the same master by a given Sentinel, is two
# times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
# to a Sentinel current configuration, to be forced to replicate
# with the right master, is exactly the failover timeout (counting since
# the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
# did not produced any configuration change (SLAVEOF NO ONE yet not
# acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
# reconfigured as slaves of the new master. However even after this time
# the slaves will be reconfigured by the Sentinels anyway, but not with
# the exact parallel-syncs progression as specified.
#
# Default is minutes.
sentinel config-epoch mymaster
#sentinel known-slave mymaster 192.168.91.233 # SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "" the execution is retried later (up to a maximum
# number of times currently set to ).
#
# If script exits with "" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code .
#
# A script has a maximum running time of seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried. # NOTIFICATION SCRIPT
#
# sentinel notification-script <master-name> <script-path>
#
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh # CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script <master-name> <script-path>
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#
# <state> is currently always "failover"
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh # Generated by CONFIG REWRITE
#sentinel current-epoch sentinel leader-epoch mymaster
sentinel known-slave mymaster 192.168.91.234
sentinel current-epoch

seninel.conf

此配置文件为实际运行的,最后的一些配置是sentinel自动写进去的,而不是配置的。

值得注意的是,需要配置protected mode为no

sentinel monitor mymaster 这个参数不要用默认的127.0.0.1  使用真实的IP地址

部分参数,在样例中是有的,但在这个配置文件中注释掉的

2、接下来启动sentinel

  ./src/redis-sentinel sentinel.conf &

3、登录sentinel

  ./src/redis-cli -h 192.168.91.234 -p 26379

  查看主从信息:info

  关键是最后一句:master0:name=mymaster,status=ok,address=192.168.91.234:6379,slaves=1,sentinels=1

  status 是ok就是可用的;

  sdown 是主观不可用,这种情况下;当足够多的sentinel检测到master为sdown后,就会将master 置为odown

  odown 是客观不可用。

4、客户端开发,使用jedis的JedisSentinelPool连接池

  需要引入:commons-pool2-2.4.2.jar  和   jedis-2.8.1.jar包

  代码如下:

import java.util.HashSet;
import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool; public class JedisSentinel { @Test
public final void test() {
String masterName="mymaster";
//sentinel地址集合
Set<String>set=new HashSet<String>();
set.add("192.168.91.234:26379");
GenericObjectPoolConfig gPoolConfig=new GenericObjectPoolConfig();
gPoolConfig.setMaxIdle(10);
gPoolConfig.setMaxTotal(10);
gPoolConfig.setMaxWaitMillis(10);
gPoolConfig.setJmxEnabled(true);
JedisSentinelPool jSentinelPool=new JedisSentinelPool(masterName,set,gPoolConfig);
//------------------------------------------
Jedis jedis=null;
jedis=jSentinelPool.getResource();
jedis.auth("123456");
//jedis.set("key1", "value2");
String key1 = jedis.get("key1");
System.out.println(key1);
jedis.close();
} }

  此时,当一个主redis挂掉之后,sentinel线程会改变从redis的配置文件,并使其成为主redis。当主redis启动后,会变为从redis.

  sentinel可以配置多个,配置文件没有区别,客户端连接略有区别!

  这样就实现了redis的主从复制、主从切换!

  最简配置如下:

  

port
protected-mode no dir /tmp sentinel monitor mymaster 192.168.37.101 sentinel down-after-milliseconds mymaster sentinel parallel-syncs mymaster sentinel failover-timeout mymaster

redis - 主从复制与主从切换的更多相关文章

  1. redis主从复制、主从延迟知几何

    本片章节主要从 redis 主从复制延迟相关知识及影响因素做简要论述. 1.配置:repl-disable-tcp-nodelay 也即是TCP 的 TCP_NODELAY 属性,决定数据的发送时机. ...

  2. redis的sentinel主从切换(failover)与Jedis线程池自动重连

    本文介绍如何通过sentinel监控redis主从集群,并通过jedis自动切换ip和端口. 1.配置redis主从实例 10.93.21.21:6379 10.93.21.21:6389 10.93 ...

  3. Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)

    Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...

  4. [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)

    Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...

  5. redis的主从复制,读写分离,主从切换

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. redis提供了一个master,多个s ...

  6. Redis哨兵模式(sentinel)部署记录(主从复制、读写分离、主从切换)

    部署环境: CentOS7.5  192.168.94.11 (master) 192.168.94.22 (slave0) 192.168.94.33 (slave1) 192.168.94.44 ...

  7. redis的主从复制(读写分离)/哨兵(主从切换)配置

    准备两个redis服务,两台机器,依次命名文件夹子master,slave1 10.10.10.7 10.10.10.8 1.master修改配置文件 [root@db2 conf]# cat 637 ...

  8. redis的主从复制和哨兵支持的主从切换

    1 主从复制的目的是为了读写分离 master写,然后同步到slave,slave只管读. 2 哨兵存在的目的 是为了主从切换,如果master挂了,那么一个slave成为master,重启之后的ma ...

  9. Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接

    上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 ​一.步骤如下: 1.配 ...

随机推荐

  1. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

  2. 反向代理(Reverse Proxy)

    反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...

  3. TCPDump 抓Loopback数据包

    编写网络程序必备截包工具, unix下面自带tcpdump, linux就不用说了.用于排查网络程序的bug,命令行如何使用请百度谷歌.分析包推荐wireshark,可视化非常方便.一般都是在非Win ...

  4. Linux CPU affinity

    在Linux中,我们知道可以通过nice.renice命令改变进程的执行优先级,优先级高的进程优先执行,从而一定程度上保证重要任务的运行. 除了nice.renice外,可以通过CPU  affini ...

  5. Hiberbate中的一对多关联查询

    Hibernate中一对多关系的应用 案例:一个人可以拥有多辆小轿车 目的:通过hibernate的相关配置,利用HQL语句成功的查询出某人拥有某些车辆 1. 项目结构 2. domain类的创建详情 ...

  6. .NET基础——循环、枚举

    1. 循环结构 3种循环语句:while.do-while.for 面对循环我们应当注意: 1. 循环在做什么?(重复做的事情——也就是循环体的内容) 2. 循环的终止条件是什么?(循环条件) 3种循 ...

  7. 结构-行为-样式-Js排序算法之 快速排序

    快速排序算法,是我的算法系列博客中的第二个Js实现的算法,主要思路:    在一个数组中随机取一个数(一般都取第一个或者最后一个),使这个数与数组中其他数进行比较,如果比它大就放到它的右边,比它小就放 ...

  8. Java ArrayList、Vector和LinkedList等的差别与用法(转)

    Java ArrayList.Vector和LinkedList等的差别与用法(转) ArrayList 和Vector是采取数组体式格式存储数据,此数组元素数大于实际存储的数据以便增长和插入元素,都 ...

  9. (转载)python日期函数

    转载于http://www.cnblogs.com/emanlee/p/4399147.html 所有日期.时间的api都在datetime模块内. 1. 日期输出格式化 datetime => ...

  10. Kattis - Aaah!

    Aaah! Photo by Unknown Jon Marius shouted too much at the recent Justin Bieber concert, and now need ...