本文简要介绍了Java SDK的安装方法,并提供了示例代码。

背景信息

  • OpenAPI开发者门户提供在线调试API和动态生成SDK示例代码的功能,能显著降低API的使用难度,推荐您使用。
  • 关于云监控API详情,请参见API概览

Java SDK安装

您可以通过下面两种方法安装Java SDK。具体操作,请参见快速开始

  • 方法一:通过Maven管理项目依赖(推荐)
    1. 使用Maven创建项目。
      mvn archetype:generate -DgroupId=com.aliyun.cms.sample \
      -DartifactId=cms-sdk-sample \
      -Dpackage=com.aliyun.cms.sample \
      -Dversion=1.0-SNAPSHOT
    2. 在项目的pom.xml文件中加入相应依赖项。
      <dependencies>
           <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>3.2.6</version>
          </dependency>
      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-cms</artifactId>
        <version>7.0.21</version>
      </dependency>
      </dependencies>
      说明 请访问Maven仓库获取aliyun-java-sdk-core的最新版本。
  • 方法二:手动下载并导入SDK的JAR文件
    无论您使用Eclipse还是IntelliJ作为集成开发环境,都可以通过手动下载并导入JAR文件的方式安装Java SDK。JAR文件下载地址如下:

Java SDK示例

  • 查询监控数据
    • 请求示例

      使用时请将如下示例中的accessKeyaccessSecret分别替换成您的AccessKey ID和AccessKey Secret。

      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.google.gson.Gson;
      import java.util.*;
      import com.aliyuncs.cms.model.v20190101.*;
      
      public class DescribeMetricListDemo {
      
          public static void main(String[] args) {
              DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
              DescribeMetricListRequest request = new DescribeMetricListRequest();
              request.setDimensions("{"instanceId":<instanceId_as_example>}");
              request.setEndTime("2019-05-13 11:06:27");
              request.setStartTime("2019-05-13 10:20:27");
              request.setPeriod("60");
              request.setNamespace("acs_ecs_dashboard");
              request.setMetricName("cpu_total");
      
              try {
                  DescribeMetricListResponse 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());
              }
      
          }
      }
    • 返回示例
      Code:200
      Message:null
      RequestId:727B48BD-55E3-4E5D-BEF4-1D092055F7A4
      Datapoints:[
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":94.62,
              "userId":"127067667954****",
              "timestamp":1489472460000
          },
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":95.11,
              "userId":"127067667954****",
              "timestamp":1489472520000
          },
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":95.16,
              "userId":"127067667954****",
              "timestamp":1489472580000
          },
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":94.69,
              "userId":"127067667954****",
              "timestamp":1489472640000
          },
          {
              "Maximum":100,
              "Minimum":20,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":95.82,
              "userId":"127067667954****",
              "timestamp":1489472700000
          },
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":94.77,
              "userId":"127067667954****",
              "timestamp":1489472760000
          },
          {
              "Maximum":100,
              "Minimum":33.33,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":95.18,
              "userId":"127067667954****",
              "timestamp":1489472820000
          },
          {
              "Maximum":100,
              "Minimum":25,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":95.11,
              "userId":"127067667954****",
              "timestamp":1489472880000
          },
          {
              "Maximum":100,
              "Minimum":20,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":94.5,
              "userId":"127067667954****",
              "timestamp":1489472940000
          },
          {
              "Maximum":100,
              "Minimum":20,
              "instanceId":"i-bp18abl200xk9599****",
              "Average":94.99,
              "userId":"127067667954****",
              "timestamp":1489473000000
          }
      ]
      }
  • 创建报警规则
    • 请求示例
      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.google.gson.Gson;
      import java.util.*;
      import com.aliyuncs.cms.model.v20190101.*;
      
      public class CmsDemo {
      
          public static void main(String[] args) {
              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
              PutResourceMetricRuleRequest request = new PutResourceMetricRuleRequest();
              request.setNamespace("acs_ecs_dashboard");
              request.setRuleId("aabbcccdddxxx001");
              request.setInterval("60");
              request.setPeriod("60");
              request.setRuleName("aaabbbcc");
              request.setContactGroups("测试报警联系人");
              request.setResources("[{"instanceId":"i-8vb6p16*******"}]");
              request.setMetricName("cpu_total");
      
              List<PutResourceMetricRuleRequest.Escalations> escalationsList = new ArrayList<PutResourceMetricRuleRequest.Escalations>();
      
              PutResourceMetricRuleRequest.Escalations escalations1 = new PutResourceMetricRuleRequest.Escalations();
              escalations1.setStatistics("Average");
              escalations1.setComparisonOperator("GreaterThanOrEqualToThreshold");
              escalations1.setThreshold("90");
              escalations1.setTimes(2);
              escalationsList.add(escalations1);
              request.setEscalationss(escalationsList);
      
              try {
                  PutResourceMetricRuleResponse 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());
              }
      
          }
      }
    • 返回示例
      {
          "Message": "",
          "RequestId": "7aa25dfa-3bb9-4ed3-9704-a2d41bf67649",
          "Success": true,
          "Code": 200
      }