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. excel==>csv==via phpmyadmin (edit php.ini & my.ini)==> MySQL Database

    正如同标题, 标题的顺序是 先从Excel表单,保存为csv文档. 步骤: 1.这个可以用linux下的libra office打开 abc.xls 2.用libra office 将 abc.xls ...

  2. http学习笔记(3)

    几乎所有的http通信都是由TCP/IP承载的.http好比一辆汽车,而TCP是一条公路,所有的汽车都要在公路上跑,看看http是如何在tcp这条公路上往返的. 首先简单地看看tcp,TCP连接是通过 ...

  3. asp.net MVC 路由机制

    1:ASP.NET的路由机制主要有两种用途: -->1:匹配请求的Url,将这些请求映射到控制器 -->2:选择一个匹配的路由,构造出一个Url 2:ASP.NET路由机制与URL重写的区 ...

  4. [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式

    写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...

  5. 人生在于折腾:php实现下载导出xx.tar.gz

    刚接到这样的需求,其实我是拒绝的.我甚至很有耐心地和pm商量,扔个csv不就好了么? pm:对方需要一个csv打包成.tar.gz的包,他们是linux server,这是硬性要求. 然后我开始折腾之 ...

  6. linux vi编辑常用命令

      linux vi编辑常用命令 来源:互联网 作者:佚名 时间:07-10 21:31:14 [大 中 小] linux vi编辑常用命令,需要的朋友可以参考下   vi编辑器中有三种状态模式 1. ...

  7. [ios2] 关于CGBitmapContextCreate【转】

    CGContextRef CGBitmapContextCreate ( void *data, size_t width, size_t height, size_t bitsPerComponen ...

  8. sw代码问题

    1.环境版本问题,myeclipse 配 jdk1.7  不能用jdk1.8 :jdk1.6满足不了代码本身 报错为:The type java.io.ObjectInputStream cannot ...

  9. 获取select选中的值

    $("#CalibrationYear option:selected").text();

  10. git stash让bug来的更猛烈些吧

    git stash可以用来暂存当前正在进行的工作,比如想pull最新的代码,又不想加新commit, 或者有一个紧急的bug需要修复,但是这个bug又与你已经在做的工作(还没完成)有关联.这个时候有的 ...