云数据库Redis提供Sentinel(哨兵)兼容模式,开启后客户端可以像连接原生Redis Sentinel一样连接Redis实例。
前提条件
- Redis实例的架构为标准版。
- Redis实例的引擎版本为4.0(社区版)或5.0。
- 已开启Sentinel兼容模式,详情请参见开启Sentinel兼容。
- 已将客户端的IP地址(ECS实例的内网IP地址或本地主机的外网IP地址)加入Redis白名单。
Redis Sentinel简介
Redis Sentinel为开源Redis提供主从实例监控、故障告警、自动故障切换等服务,很多使用本地自建Redis数据库并且对可靠性要求较高的业务场景都用到了Sentinel。为了给这类场景中的Redis数据库迁移上云提供方便,阿里云开发了Sentinel兼容模式。
说明 阿里云云数据库Redis版使用自研的高可用服务HA组件,无需Sentinel。
开启Sentinel兼容模式后,您可以使用如下的Sentinel相关命令:
命令 | 说明 |
---|---|
SENTINEL sentinels | 查询指定master的Sentinel实例列表以及这些Sentinel实例的状态。使用方式:
|
SENTINEL get-master-addr-by-name | 查询指定master的IP地址和端口号。使用方式:
|
说明 以上命令不支持2.8版本的云数据库Redis实例。
Sentinel免密连接示例
本示例以spring-data-redis为例,开源代码配置如下:
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("original-master-name")
.sentinel(original-sentinel-1-host, original-sentinel-1-port)
.sentinel(original-sentinel-2-host, original-sentinel-2-port);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
参数说明:
- master-name:可自定义一个名称。
- sentinel-host:设置为Redis实例的专有网络连接地址。
- sentinel-port:设置为Redis实例的端口号,默认端口号为6379。
阿里云Redis Sentinel兼容模式连接代码配置示例:
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("any-name")
.sentinel("r-********.redis.rds.aliyuncs.com", 6379);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
Sentinel密码连接示例
本示例以Jedis为例,开源代码配置如下:
String masterName = "original-master-name";
Set<String> sentinels = new HashSet<>();
sentinels.add("original-sentinel-1-host:original-sentinel-1-port");
sentinels.add("original-sentinel-2-host:original-sentinel-2-port");
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
String dbPassword = "original-db-password";
String sentinelPassword = "original-sentinel-password";
JedisSentinelPool jedisSentinelPool =
new JedisSentinelPool(masterName, sentinels, poolConfig,
2000, 2000, dbPassword,
0, null, 2000, 2000,
sentinelPassword, null);
参数说明:
- masterName:可自定义一个名称。
- sentinels.add:设置为Redis实例的专有网络连接地址和端口号,格式为
r-********.redis.rds.aliyuncs.com:6379
。 - dbPassword/sentinelPassword:设置为Redis实例的密码。如果忘记密码,您可以通过控制台重置密码,详情请参见修改密码。
说明 如果使用自定义账号连接Redis,连接密码格式需为
<user>:<password>
。例如,自定义账号的用户名为admin,密码为Rp829dlwa,则连接Redis时的密码为admin:Rp829dlwa
。
阿里云Redis Sentinel兼容模式连接代码配置示例:
String masterName = "any-name";
Set<String> sentinels = new HashSet<>();
sentinels.add("r-********.redis.rds.aliyuncs.com:6379");
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
String dbPassword = "admin:Rp829dlwa";
String sentinelPassword = "admin:Rp829dlwa";
JedisSentinelPool jedisSentinelPool =
new JedisSentinelPool(masterName, sentinels, poolConfig,
2000, 2000, dbPassword,
0, null, 2000, 2000,
sentinelPassword, null);
在文档使用中是否遇到以下问题
更多建议
匿名提交