Spring Data Redis:Sentinel的高可用性

1.概述

为了使Redis具有高可用性,我们可以使用Spring Data Redis对Redis Sentinel的支持。 借助Sentinel,我们可以创建自动抵御某些故障的Redis部署。

Redis Sentinel还提供其他附带任务,例如监视,通知,并充当客户端的配置提供程序。

在较高级别,Sentinel的功能是:

  • 自动故障转移 。 当主服务器无法正常工作时,Sentinel会为我们启动故障转移过程,将从服务器升级为主服务器。 此外,其他从属服务器也被重新配置为使用新的主服务器,并且使用Redis服务器的应用程序将被告知要使用的新地址。
  • 配置源 。 故障转移发生时,Sentinels将报告新地址。 这是因为Sentinel充当客户端的授权来源。 客户端进行服务发现时,它们会连接到Sentinels,以请求负责给定服务的当前Redis主服务器的地址。
  • 监控 。 Sentinel会定期检查我们的主实例和从实例是否按预期工作。
  • 通知 。 可以将Sentinel配置为在Redis实例之一发生错误时通知各种目标。 这些目标包括其他应用程序,系统管理员或API。

2.如何运行前哨

自Redis 2.8起,Redis附带了Sentinel的稳定版本。

启动Sentinel非常容易。 当我们在上一篇文章中回顾Spring Data Redis(使用Spring Boot)时,我们在Mac上使用homebrew安装了Redis。 此命令使我们可以在该安装中运行Sentinel:

redis-sentinel /path/to/sentinel.conf

如果我们使用的是redis-sentinel可执行文件(或者如果有一个使用该名称的符号链接到redis-server的可执行文件),那么我们也可以使用上述命令运行Sentinel。

另外,我们可以使用redis-server可执行文件并以Sentinel模式启动它,如下所示:

redis-server /path/to/sentinel.conf --sentinel

3.部署Sentinel之前需要了解的关键概念

在部署到Sentinel之前,我们应该检查的一些概念包括:

  1. 我们至少需要三个Sentinel实例才能进行持久的Redis部署。
  2. 我们应该将三个Sentinel实例放置在据信会独立而不是一起失败的计算机或虚拟机中。 例如,这可能意味着不同的可用区。
  3. Redis使用异步复制,因此即使在使用Sentinel时,Redis也不能保证在故障期间会保持接收到的写入。 但是,我们可以部署Sentinel来减少写入丢失的时间。
  4. 任何高可用性设置都必须定期进行测试,并且Sentinel不变。 我们需要在开发环境和生产环境中进行测试。 通过计划和测试故障,我们可以限制故障。

4. Spring数据中的配置

当我们使用基于Sentinels的配置时,我们不会向Spring Data Redis提供Redis主机/端口信息。 相反,我们提供了主服务器的属性和Sentinel URL列表。 每个Sentinel进程都有其自己的配置文件,该文件列出了主Redis服务器,例如:

sentinel monitor themaster 127.0.0.1 6379 2
sentinel down-after-milliseconds themaster 60000
sentinel failover-timeout themaster 180000
sentinel parallel-syncs themaster 1

一旦配置好了主服务器,从服务器和Sentinels,我们就需要在应用程序中更改spring数据redis配置,以与哨兵一起工作。

4.1 Java配置

可以使用Jedis和Lettuce来完成Java配置:

/**
 * Jedis
 */
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("themaster")
  .sentinel("127.0.0.1", 26579)
  .sentinel("127.0.0.1", 26580);
  return new JedisConnectionFactory(sentinelConfig);
}

/**
 * Lettuce
 */
@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
  .master("themaster")
  .sentinel("127.0.0.1", 26579)
  .sentinel("127.0.0.1", 26580);
  return new LettuceConnectionFactory(sentinelConfig);
}

4.2属性配置

ProperySource (例如application.properties )可用于配置。 例如,如果我们使用本地主机:

spring.redis.sentinel.master= themaster # Name of our Redis server.
spring.redis.sentinel.nodes= localhost:26579, localhost:26580, localhost:26581 # Comma-separated list of host:port pairs.

5.结论

今天,我们回顾了如何通过使用Sentinel使用Redis实现高可用性,以及Spring Data Redis如何在Spring应用程序中支持这一点。 有关Sentinel的更多信息, Redis网站是一个很好的来源。

在我的网站上,还有从Spring Data Redis和Spring Boot开始的信息以及有关Spring Framework的几篇文章。

翻译自: https://www.javacodegeeks.com/2019/01/spring-data-redis-high-availability-sentinel.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值