本教程详细介绍如何使用Alibaba Cloud SDK for Java查询Redis实例列表以及单个实例信息详情。

前提条件

在使用本教程之前,请确保已完成以下操作:

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core --&gt;     <dependency&gt;         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-core</artifactId>         <version>4.4.3</version>     </dependency>     <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-rds -->     <dependency>         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-r-kvstore</artifactId>         <version>2.1.1</version>     </dependency> </dependencies>

示例代码

本文操作示例主要以代码形式体现,具体代码如下:

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.r_kvstore.model.v20150101.DescribeInstanceAttributeRequest; import com.aliyuncs.r_kvstore.model.v20150101.DescribeInstanceAttributeResponse; import com.aliyuncs.r_kvstore.model.v20150101.DescribeInstancesRequest; import com.aliyuncs.r_kvstore.model.v20150101.DescribeInstancesResponse; import com.google.gson.Gson; import java.util.List;  /**  * DescribeInstances    查询一个或多个Redis实例的信息  * DescribeInstanceAttribute    查询Redis实例的详细信息  */ public class DescribeInstanceAttribute {      public static void main(String[] args) {         IAcsClient client = Initialization();         // 查询Redis实例列表(可做筛选)         List<DescribeInstancesResponse.KVStoreInstance> instances = DescribeInstances(client);         // 去Redis实例集合中的某一个进行详细信息查询         // 请根据实例情况进行选取,或者配合前端         DescribeInstancesResponse.KVStoreInstance instance = instances.get(0);         // 要查询详细信息的实例Id         String instanceId = instance.getInstanceId();         //查询Redis实例的详细信息         testDescribeInstanceAttribute(client, instanceId);     }      /**      * 查询Redis实例列表(可做筛选)      * 筛选条件可参考API:DescribeInstances      */     private static List<DescribeInstancesResponse.KVStoreInstance> DescribeInstances(IAcsClient client) {         DescribeInstancesRequest request = new DescribeInstancesRequest();         // 实例状态:         // Normal(正常)         // Creating(创建中)         // Changing(修改中)         // Inactive(被禁用)         // Flushing(清除中)         // Released(已释放)         // Transforming(转换中)         // Unavailable(服务停止)         // Error(创建失败)         // Migrating(迁移中)         // BackupRecovering(备份恢复中)         // MinorVersionUpgrading(小版本升级中)         // NetworkModifying(网络变更中)         // SSLModifying(SSL变更中)         // MajorVersionUpgrading (大版本升级中,可正常访问)         request.setInstanceStatus("Normal");         try {             DescribeInstancesResponse response = client.getAcsResponse(request);             // 获取到Redis实例的集合             return response.getInstances();         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             throw new RuntimeException();         }     }      /**      * DescribeInstanceAttribute    查询单个Redis实例的详细信息      */     private static void testDescribeInstanceAttribute(IAcsClient client, String instanceId) {          DescribeInstanceAttributeRequest request = new DescribeInstanceAttributeRequest();         // 实例ID         request.setInstanceId(instanceId);         try {             DescribeInstanceAttributeResponse response = client.getAcsResponse(request);             // 处理单个Redis实例详细信息             System.out.println(new Gson().toJson(response));             // return new Gson().toJson(response.getInstances());         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             throw new RuntimeException();         }     }       /**      * Initialization  初始化公共请求参数      */     private static IAcsClient Initialization() {         // 初始化请求参数         DefaultProfile profile = DefaultProfile.getProfile(                 "<your-region-id>", // 您的可用区ID                 "<your-access-key-id>", // 您的AccessKey ID                 "<your-access-key-secret>"); // 您的AccessKey Secret         return new DefaultAcsClient(profile);     } }

执行结果

正确的返回结果类似如下:

{     "requestId": "F74094B2-B9A8-4309-A3CC-40F4A050B8A0",      "instances": [         {             "instanceId": "r-bp1i*****",              "instanceName": "funck",              "connectionDomain": "r-bp1i*****.redis.rds.aliyuncs.com",              "port": 6379,              "instanceStatus": "Normal",              "regionId": "cn-hangzhou",              "capacity": 8192,              "instanceClass": "redis.master.large.default",              "qPS": 100000,              "bandwidth": 24,              "connections": 10000,              "zoneId": "cn-hangzhou-f",              "config": "{"EvictionPolicy":"volatile-lru","hash-max-ziplist-entries":512,"zset-max-ziplist-entries":128,"list-max-ziplist-entries":512,"list-max-ziplist-value":64,"zset-max-ziplist-value":64,"set-max-intset-entries":512,"hash-max-ziplist-value":64}",              "chargeType": "PostPaid",              "nodeType": "double",              "networkType": "VPC",              "vpcId": "vpc-bp1g*****",              "vSwitchId": "vsw-bp1s*****",              "privateIp": "172.16.253.169",              "createTime": "2019-09-25T14:19:00Z",              "hasRenewChangeOrder": "false",              "isRds": true,              "engine": "Redis",              "engineVersion": "5.0",              "maintainStartTime": "18:00Z",              "maintainEndTime": "22:00Z",              "availabilityValue": "100.0%",              "securityIPList": "127.0.0.1",              "instanceType": "Redis",              "architectureType": "standard",              "nodeType1": "double",              "packageType": "standard",              "vpcAuthMode": "Open",              "auditLogRetention": "0",              "replicationMode": "master-slave",              "vpcCloudInstanceId": "r-bp1*****",              "tags": [ ]         }     ] }