本教程详细介绍如何使用Alibaba Cloud SDK for Java查询DRDS实例详情。

前提条件

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

  • 使用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-drds -->     <dependency>         <groupId>com.aliyun</groupId>         <artifactId>aliyun-java-sdk-drds</artifactId>         <version>4.4.0</version>     </dependency> </dependencies>

代码示例

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

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.drds.model.v20190123.DescribeDrdsDBsRequest; import com.aliyuncs.drds.model.v20190123.DescribeDrdsDBsResponse; import com.aliyuncs.drds.model.v20190123.DescribeDrdsInstanceRequest; import com.aliyuncs.drds.model.v20190123.DescribeDrdsInstanceResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson;  /**  * DescribeDrdsDBs        查询数据库列表  * DescribeDrdsInstance    查看DRDS实例详情  */ public class DescribeDrdsInstanceDemo{      // drds实例ID     private static String drdsInstanceId = "";      public static void main(String[] args) {         IAcsClient client = initialization();         // 查询数据库列表         DescribeDrdsDBsResponse describeDrdsDBsResponse = describeDrdsDBs(client);         // 查看DRDS实例详情         DescribeDrdsInstanceResponse describeDrdsInstanceResponse = describeDrdsInstance(client);         // 处理业务逻辑         System.out.println(new Gson().toJson(describeDrdsDBsResponse));         System.out.println(new Gson().toJson(describeDrdsInstanceResponse));      }      /**      * DescribeDrdsInstance    查看DRDS实例详情      */     private static DescribeDrdsInstanceResponse describeDrdsInstance(IAcsClient client) {         DescribeDrdsInstanceRequest request = new DescribeDrdsInstanceRequest();         request.setDrdsInstanceId(drdsInstanceId);         try {             DescribeDrdsInstanceResponse response = client.getAcsResponse(request);             return response;         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             throw new RuntimeException();         }     }      /**      * DescribeDrdsDBs        查询数据库列表      */     private static DescribeDrdsDBsResponse describeDrdsDBs(IAcsClient client) {         DescribeDrdsDBsRequest request = new DescribeDrdsDBsRequest();         // DRDS实例ID         request.setDrdsInstanceId(drdsInstanceId);         try {             DescribeDrdsDBsResponse response = client.getAcsResponse(request);             return response;         } 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);     } }

执行结果

正确的执行结果类似如下:

{     "Data": {         "Db": [             {                 "Status": "NORMAL",                  "DbName": "george",                  "CreateTime": 1563773824000,                  "Schema": "george_1563773871118kxqd",                  "DbInstType": "RDS",                  "Mode": "HORIZONTAL"             }         ]     },      "PageNumber": 1,      "PageSize": 10,      "RequestId": "006B7D19-8CDB-4AA6-AAE7-23C1075276E1",      "Success": true,      "Total": 1 } {     "requestId": "4A531D5F-C99E-4123-8B10-54AD6D23DA77",      "success": true,      "data": {         "drdsInstanceId": "drds*****",          "type": "PRIVATE",          "regionId": "cn-hangzhou",          "zoneId": "cn-hangzhou-e",          "description": "log_test",          "networkType": "CLASSIC",          "status": "RUN",          "createTime": 1540889585000,          "instanceSeries": "drds.sn1.4c8g",          "instanceSpec": "drds.sn1.4c8g.8C16G",          "instRole": "MASTER",          "commodityCode": "drdspre",          "expireDate": 1543593600000,          "versionAction": "Upgradeable",          "label": "NORMAL",          "machineType": "ecs",          "orderInstanceId": "drdshbgaj7da98uu",          "vips": [             {                 "dns": "drdshbgaj7da98uu.drds.aliyuncs.com",                  "port": "3306",                  "type": "intranet",                  "vpcId": ""             }         ],          "readOnlyDBInstanceIds": [...]     } }