本教程介绍如何使用Alibaba Cloud SDK for Java调用RDS的DescribeDBInstancePerformance接口获取实例的性能指标情况。

前提条件

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

  • 使用RDS管理控制台或通过API获取到想要查看性能指标的RDS实例ID,API的相关操作,请参见DescribeDBInstances
  • 使用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-rds</artifactId>         <version>2.3.10</version>     </dependency> </dependencies>

代码示例

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

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.rds.model.v20140815.DescribeDBInstancePerformanceRequest; import com.aliyuncs.rds.model.v20140815.DescribeDBInstancePerformanceResponse; import com.google.gson.Gson;  public class DescribeDBInstancePerformanceDemo {      // RDS实例ID     private static final String dbInstanceId = "rm-bp1io2x7682t1****";     // 性能参数key:CPU和内存使用率,多个值用英文逗号(,)分隔     private static final String performanceKeys = "MySQL_MemCpuUsage";     // 查询开始时间。格式:yyyy-MM-ddTHH:mmZ(UTC时间)     private static final String startTime = "2019-11-04T16:00Z";     // 查询结束时间。格式:yyyy-MM-ddTHH:mmZ(UTC时间)     private static final String endTime = "2019-11-04T18:00Z";      public static void main(String[] args) {         DefaultProfile profile = DefaultProfile.getProfile(                 "<your-region-id>",            // 您的地域ID                 "<your-access-key-id>",     // 您的AccessKey ID                 "<your-access-key-secret>");// 您的AccessKey Secret         IAcsClient client = new DefaultAcsClient(profile);          DescribeDBInstancePerformanceRequest request = new DescribeDBInstancePerformanceRequest();         request.setDBInstanceId(dbInstanceId);         request.setKey(performanceKeys);         request.setStartTime(startTime);         request.setEndTime(endTime);          try {             DescribeDBInstancePerformanceResponse response = client.getAcsResponse(request);             System.out.println(new Gson().toJson(response));         } catch (ServerException e) {             e.printStackTrace();         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());         }      } }

运行结果

正确运行结果类似如下:

{     "requestId": "0512859A-1A61-4E43-962B-F6890F553336",     "dBInstanceId": "rm-bp1io2x7682t1****",     "engine": "MySQL",     "startTime": "2019-11-04T16:00Z",     "endTime": "2019-11-04T18:00Z",     "performanceKeys": [         {             "key": "MySQL_MemCpuUsage",             "unit": "%",             "valueFormat": "cpuusagememusage",             "values": [                 {                     "value": "0.48&8.8",                     "date": "2019-11-04T16:00:31Z"                 },                 {                     "value": "0.48&8.8",                     "date": "2019-11-04T16:01:31Z"                 },                 {                     "value": "0.0&8.8",                     "date": "2019-11-04T16:02:31Z"                 },                 {                     "value": "0.48&8.8",                     "date": "2019-11-04T16:03:32Z"                 },                 {                     "value": "0.0&8.8",                     "date": "2019-11-04T16:04:31Z"                 },                 ......                 {                     "value": "0.0&8.8",                     "date": "2019-11-04T17:58:39Z"                 },                 {                     "value": "0.48&8.8",                     "date": "2019-11-04T17:59:39Z"                 }             ]         }     ] }