为提高云原生内存数据库Tair的兼容性,减少代码改动,云原生内存数据库Tair提供Sentinel(哨兵)兼容模式,开启后客户端可以像连接原生Redis Sentinel一样连接Tair实例。
云原生内存数据库Tair采用自研的高可用服务HA组件,无需Sentinel。
Tair Sentinel简介
Redis Sentinel为开源Redis提供主从实例监控、故障告警、自动故障切换等服务,很多使用本地自建Redis数据库并且对可靠性要求较高的业务场景都用到了Sentinel。为了给这类场景中的Redis数据库迁移上云提供方便,阿里云开发了Sentinel兼容模式。
开启Sentinel兼容模式后,您可以使用如下的Sentinel相关命令:
命令 | 说明 |
---|---|
SENTINEL sentinels | 查询master的Sentinel实例列表以及这些Sentinel实例的状态。使用方式:
|
SENTINEL get-master-addr-by-name | 查询master的IP地址和端口号。使用方式:
|
说明 关于Sentinel相关命令在各版本中的支持度,请参见Tair命令支持与限制。
前提条件
已将客户端的IP地址(ECS实例的内网IP地址或本地主机的外网IP地址)加入Tair白名单。
操作步骤
Sentinel连接示例
开启Sentinel兼容模式后,有两种方式连接Tair实例:
若实例开启专有网络免密访问,您可以通过Sentinel模式免密连接Tair实例;若未开启免密访问,您需要在连接时配置验证信息。
- 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:自定义一个任意的名称,例如testmaster。
- sentinel-host:设置为Tair实例的专有网络连接地址。
- sentinel-port:设置为Tair实例的端口号,默认端口号为6379。
Tair 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密码连接本示例以Java客户端的最低版本为例, 客户端版本要求如下:
- Jedis为3.6.0版本及以上
- Lettuce为5.3.0.RELEASE版本及以上
- Spring Data Redis为2.5.1版本及以上,Spring Data Redis需要配置spring.redis.sentinel.password参数。
说明 强烈建议您升级最新稳定版本客户端,最新版本请搜索MVN Repository。开源代码配置如下:
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:自定义一个任意的名称,例如testmaster。
- sentinels.add:设置为Tair实例的专有网络连接地址和端口号,格式为
r-********.redis.rds.aliyuncs.com:6379
。 - dbPassword/sentinelPassword:设置为Tair实例账号的密码。根据选取账号的不同,密码的填写格式有一定区别。如果忘记密码,您可以重置密码。具体操作,请参见修改或重置密码。说明
- 默认账号(即以实例ID命名的账号):直接填写密码即可。
- 新创建的账号:密码格式为
<user>:<password>
,默认账号也支持此认证方式。例如自定义账号为testaccount
,密码为Rp829dlwa
,密码需填写为testaccount:Rp829dlwa
。
Tair Sentinel兼容模式连接代码配置示例:
String masterName = "any-name"; Set<String> sentinels = new HashSet<>(); sentinels.add("r-********.redis.rds.aliyuncs.com:6379"); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); String dbPassword = "testaccount:Rp829dlwa"; String sentinelPassword = "testaccount:Rp829dlwa"; JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, poolConfig, 2000, 2000, dbPassword, 0, null, 2000, 2000, sentinelPassword, null);
常见问题
- Q:原来使用自建Redis Sentinel模式,切换至Tair Sentinel后,遇到
NOAUTH Authentication required
错误该怎么处理?A:请升级您的客户端,并修改部分代码用以添加Sentinel认证密码,再进行重试。更多信息,请参见本文中的Sentinel密码连接。