redis - 主从复制与主从切换
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 - 主从复制与主从切换的更多相关文章
- redis主从复制、主从延迟知几何
本片章节主要从 redis 主从复制延迟相关知识及影响因素做简要论述. 1.配置:repl-disable-tcp-nodelay 也即是TCP 的 TCP_NODELAY 属性,决定数据的发送时机. ...
- redis的sentinel主从切换(failover)与Jedis线程池自动重连
本文介绍如何通过sentinel监控redis主从集群,并通过jedis自动切换ip和端口. 1.配置redis主从实例 10.93.21.21:6379 10.93.21.21:6389 10.93 ...
- Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- redis的主从复制,读写分离,主从切换
当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. redis提供了一个master,多个s ...
- Redis哨兵模式(sentinel)部署记录(主从复制、读写分离、主从切换)
部署环境: CentOS7.5 192.168.94.11 (master) 192.168.94.22 (slave0) 192.168.94.33 (slave1) 192.168.94.44 ...
- redis的主从复制(读写分离)/哨兵(主从切换)配置
准备两个redis服务,两台机器,依次命名文件夹子master,slave1 10.10.10.7 10.10.10.8 1.master修改配置文件 [root@db2 conf]# cat 637 ...
- redis的主从复制和哨兵支持的主从切换
1 主从复制的目的是为了读写分离 master写,然后同步到slave,slave只管读. 2 哨兵存在的目的 是为了主从切换,如果master挂了,那么一个slave成为master,重启之后的ma ...
- Redis集群(九):Redis Sharding集群Redis节点主从切换后客户端自动重新连接
上文介绍了Redis Sharding集群的使用,点击阅读 本文介绍当某个Redis节点的Master节点发生问题,发生主从切换时,Jedis怎样自动重连新的Master节点 一.步骤如下: 1.配 ...
随机推荐
- ice 有道德的黑客!
ice 有道德的黑客! 在开始正文之前,请帮忙为当前 排名前 10 唯一的 .Net 开源软件 FineUI 投一票: 投票地址: https://code.csdn.net/2013OSSurve ...
- C语言面试题汇总
1. 阅读下面程序并写出输出结果(10分). main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d, ...
- 关于三星设备 Activity.onDestroy() 被调用。显示“开发者选项”
昨天在三星的Galaxy s4上测试自己的App时,在Activity页面间跳转的时候,第一个Activity的onDestory()总是被调用,导致从第二个Activity返回的时候,第一个Acti ...
- vs2012中程序集生成无法自动在网站Bin目录下生成Dll文件?(已解决!)
最近,突然发现生成程序集后,网站bin目录下dll没有更新,也没有自动生成dll文件,通过近半个小时的摸索和实验,找到了解决方法: 1.右键网站,不是项目,选择[属性页],在左侧[引用]中如果没有,就 ...
- LaTeX入门教程
LaTeX(LATEX,音译"拉泰赫")是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用 ...
- js如何判断是否在iframe中
JS代码://方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME") { alert ...
- selenium自动化过程中遇到的小问题(未完待续)
1.chrome浏览器调用不起来 代码没出错的情况下,检查下chrome浏览器的版本与chromedriver.exe的版本是否匹配;下面的表格是根据网上及官网整理的chromedriver与chro ...
- 第10章 使用MySQL数据库
1.在数据库中插入数据:INSERT语句: 如://插入一整行: insert into customers values (NULL,'-','-','-'), - ; //插入一行中指定的列内容: ...
- java 的序列化
(1) 首先是java自己内部实现的对象序列化机制 其实就是ObjectInputStream 和 ObjectOutputStream 首先实现一个实体对象 记住必须实现Serializable ...
- Win32窗口
#include <Windows.h> #include <CommCtrl.h> #pragma comment(lib, "comctl32.lib" ...