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

背景信息

  • Java SDK包含阿里云Java SDK基础包(aliyun-java-sdk-core)和RAM接口定义包(aliyun-java-sdk-cms),两者都需要安装。
  • 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.24</version>
      </dependency>
      </dependencies>
      说明 请您访问以下路径查看并使用SDK最新版本:
  • 方法二:手动下载并导入SDK的JAR文件
    无论您使用Eclipse还是IntelliJ作为集成开发环境,都可以通过手动下载并导入JAR文件的方式安装Java SDK。JAR文件下载地址如下:

Java SDK示例

下面为您提供云监控API DescribeMetricList和PutResourceMetricRule的Java SDK示例代码。关于其他API,请访问OpenAPI开发者门户调试并获取示例代码。

  • DescribeMetricList

    关于如何设置DescribeMetricList的请求参数,请参见DescribeMetricList

    • 请求示例
      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 DescribeMetricList {
      
          public static void main(String[] args) {
      
              //构建一个阿里云客户端, 用于发起请求。
              //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
             //构建请求,设置请求参数。
              DescribeMetricListRequest request = new DescribeMetricListRequest();
              request.setRegionId("cn-hangzhou");
              //设置阿里云服务和监控项。
              request.setMetricName("cpu_total");
              request.setNamespace("acs_ecs_dashboard");
      
              //设置查询监控数据的开始时间和结束时间。
              request.setStartTime("1628055731050");
              request.setEndTime("1628062931050");
      
              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());
              }
      
          }
      } 
    • 返回示例
      {
          "RequestId": "2BE7EFE0-B50B-5168-BBA2-69B57E98D9C9",
          "NextToken": "1628055900000457fadc4b4c414f7166106dde17be869f14b2c0031323038383633313738363132393533242c692d6270316371686977317a613238303773636d316a242c",
          "Period": "900",
          "Datapoints": "[{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-0xii2bvf42iqvxbp****\",\"Minimum\":0.0,\"Maximum\":2.0,\"Average\":0.11},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze1qff61suafi71****\",\"Minimum\":0.0,\"Maximum\":4.95,\"Average\":0.88},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":9.3,\"Average\":1.03},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":10.86,\"Average\":1.2},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":7.34,\"Average\":0.96}]",
          "Code": "200",
          "Success": true
      }
  • PutResourceMetricRule

    关于如何设置PutResourceMetricRule的请求参数,请参见PutResourceMetricRule

    • 请求示例
      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 PutResourceMetricRule {
      
          public static void main(String[] args) {
              //构建一个阿里云客户端, 用于发起请求。
              //构建阿里云客户端时需要设置AccessKey ID和AccessKey Secret。
              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
              PutResourceMetricRuleRequest request = new PutResourceMetricRuleRequest();
              request.setRegionId("cn-hangzhou");
              //设置报警规则ID。
              request.setRuleId("a151cd6023eacee2f0978e03863cc1697c89508****");
              request.setNamespace("acs_ecs_dashboard");
              request.setMetricName("cpu_total");
              //设置报警的资源。
              request.setResources("[{\"instanceId\":\"i-0xii2bvf42iqvxbp****\"}]");
              request.setContactGroups("ECS_Group");
              //设置报警规则名称。
              request.setRuleName("myRuleId123");
              request.setEscalationsCriticalStatistics("Average");
              request.setEscalationsCriticalComparisonOperator("GreaterThanOrEqualToThreshold");
              request.setEscalationsCriticalThreshold("95");
              request.setEscalationsCriticalTimes(3);
      
              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": "80AFF69C-A779-5B68-BAA7-91CD059C05CA",
          "Code": 200,
          "Success": true
      }