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

Connecting to an Instance Using Jedis

This section describes how to access a GeminiDB Redis instance using the Java client, Jedis.

The proxy cluster architecture of GeminiDB Redis API provides a unified load balancing address and high availability. So, JedisPool is recommended for easy access.

JedisSentinelPool and JedisCluster can also be used to connect to GeminiDB Redis instances.

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.
  • 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.

Using JedisPool for Access (Recommended)

Example code:

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisPoolTests {

  private static void testPool() {
    // 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 pwd = System.getenv("EXAMPLE_PASSWORD_ENV");
    JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "172.xx.xx.xx", 8635,
        2000, pwd);
    Jedis jedis = pool.getResource();
    try {
      System.out.println(jedis.hgetAll("676296"));
      System.out.println(jedis.set("key1", "value1"));
    } finally {
      jedis.close();
    }
    pool.destroy();
  }

  public static void main(String[] args) {
    testPool();
  }
}
  • 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 the open-source Redis instance is different from that used by GeminiDB Redis instances. It is necessary to add hash tags to the corresponding keys in some commands. Otherwise, unexpected behavior may occur. For details about how to use hash tags, see Development and O&M Rules.

Using JedisCluster for Access

Example code:

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class ClusterTests {

  private static void testCluster() {
    String pwd = "a";
        JedisCluster cluster = new JedisCluster(new HostAndPort("172.xx.xx.xx", 8635),
        200, 2000, 5, pwd, new GenericObjectPoolConfig());
    System.out.println(cluster.hgetAll("676296"));
    System.out.println(cluster.set("key1", "value1"));
  }

  public static void main(String[] args) {
    testCluster();
  }
}
  • 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.
  • For details about the supported and restricted commands, see Development and O&M Rules.
  • The hash algorithm used by the open-source Redis instance is different from that used by GeminiDB Redis instances. It is necessary to add hash tags to the corresponding keys in some commands. Otherwise, unexpected behavior may occur. For details about how to use hash tags, see Development and O&M Rules.

Using JedisSentinelPool for Access

Example code:

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import java.util.HashSet;
import java.util.Set;

public void SentinelTest {
    public static void main(String[] args) {
        GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<Jedis>();
        Set<String> mySentinels = new HashSet<String>();
        mySentinels.add("172.xx.xx.xx:8635");
        JedisSentinelPool pool = new JedisSentinelPool(master-name, mySentinels, config, 1000, password, 0);
        Jedis jedis = pool.getResource();
        jedis.auth(password);
        jedis.set("foo", "bar");
        String s = jedis.get("foo");
        System.out.println(s);
        jedis.close();
        pool.close();
    }
}
  • 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.
  • In the preceding code, master-name can only be set to mymaster.
  • For details about the supported and restricted commands, see Development and O&M Rules.
  • The hash algorithm used by the open-source Redis instance is different from that used by GeminiDB Redis instances. It is necessary to add hash tags to the corresponding keys in some commands. Otherwise, unexpected behavior may occur. For details about how to use hash tags, see Development and O&M Rules.