Help Center> GeminiDB> GeminiDB Redis API> Development Reference> Client Program Demo> Connecting to an Instance Using Sentinel
Updated on 2024-05-20 GMT+08:00

Connecting to an Instance Using Sentinel

This section describes how to use GeminiDB, which is compatible with Redis Sentinel access.

Prerequisites

  • A GeminiDB Redis instance has been created and is in the Available status.
  • An ECS has been created. For details about how to create an ECS, see Purchasing an ECS in Getting Started with Elastic Cloud Server.
  • The 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 management 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.