ApsaraDB for Redis provides the sentinel compatibility mode. After you enable this mode, clients can connect to ApsaraDB for Redis instances in the same way as they connect to native Redis Sentinel.

Prerequisites

  • The ApsaraDB for Redis cluster instance is used.
  • The engine version of the cluster instance is Redis 4.0 (Community Edition) or Redis 5.0 (Enhanced Edition).
  • The sentinel compatibility mode is enabled. For more information, see Enable the Redis Sentinel-compatible mode.
  • The internal IP address of the Elastic Compute Service (ECS) instance or the public IP address of the on-premises host is added to an IP address whitelist of the ApsaraDB for Redis instance. For more information, see Set IP address whitelists.

Introduction to Redis Sentinel

Redis Sentinel provides Redis with services such as master and replica monitoring, fault alerting, and automatic failover. Redis Sentinel is used in many business scenarios that use local on-premises Redis databases and require high reliability. To facilitate the migration of Redis databases to the cloud in such scenarios, Alibaba Cloud provides the Redis Sentinel-compatible mode.

Note ApsaraDB for Redis uses the HA component developed by Alibaba Cloud, without the need to use Redis Sentinel.

After you enable the Sentinel-compatible mode, you can use the following commands:

Command Description
SENTINEL sentinels Queries Sentinel instances for a specified master and the status of these Sentinel instances. Follow this syntax:
SENTINEL sentinels <the name of a master>
SENTINEL get-master-addr-by-name Queries the IP address and port number of a specified master. Follow this syntax:
SENTINEL get-master-addr-by-name <the name of a master>
Note Instances of ApsaraDB for Redis 2.8 do not support the preceding commands.

Example: Create a password-free connection in sentinel mode

Note To use the sentinel mode to connect to an ApsaraDB for Redis instance without a password, you must set the network type to VPC and enable password-free access. For more information, see Switch to VPC network and Enable password-free access.

In this example, spring-data-redis is used, as shown in the following source code:

    @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;
    }

Parameters:

  • master-name: the name of the master sentinel. You can specify a custom name.
  • sentinel-host: the endpoint in the VPC network for an ApsaraDB for Redis instance.
  • sentinel-port: the port number of an ApsaraDB for Redis instance. The default port number is 6379.

The following example shows how to configure the connection to use the sentinel compatibility mode to connect to ApsaraDB for Redis:

    @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;
    }

Example: Create a connection that requires a password in sentinel mode

In this example, Jedis is used, as shown in the following source code:

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

Parameters:

  • masterName: the name of the master sentinel. You can specify a custom name.
  • sentinels.add: Set the value to the endpoint and port number in the VPC network for an ApsaraDB for Redis instance. The format must be r-********.redis.rds.aliyuncs.com:6379.
  • dbPassword/sentinelPassword: Set the dbPassword and sentinelPassword parameters to the password of the ApsaraDB for Redis instance. For more information about how to change your password, see Change the password.
    Note To use a custom account to connect to the ApsaraDB for Redis instance, the connection string must be in the <user>:<password> format. For example, the username of the custom account is admin and the password is Rp829dlwa, use admin:Rp829dlwa to log on to the instance.

The following example shows how to configure the connection to use the sentinel compatibility mode to connect to ApsaraDB for Redis:

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