内容简介:为了使用Redis实现高可用性,我们可以使用Spring Data Redis对Redis Sentinel的支持。使用Sentinel,我们可以创建一个自动抵御某些故障的Redis部署。Redis Sentinel还提供其他附属任务,如监控,通知,并充当客户端的配置提供程序。在较高的层面上,Sentinel的能力是:
为了使用 Redis 实现高可用性,我们可以使用Spring Data Redis对Redis Sentinel的支持。使用Sentinel,我们可以创建一个自动抵御某些故障的Redis部署。
Redis Sentinel还提供其他附属任务,如监控,通知,并充当客户端的配置提供程序。
在较高的层面上,Sentinel的能力是:
- 自动故障转移。当主服务器未按预期工作时,Sentinel会为我们启动故障转移过程,其中将从属服务器提升为主服务器。此外,其他从站被重新配置为使用新主站,并且使用Redis服务器的应用程序将被告知要使用的新地址。
- 配置源。发生故障转移时,Sentinels将报告新地址。这是因为Sentinel充当客户端的权限来源。当客户端进行服务发现时,它们会连接到Sentinels以请求负责给定服务的当前Redis主服务器的地址。
- 监控。Sentinel会定期检查我们的主实例和从属实例是否按预期工作。
- 通知。可以将Sentinel配置为在其中一个Redis实例发生错误时通知各种目标。这些目标包括其他应用程序,系统管理员或API。
如何运行Sentinel
自Redis 2.8以来,Robis已经发布了稳定版本的Sentinel。
启动Sentinel非常简单。我们在Mac上使用自制软件安装了Redis。此命令允许我们使用该安装运行Sentinel:
1redis-sentinel /path/to/sentinel.conf
如果我们使用redis-sentinel可执行文件(或者如果使用该名称的符号链接到redis-server可执行文件),我们也可以使用上述命令运行Sentinel。
或者,我们可以使用redis-server 可执行文件并在Sentinel模式下启动它,如下所示:
redis-server /path/to/sentinel.conf --sentinel
部署Sentinel之前需要了解的关键概念
在部署到Sentinel之前我们应该审查的一些概念包括:
- 我们需要至少三个Sentinel实例才能实现持久的Redis部署。
- 我们应该将三个Sentinel实例放入被认为独立失败而不是一起失败的计算机或虚拟机中。例如,这可能意味着不同的可用区域。
- Redis使用异步复制,因此无法保证在失败期间保留接收的写入,即使使用Sentinel也是如此。但是,我们可以部署Sentinel,以减少写入丢失的时间。
- 必须定期测试任何高可用性设置,Sentinel也不例外。我们需要在开发环境和生产环境中进行测试。通过规划和测试失败,我们限制了我们的失败。
Spring Data中的配置
当我们使用基于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
一旦我们配置了master,slave和Sentinels,我们需要在应用程序中更改spring数据redis配置以使用sentinels。
可以使用Jedis和Lettuce完成 Java 配置:
<font><i>/**
* Jedis
*/</i></font><font>
@Bean
<b>public</b> RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = <b>new</b> RedisSentinelConfiguration()
.master(</font><font>"themaster"</font><font>)
.sentinel(</font><font>"127.0.0.1"</font><font>, 26579)
.sentinel(</font><font>"127.0.0.1"</font><font>, 26580);
<b>return</b> <b>new</b> JedisConnectionFactory(sentinelConfig);
}
</font><font><i>/**
* Lettuce
*/</i></font><font>
@Bean
<b>public</b> RedisConnectionFactory lettuceConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = <b>new</b> RedisSentinelConfiguration()
.master(</font><font>"themaster"</font><font>)
.sentinel(</font><font>"127.0.0.1"</font><font>, 26579)
.sentinel(</font><font>"127.0.0.1"</font><font>, 26580);
<b>return</b> <b>new</b> LettuceConnectionFactory(sentinelConfig);
}
</font>
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.
结论
今天我们回顾了使用Sentinel如何通过Redis实现高可用性以及Spring Data Redis如何支持这一点在我们的Spring应用程序中。有关Sentinel的更多信息, Redis网站 是一个很好的来源。
以上所述就是小编给大家介绍的《Spring Data Redis:Sentinel的高可用性 - Michael C. Good》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 可用性高达5个9!支付系统高可用架构设计实战
- 浪潮InCloud OpenStack:度量可用性“三维”,实现高可用云环境
- 如何提升系统可用性?
- WebP 可用性探测
- 持续交付与可用性
- 特征工程:特征设计、特征可用性评估
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coding the Matrix
Philip N. Klein / Newtonian Press / 2013-7-26 / $35.00
An engaging introduction to vectors and matrices and the algorithms that operate on them, intended for the student who knows how to program. Mathematical concepts and computational problems are motiva......一起来看看 《Coding the Matrix》 这本书的介绍吧!