您好,欢迎来到要发发知识网。
搜索
您的当前位置:首页Redis 主从、哨兵Sentinel、Jedis

Redis 主从、哨兵Sentinel、Jedis

来源:要发发知识网

 

Redis 主从、哨兵Sentinel、Jedis

版权声明:本文为博主原创文章,遵循版权协议,转载请附上原文出处链接和本声明。
本文链接:

上篇说到了。今天来看一看Redis的主从复制、Sentinel;


一、主从复制

1. 配置

Master上修改redis.conf

// 不想用密码,所以把保护模式设置为no
protected-mode no
// 其实master上不需要配置什么,这里只是取消了保护模式
  • 1
  • 2
  • 3

Slave1Slave2上修改redis.conf

// 同样关闭保护模式
protected-mode no
// 设置本机是谁的slave
slaveof master的ip 6379 // 当配置了slaveof后,下面这条控制本机只能读 slave-read-only yes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. Jedis操作

    // 简单设置3个连接池
    private static final JedisPool masterPool;
    private static final JedisPool slavePool1; private static final JedisPool slavePool2; static { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 最多可以有10个连接 jedisPoolConfig.setMaxTotal(10); jedisPoolConfig.setMaxIdle(5); jedisPoolConfig.setMinIdle(5); masterPool = new JedisPool(jedisPoolConfig, "111.111.111.111"); slavePool1 = new JedisPool(jedisPoolConfig, "111.111.111.112"); slavePool2 = new JedisPool(jedisPoolConfig, "111.111.111.113"); } public static void main(String[] args) throws Exception { // 简单使用,通过try-with-resource try (Jedis jedis = masterPool.getResource()) { jedis.get("key1"); } catch (Exception e) { e.printStackTrace(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3. 主从的意义


一个企业级系统最重要的指标就是“可用性”和“高性能”。

显然,上面的主从复制、读写分离能够简单的提供“高性能”,但也只是提升了“读”的性能,并不能扩展“写”。(写的扩展这里暂且不表)

另一方面,“可用性”也是极其重要的。如上结构可用性并不高,一旦Master宕机则Redis将立即不可写,Slave将只剩下旧数据,系统随即不可用。 
必然的,Redis提供了高可用(High Availability)方案,其中之一就是Sentinel-哨兵。


二、Sentinel - 高可用

1. 什么是Sentinel?

见名知意,它是Redis提供的哨兵程序,它是分布式程序,可以这样描述它们:

Sentinel的主要工作如下:

  1. 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。

  2. 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。

2. 配置Sentinel

哨兵应该被放在的服务器上,最好最少应该有3个哨兵(3台服务器)。

  1. 配置文件sentinel.conf

    // 26379模式是sentinel的运行端口,6379是redis-server的
    port 26379
    // 作为守护进程
    daemonize yes
    // 工作目录,设置到你统一规划的地方
    dir /tmp
    // log文件 dir /.... // protected-mode必须要设置的,不设置不行 protected-mode no // 配置监视的Master,注意无需配置其Slave,Sentinel会自己去询问Master // sentinel monitor master-name ip redis-port quorum // quorum 哨兵们认为master客观死亡(Objectively Down)所需要的法定人。无论是否设置这个值,想要启动failover都必须有多数哨兵同意 sentinel monitor yewu01 127.0.0.1 6379 2 // 如果30000 ms后master还是不回应,就说明Master处于主观死亡(Subjectively Down) sentinel down-after-milliseconds yewu01 30000 // 当发生failover的同时,1个slave开始与新master进行同步。意思是:此slave开始接收master的RDB文件而不能对外提供服务了,而其它slave还能对外服务(具体能否对外服务看第二个//),所以越少意味着redis能越快恢复对外服务 // 同时还要搭配配置slave的redis.conf中的 slave-serve-stale-data参数,指定是否可用过期数据 sentinel parallel-syncs yewu01 1 // 执行failover多久算failover超时 sentinel failover-timeout yewu01 180000
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  2. 运行sentinel 
    虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器, 你可以在启动一个普通 Redis 服务器时通过给定 --sentinel 选项来启动 Redis Sentinel 。

    // 1. 用redis-server加参数 --sentinel的方式启动
    redis-server /path/to/sentinel.conf --sentinel // 2. 用redis-sentinel 方式启动 redis-sentinel /path/to/sentinel.conf //注意!注意!注意!不要忘记在防火墙添加端口,我的是CentOS7,所以如下 firewall-cmd --zone=public --add-port=26379/tcp --permanent 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  3. 查看状态 
    redis-cli -h <hostname> -p 26379登录到Sentinel;

    Sentinel 可接受的命令():

    • PING :返回 PONG 。
    • SENTINEL masters :列出所有被监视的主服务器,以及这些主服务器的当前状态。
    • SENTINEL master <master name>:特定主服务器的当前状态。
    • SENTINEL slaves <master name>:列出给定主服务器的所有从服务器,以及这些从服务器的当前状态。
    • SENTINEL sentinels <master name> Show a list of sentinel instances for this master, and their state.
    • SENTINEL get-master-addr-by-name <master name> : 返回给定名字的主服务器的 IP 地址和端口号。 如果这个主服务器正在执行故障转移操作, 或者针对这个主服务器的故障转移操作已经完成, 那么这个命令返回新的主服务器的 IP 地址和端口号。
    • SENTINEL reset <pattern>: 重置所有名字和给定模式 pattern 相匹配的主服务器。 pattern 参数是一个 Glob 风格的模式。 重置操作清楚主服务器目前的所有状态, 包括正在执行中的故障转移, 并移除目前已经发现和关联的, 主服务器的所有从服务器和 Sentinel 。
    • SENTINEL failover : 当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移 (不过发起故障转移的 Sentinel 会向其他 Sentinel 发送一个新的配置,其他 Sentinel 会根据这个配置进行相应的更新)。
  4. sentinel.conf配置被改变

    每当一个Sentinel启动后,它就会修改并通知其它Sentinel同时修改自身的sentinel.conf文件,例如:

    生成一个myid

    sentinel myid 0f9bd55b18aaa5f5efc6fb7b3371da56d48d4a
    • 1

    文件最后会加上如下:

    
    # Generated by CONFIG REWRITE
    
    sentinel known-sentinel yewu01 192.168.0.1 26379 58a141a0f97669925bcc84e3a3b3dbc8602dea99
    sentinel known-sentinel yewu01 192.168.0.2 26379 a0fbf10df21374f8b5cac1f410d9df3b26618575 sentinel current-epoch 0
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

3. failover - 故障转移

  1. 执行pkill redis-sever关掉Master观察Sentinel日志如下:

    这是一个被选为failover执行者的sentinel的日志,英文挺清晰明了的就不翻译了:

    80:X 14 Feb 19:46:.746 # +sdown master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:.798 # +odown master yewu01 10.173.244.98 6379 #quorum 3/2 80:X 14 Feb 19:46:.798 # +new-epoch 1 80:X 14 Feb 19:46:.798 # +try-failover master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:.807 # +vote-for-leader 214bd3df8363327488cd8c430166cf48cd2ab33a 1 80:X 14 Feb 19:46:.819 # f7462491e6881da2c1efbfd65ece6380c653cf voted for 214bd3df8363327488cd8c430166cf48cd2ab33a 1 80:X 14 Feb 19:46:.823 # 6c95942bbcc39a0703ec5da76d6a696a500a17 voted for 214bd3df8363327488cd8c430166cf48cd2ab33a 1 80:X 14 Feb 19:46:.907 # +elected-leader master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:.907 # +failover-state-select-slave master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:.974 # +selected-slave slave 10.174.249.145:6379 10.174.249.145 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:.974 * +failover-state-send-slaveof-noone slave 10.174.249.145:6379 10.174.249.145 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:55.057 * +failover-state-wait-promotion slave 10.174.249.145:6379 10.174.249.145 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:55.534 # +promoted-slave slave 10.174.249.145:6379 10.174.249.145 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:55.534 # +failover-state-reconf-slaves master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:55.581 * +slave-reconf-sent slave 10.251.22.210:6379 10.251.22.210 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:55.957 # -odown master yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:56.041 * +slave-reconf-inprog slave 10.251.22.210:6379 10.251.22.210 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:56.042 * +slave-reconf-done slave 10.251.22.210:6379 10.251.22.210 6379 @ yewu01 10.173.244.98 6379 80:X 14 Feb 19:46:56.096 # +failover-end master yewu01 10.173.244.98 6379 80:X 14 Feb 19

转载于:https://www.cnblogs.com/developer-ios/p/11441116.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务