云原生内存数据库Tair采用自研的高可用服务HA组件,无需依赖Sentinel(哨兵)。但为了提高云原生内存数据库Tair的兼容性,减少代码改动,云原生内存数据库Tair提供Sentinel兼容模式,开启后客户端可以像连接开源Redis Sentinel一样连接Tair实例。
Redis Sentinel简介
前提条件
已将客户端的IP地址(ECS实例的内网IP地址或本地主机的外网IP地址)加入Tair白名单。
操作步骤
访问Tair实例列表,在上方选择地域,然后单击目标实例ID。
在实例信息页的左侧导航栏中,单击参数设置。
根据实例架构,通过修改对应的参数开启Sentinel兼容模式,具体操作请参见设置实例参数。
若实例为集群架构代理模式或读写分离架构:将sentinel_compat_enable参数的值修改为1。
若实例为标准架构:将#no_loose_sentinel-enabled参数的值修改为yes。
说明您可以在实例详情页确认实例的架构信息。
集群架构直连模式使用开源Redis Cluster进行负载均衡,无需Sentinel组件,也不支持设置Sentinel参数。
开启后,您可以连接实例,执行
SENTINEL sentinels test
命令进行测试,执行成功表示实例已开启Redis Sentinel兼容模式。Sentinel兼容模式不提供额外的连接地址,您可以直接通过原连接地址(例如r-********.redis.rds.aliyuncs.com:6379
)进行连接。
Sentinel连接示例
开启Sentinel兼容模式后,有两种方式连接Tair实例:若实例开启专有网络免密访问,您可以通过Sentinel模式免密连接Tair实例;若未开启免密访问,您需要在连接时配置验证信息。
Sentinel免密连接
说明开启专有网络免密访问的具体操作,请参见开启专有网络免密访问。
本示例以Spring Data Redis为例,阿里云Tair Sentinel兼容模式连接代码配置示例如下:
@Bean public JedisConnectionFactory connectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("testmaster") .sentinel("r-********.redis.rds.aliyuncs.com", 6379); JedisPoolConfig poolConfig = new JedisPoolConfig(); ... JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig); return connectionFactory; }
参数说明:
master:自定义名称,可保持默认,例如
testmaster
。sentinel:Tair实例的专有网络连接地址与端口号,用英文逗号(,)分隔,例如
"r-********.redis.rds.aliyuncs.com", 6379
。
Sentinel密码连接
本示例以Java客户端的最低版本为例,客户端版本要求如下:
Jedis为3.6.0版本及以上。
Lettuce为5.3.0.RELEASE版本及以上。
Spring Data Redis为2.5.1版本及以上,同时需要配置spring.redis.sentinel.password参数。
说明强烈建议您升级最新稳定版本客户端,最新版本请搜索MVN Repository。
阿里云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);
参数说明:
masterName:自定义名称,可保持默认,例如
testmaster
。sentinels.add:设置为Tair实例的专有网络连接地址和端口号,格式为
r-********.redis.rds.aliyuncs.com:6379
。dbPassword与sentinelPassword:设置为Tair实例账号的密码。根据选取账号的不同,密码的填写格式有一定区别。如果忘记密码,您可以重置密码。具体操作,请参见修改或重置密码。
说明默认账号(即以实例ID命名的账号):直接填写密码即可。
新创建的账号:密码格式为
<user>:<password>
,默认账号也支持此认证方式。例如自定义账号为testaccount
,密码为Rp829dlwa
,密码需填写为testaccount:Rp829dlwa
。
常见问题
Q:原来使用自建Redis Sentinel模式,切换至Tair Sentinel后,遇到
NOAUTH Authentication required
错误该怎么处理?A:请升级您的客户端,并修改部分代码用以添加Sentinel认证密码,再进行重试。更多信息,请参见本文中的Sentinel密码连接。
- 本页导读 (1)