Updated on 2024-10-30 GMT+08:00

Connecting to an Instance Using Redisson

This section describes how to connect to a GeminiDB Redis instance using Redisson in single-node or sentinel mode.

Prerequisites

  • A GeminiDB Redis instance has been created and is running properly. For details about how to create a GeminiDB Redis instance, see Buying a Cluster Instance.
  • 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.

SingleServer Mode

Example code:

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class SingleServerTests {

  private static void testSingleServer() {
    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.useSingleServer().setAddress("redis://172.xx.xx.xx:8635")
                            .setPassword(password);
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);   // send requests to database
    redisson.shutdown();
  }

  public static void main(String[] args) {
    testSingleServer();
  }
}
  • In the preceding code, 172.xx.xx.xx indicates the load balancer IP address of the GeminiDB Redis instance that you want to connect to.

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

    Figure 1 Viewing the load balancer IP address
  • 8635 in the preceding code is the port of the instance to be connected. Specify a port number based on service requirements. For details about how to obtain the port number, see Viewing the IP Address and Port Number.
  • For details about the supported and restricted commands, see Development and O&M Rules.
  • The hash algorithm used by an open-source Redis cluster is different from that used by a GeminiDB Redis cluster. It is necessary to add hashtags to keys in some commands. Otherwise, unexpected exceptions may occur. For details about how to use hash tags, see Development and O&M Rules.

Sentinel Mode

Example code:

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(master_name)
            .setCheckSentinelsList(false)
            .setReadMode(MASTER)
            .setPassword(password)
            .addSentinelAddress("redis://172.xx.xx.xx:8635");
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);      // send requests to database
    redisson.shutdown();
}

  public static void main(String[] args) {
    testSentinel();
  }
}
  • In the preceding code, 172.xx.xx.xx indicates the load balancer IP address of the GeminiDB Redis instance that you want to connect to.

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

    Figure 2 Viewing the load balancer IP address
  • 8635 in the preceding code is the port of the instance to be connected. Specify a port number based on service requirements. For details about how to obtain the port number, see Viewing the IP Address and Port Number.
  • 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.
  • For details about the supported and restricted commands, see Development and O&M Rules.
  • The hash algorithm used by an open-source Redis cluster is different from that used by a GeminiDB Redis cluster. It is necessary to add hashtags to keys in some commands. Otherwise, unexpected exceptions may occur. For details about how to use hash tags, see Development and O&M Rules.

ClusterServer Mode

Example code:

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
 
public class ClusterServerTests {
 
  private static void testClusterServer() {
    Config config = new Config();
    config.useClusterServers()
                .addNodeAddress("172.xx.xx.xx:8635").setPassword(password);
 
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);   // send requests to database
    redisson.shutdown();
  }
 
  public static void main(String[] args) {
    testClusterServer();
  }
}
  • In the preceding code, 172.xx.xx.xx indicates the load balancer IP address of the GeminiDB Redis instance that you want to connect to.

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

    Figure 3 Viewing the load balancer IP address
  • 8635 in the preceding code is the port of the instance to be connected. Specify a port number based on service requirements. For details about how to obtain the port number, see Viewing the IP Address and Port Number.
  • For details about the supported and restricted commands, see Development and O&M Rules.
  • The hash algorithm used by an open-source Redis cluster is different from that used by a GeminiDB Redis cluster. It is necessary to add hashtags to keys in some commands. Otherwise, unexpected exceptions may occur. For details about how to use hash tags, see Development and O&M Rules.