文档

Sentinel兼容模式连接

更新时间:

云数据库 Redis 版采用自研的高可用服务HA组件,无需依赖Sentinel(哨兵)。但为了提高云数据库 Redis 版的兼容性,减少代码改动,云数据库 Redis 版提供Sentinel兼容模式,开启后客户端可以像连接开源Redis Sentinel一样连接Tair实例。

Redis Sentinel简介

Redis Sentinel为开源Redis提供主从实例监控、故障告警、自动故障切换等服务,很多使用本地自建Redis数据库并且对可靠性要求较高的业务场景都用到了Sentinel。为了给这类场景中的Redis数据库迁移上云提供方便,阿里云开发了Sentinel兼容模式。开启Sentinel兼容模式后,您可以使用如下的Sentinel相关命令:

命令

说明

SENTINEL sentinels

查询master的Sentinel实例列表以及这些Sentinel实例的状态。使用方式:

SENTINEL sentinels <任意名称>

SENTINEL get-master-addr-by-name

查询master的IP地址和端口号。使用方式:

SENTINEL get-master-addr-by-name <任意名称>

关于Sentinel相关命令在各版本中的支持度,请参见Redis社区版命令支持

前提条件

  • Redis实例的版本为4.0及以上。

  • Redis实例的网络类型为专有网络。

    说明

    如果实例为经典网络,切换方法请参见切换为专有网络VPC

  • 已将客户端的IP地址(ECS实例的内网IP地址或本地主机的外网IP地址)加入Redis白名单

操作步骤

  1. 访问Redis实例列表,在上方选择地域,然后单击目标实例ID。

  2. 实例信息页的左侧导航栏中,单击参数设置

  3. 根据实例架构,通过修改对应的参数开启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兼容模式后,有两种方式连接Redis实例:若实例开启专有网络免密访问,您可以通过Sentinel模式免密连接Redis实例;若未开启免密访问,您需要在连接时配置验证信息。

  • Sentinel免密连接

    说明

    开启专有网络免密访问的具体操作,请参见开启专有网络免密访问

    本示例以Spring Data Redis为例,阿里云Redis 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

    • sentinelRedis实例的专有网络连接地址与端口号,用英文逗号(,)分隔,例如"r-********.redis.rds.aliyuncs.com", 6379

    开源代码配置如下:

        @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;
        }
  • Sentinel密码连接

    本示例以Java客户端的最低版本为例,客户端版本要求如下:

    • Jedis为3.6.0版本及以上。

    • Lettuce为5.3.0.RELEASE版本及以上。

    • Spring Data Redis为2.5.1版本及以上,同时需要配置spring.redis.sentinel.password参数。

    说明

    强烈建议您升级最新稳定版本客户端,最新版本请搜索MVN Repository

    阿里云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 = "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:设置为Redis实例的专有网络连接地址和端口号,格式为r-********.redis.rds.aliyuncs.com:6379

    • dbPasswordsentinelPassword:设置为Redis实例账号的密码。根据选取账号的不同,密码的填写格式有一定区别。如果忘记密码,您可以重置密码。具体操作,请参见修改或重置密码

      说明
      • 默认账号(即以实例ID命名的账号):直接填写密码即可。

      • 新创建的账号:密码格式为<user>:<password>,默认账号也支持此认证方式。例如自定义账号为testaccount,密码为Rp829dlwa,密码需填写为testaccount:Rp829dlwa

    开源代码配置如下:

            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);

常见问题

  • Q:原来使用自建Redis Sentinel模式,切换至Tair Sentinel后,遇到NOAUTH Authentication required错误该怎么处理?

    A:请升级您的客户端,并修改部分代码用以添加Sentinel认证密码,再进行重试。更多信息,请参见本文中的Sentinel密码连接

  • Q:Redis经济版实例支持Sentinel兼容模式吗?

    A:支持。

  • 本页导读 (1)
文档反馈