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.配 ...
随机推荐
- .Net程序员面试试题
1:简述private protected public internal修饰符的访问权限? private:私有成员在类的内部可以访问: protected:受保护的成员,在类的内部和继承类中可以访 ...
- 持续集成环境(Hudson)搭建
持续集成环境(Hudson)搭建 这是在公司写的,公司要求用英文,我也没时间翻译了.还请见谅! Hudson是个非常强大持续集成工具,配合svn,maven,sonar,redmine工具就更加完美了 ...
- Reeder Web版
Reeder Web版 访Reeder界面效果 一直很欣赏触控手势的代码实现,所以最近折腾了个Javascript触控手势库--JTouch,效果还有诸多不完善之处,苦于硬件设备不完善,针对ie10的 ...
- CoreCRM 开发实录 —— 单元测试、测试驱动开发和在线服务
测试不是问题,问题是怎么测试. ## 单元测试 我认为单元测试已经是无可争议的最佳开发实践之一.但是很多人并不同意这个观点.他们的说法无非是:写测试需要花很多时间,需求又经常变动,一但变动,一大片测试 ...
- toFixed()要注意的
toFixed()是Number类型的一种方法,该方法是保留四舍五入取指定位数的小数点,但最终返回值类型是字符串.所以,刚开始用该方法的人要注意不要在计算时使用到该方法.
- ASP.NET DropDownList FindByValue 未将对象引用设置到对象的实例 解决方法
1.粗心把DataValueField中的字段名称写错了. 2.把DataBind()写在了FindByValue()后面了,以下代码是正确顺序: BLL.Student bllStu = new B ...
- 从Chrome源码看浏览器如何构建DOM树
.aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } p { font-size: 1 ...
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- 设计模式(二)单例模式Singleton(创建型)
几乎所有面向对象的程序中,总有一些类的对象需要是唯一的,例如,通过数据库句柄到数据库的连接是独占的.您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销.再如大家最经常用的IM, ...
- USB调试不能弹出授权窗口 unauthorized 的解决办法
今天把 AndroidStudio 1.5 给卸载了,重新安装 2.1版本,据说速度快了很多,结果手机一直没法授权调试,授权USB调试信任PC设备的窗口始终没看到,网上找了好多方法,什么驱动.重启.各 ...