Spring Boot配置Redis Sentinel的例子

栏目: 数据库 · Redis · 发布时间: 4年前

内容简介:本文介绍Spring Boot连接本文关联的源码:拓扑(M代表redis-master,S代表redis-sentinel,R代表redis-slave,C代表Spring Boot Application):

本文介绍Spring Boot连接 Redis Sentinel 的例子。

本文关联的源码: github

基本信息

拓扑(M代表redis-master,S代表redis-sentinel,R代表redis-slave,C代表Spring Boot Application):

+---------------------------+
     |                           |
  +-----+       +-----+       +-----+
  |  M  |-------|  S  |-------|  R  |
  +-----+       +-----+       +-----+
     |             |
     |          +-----+
     +----------|  C  |
                +-----+

application.yaml 配置:

spring:
  redis:
#    host: redis-master
#    port: 6379
    password: abc
    sentinel:
      master: springboot
      nodes:
        - redis-sentinel:26379

注意这里不需要配置master的host和port,这些信息会从Redis Sentinel中得到。

演示步骤

打包并构建镜像: mvn clean install dockerfile:build

进入 docker 目录,执行 docker-compose up -d

观察Spring Boot Application的日志: docker logs -f docker_spring-boot_1 ,会发现每隔3秒执行 INCR foo

07:53:49.205  INFO  hello.Application                    : INCR foo: 1
07:53:52.212  INFO  hello.Application                    : INCR foo: 2
07:53:55.213  INFO  hello.Application                    : INCR foo: 3
07:53:58.216  INFO  hello.Application                    : INCR foo: 4
07:54:01.217  INFO  hello.Application                    : INCR foo: 5

停止redis-master: docker stop docker_redis-master_1 ,会看到Spring Boot Application的 Redis 链接出现了问题:

07:54:37.206  INFO  hello.Application                    : INCR foo: 17
07:54:40.204  INFO  hello.Application                    : INCR foo: 18
07:54:42.238  INFO  i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was /10.0.19.4:6379
07:54:52.247  WARN  i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.ConnectTimeoutException: connection timed out: /10.0.19.4:6379
...
07:55:22.560  INFO  i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 10.0.19.4:6379
07:55:22.842  WARN  i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedNoRouteToHostException: Host is unreachable: /10.0.19.4:6379
...
07:55:29.582  INFO i.l.core.protocol.ConnectionWatchdog  : Reconnecting, last destination was 10.0.19.4:6379
07:55:32.353  WARN i.l.core.protocol.ConnectionWatchdog  : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedNoRouteToHostException: Host is unreachable: /10.0.19.4:6379
...

等待大约60秒,Redis Sentinel介入,将redis-slave提拔为master,链接恢复:

07:55:43.860  INFO i.l.core.protocol.ConnectionWatchdog  : Reconnecting, last destination was 10.0.19.4:6379
07:55:43.882  INFO i.l.core.protocol.ReconnectionHandler : Reconnected to 10.0.19.6:6379
07:55:43.887  INFO hello.Application                     : INCR foo: 20
07:55:43.889  INFO hello.Application                     : INCR foo: 21
07:55:43.891  INFO hello.Application                     : INCR foo: 22
07:55:43.892  INFO hello.Application                     : INCR foo: 23

此时拓扑变成这样:

+-------------//------------+
     |                           |
  +-----+       +-----+       +-----+
  |  M  |--//---|  S  |-------| [M] |
  +-----+       +-----+       +-----+
     |             |             |
     |          +-----+          |
     +----//----|  C  |----------+
                +-----+

清理容器: docker-compose down

Master重启之后的问题

这个问题和Spring Boot没有关系,是Redis本身的。如果我们把前面停掉的master重启,sentinel是不会感知到这个master的,因为这个master的ip变了(见这个 comment ):

你可以观察重启之以后的master的INFO:

$ docker exec docker_redis-master_1 redis-cli -a abc INFO replication
# Replication
role:master
...

可以看到它启动之后还是master,不是slave。这样的话就等于出现了两个master,这就出问题了。

BTW,redis的配置中可以使用hostname,比如 slaveof redis-master 。但是redis-sentinel,使用的是ip,即使你配置的是hostname,最终也是ip。执行下面命令可以看见sentinel的配置:

$ docker exec docker_redis-sentinel_1 cat /bitnami/redis-sentinel/conf/sentinel.conf | grep springboot

sentinel monitor springboot 10.0.25.2 6379 1
sentinel down-after-milliseconds springboot 60000
sentinel auth-pass springboot abc
sentinel config-epoch springboot 0
sentinel leader-epoch springboot 0
sentinel known-slave springboot 10.0.25.5 6379

解决办法1:使用host network

使用host network来部署redis-master、redis-slave,使用 <host-ip>:<container-port> 来访问它们,因为host的ip是比较固定的,可以缓解这个问题。

用host network则还有一个限制:不能在同一个host上启动两个相同container-port的容器。

解决办法2:publish端口

把redis-master、redis-slave的端口publish到host上,redis.config中把 slave-announce-ipslave-announce-port 设置为host-ip和host-port,最后使用 <host-ip>:<host-port> 访问,同样也是利用host的ip固定特性来解决这个问题。


以上所述就是小编给大家介绍的《Spring Boot配置Redis Sentinel的例子》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

微信营销与运营

微信营销与运营

王易 / 机械工业出版社 / 2014-1-1 / CNY 49.00

这是一本深度介绍微信营销的书,也是一本系统讲解微信公众账号运营的书,它基于微信的最新版本,从策略、方法、技巧与实践等多角度详细解析了微信的营销与运营,所有内容都是行业经验的结晶,旨在为企业运用微信提供有价值的参考。 本书首先从商业模式角度全面分析了微信5.0推出的“扫一扫”、表情商店、微信游戏、微信支付等新功能背后的商业机会,以及订阅号折叠给企业带来的影响和应对策略;其次从运营角度系统归纳了......一起来看看 《微信营销与运营》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具