Updated on 2024-12-02 GMT+08:00

Connecting to an Instance Using Sentinel

GeminiDB Redis API uses an in-house HA component and does not depend on Sentinel. To reduce code modifications and improve instance compatibility, GeminiDB Redis API is compatible with Redis Sentinel. After the Sentinel mode is enabled, you can connect to a GeminiDB Redis instance to Redis Sentinel.

Prerequisites

  • A cluster or primary/standby GeminiDB Redis instance has been created and is in the Available state.
  • An ECS is available. For details, see Purchasing an ECS.
  • GNU Compiler Collection (GCC) has been installed on the ECS.
  • The created ECS is in the same region, AZ, VPC, and security group as the GeminiDB Redis instance.
  • To connect to a DB instance in Sentinel mode, you must enable the Sentinel compatibility mode first.

Enabling the Sentinel Compatibility Mode

  1. Log in to the GeminiDB console.
  2. In the service list, choose Databases > GeminiDB Redis API.
  3. On the Instances page, click the instance whose specifications you want to change. The Basic Information page is displayed.
  4. In the navigation pane on the left, choose Parameters.
  5. Change the value of CompatibleMode and click Save.

    • For a cluster DB instance, change the value of CompatibleMode to 3.
    • For a primary/standby DB instance, change the value of CompatibleMode to 2.
    Figure 1 Changing parameters

Connecting to a DB Instance in Sentinel Mode

This section uses Java as an example to describe how to access a GeminiDB Redis instance through the open-source libraries Redisson and Jedis.

Redisson code example:

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import static org.redisson.config.ReadMode.MASTER;
 
public class SingleServerTests {
 
  public static void testSentinel() {
    Config config = new Config();
    // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
    // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");
    config.useSentinelServers()
            .setMasterName("mymaster")
            .setCheckSentinelsList(false)
            .setReadMode(ReadMode.MASTER)
            .setPassword(password)
            .addSentinelAddress("redis://172.xx.xx.xx:6379");
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);      // send requests to database
    redisson.shutdown();
}
 
  public static void main(String[] args) {
    testSentinel();
  }
}

Jedis code example:

import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class TestJedisSentinel {
	public static void main(String[] args) {
		Set<String> sentinels = new HashSet<>();
		sentinels.add("192.xx.xx.xx:6379");
		GenericObjectPoolConfig<Jedis> poolConfig = new GenericObjectPoolConfig<>();
		poolConfig.setMaxIdle(100);
		poolConfig.setMaxWaitMillis(10000);
		poolConfig.setTestOnBorrow(true);
		int connectionTimeout = 5000;
		int soTimeout = 5000;
		int database = 0;
              // There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
    // In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
               String password = System.getenv("EXAMPLE_PASSWORD_ENV");
		try (JedisSentinelPool jspool = new JedisSentinelPool("mymaster", sentinels, poolConfig, 
				connectionTimeout, soTimeout, password, database)) {
			Jedis jedis = jspool.getResource();
			jedis.mset("testkey", "AAA", "b", "BBB");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • MasterName: fixed character string mymaster.
  • CheckSentinelsList: The value must be false.
  • ReadMode: ReadMode.MASTER is used.
  • Password: password of the instance
  • SentinelAddress: load balancer address of the GeminiDB Redis instance. Replace it with the actual IP address and port number.

    You can click the instance name to go to the Basic Information page and obtain the load balancer IP address in the Connection Information area.

    Figure 2 Viewing the load balancer IP address

The Sentinel mode is only used for connection and its native availability is not used. Thus, in the sample code, master_name is fixed to mymaster. CheckSentinelsList must be set to false and ReadMode must be set to MASTER.