6.5 KiB
- original_name
en-us_topic_0054235811.html
Accessing a DCS Redis Instance
You can access the created DCS instances through your Redis client.
DCS works with multiple types of Redis clients. This section describes how to use the redis-cli and Redis Java (Jedis) clients to access a DCS Redis instance. For more information on how to use other Redis clients, visit https://redis.io/clients.
Prerequisites
The DCS Redis instance you want to access is in the Running state.
An ECS has been created to serve as your Redis client.
For more information on how to create ECSs, see the Elastic Cloud Server User Guide.
The GNU Compiler Collection (GCC) has been installed on the ECS serving as your Redis client.
Note
The ECS serving as your Redis client and the DCS instance to be accessed must belong to the same VPC and can communicate with each other.
Procedure
- Log in to the management console.
- Click
in the upper left corner of the management console and select a region.
- Click Service List, and choose Database > Distributed Cache Service to launch the DCS console.
- In the navigation pane, choose Cache Manager.
Obtain the IP address:port number of the DCS instance.
- On the Cache Manager page, click the name of the DCS instance you want to access.
- On the Basic Information tab page of the instance, view the Connection Address in the Connection section.
- Access the chosen DCS instance.
- By using redis-cli
Download the source code package of your Redis client from http://download.redis.io/releases/redis-3.0.7.tar.gz.
Upload the source code package of your Redis client to the ECS serving as your Redis client.
Log in to the ECS that serves as your Redis client.
Run the following command to decompress the redis-3.0.7 directory from the source code package of your Redis client:
tar -xzf redis-3.0.7.tar.gz
Run the following commands to go to the redis-3.0.7 directory and compile the source code of your Redis client:
cd redis-3.0.7
make
Run the following commands to access the chosen DCS instance:
cd src
./redis-cli -h 192.168.0.148 -p 6379
In the second command, use the DCS instance IP address and port number obtained in
5 <en-us_topic_0054235811__li7304143>
.On redis-cli, run the following command to authenticate access to the selected DCS instance:
auth password
In this command, password indicates the password used for login to the chosen DCS instance. This password is defined during DCS Redis instance creation.
If information similar to the following is displayed, access to the chosen DCS instance is authenticated, and you can now read from and write to the chosen DCS instance:
[root@dcs-vm ~]# ./redis-cli -h 192.168.0.148 -p 6379 192.168.0.148:6379> auth ****** OK 192.168.0.148:6379>
In this example command output:
- 192.168.0.148 is an example IP address of DCS instance, which is obtained in
5 <en-us_topic_0054235811__li7304143>
. - 6379 is an example port number of DCS instance, which is obtained in
5 <en-us_topic_0054235811__li7304143>
.
- 192.168.0.148 is an example IP address of DCS instance, which is obtained in
- By using Jedis
- Obtain the source code of the Jedis client from https://github.com/xetorthio/jedis.
- Write code.
Example code for a single Jedis connection
//Create a connection String host = "192.168.0.150"; int port = 6379; String pwd = "passwd"; Jedis client = new Jedis(host, port); client.auth(pwd); client.connect(); //Run the set command String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set command result:%s", result) ); //Run the get command String value = client.get("key-string"); System.out.println( String.format("get command result:%s", value) );
Note
In actual scenarios, replace passwd with the actual ciphertext password of the Redis instance. Do not hard-code the plaintext password in the code. This advice also applies to passwd configurations in other steps.
Example code for a Jedis connection pool
//Generate configuration information of a Jedis connection pool String ip = "192.168.0.150"; int port = 6379; String pwd = "passwd"; GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setTestOnBorrow(false); config.setTestOnReturn(false); config.setMaxTotal(100); config.setMaxIdle(100); config.setMaxWaitMillis(2000); JedisPool pool = new JedisPool(config, ip, port, 100000, pwd);//Generate a Jedis connection pool when the client application is being initialized //Get a Jedis connection from the Jedis connection pool when the client initiates a request Jedis client = pool.getResource(); try { //Run commands String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set command result:%s", result) ); String value = client.get("key-string"); System.out.println( String.format("get command result:%s", value) ); } catch (Exception e) { // TODO: handle exception } finally { //Return the Jedis connection to the Jedis connection pool after the client's request is processed if (null != client) { pool.returnResource(client); } } // end of try block //Destroy the Jedis pool when the client application is closed pool.destroy();
- Compile code according to the readme file in the source code of the Jedis client. Run the Jedis client to access the chosen DCS Redis instance.
- By using redis-cli