本教程详细介绍如何使用Alibaba Cloud SDK for Java查询加速域名所有信息。

前提条件

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

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

代码示例

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

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.cdn.model.v20180510.*; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson; import java.util.List;  /**  * DescribeDomainCertificateInfo    获取指定加速域名证书信息  * DescribeCdnDomainDetail        获取指定加速域名配置的基本信息  * DescribeDomainsBySource        查询用户名下,源站对应的所有域名名称列表。不支持模糊匹配  * DescribeUserDomains                查询用户名下所有的域名与状态  * DescribeCdnService                查询CDN服务状态  * DescribeCdnUserResourcePackage   查询CDN用户当前流量包  */ public class Demo {      private static String source = "****";      private static Gson gson = new Gson();      public static void main(String[] args) {          IAcsClient client = initialization();         // 查询用户名下所有的域名与状态         DescribeUserDomainsResponse describeUserDomainsResponse = describeUserDomains(client);         //         List<DescribeUserDomainsResponse.PageData> domains = describeUserDomainsResponse.getDomains();         // 获取加速域名         String domainName = domains.get(0).getDomainName();          // 查询用户名下,源站对应的所有域名名称列表。不支持模糊匹配         describeDomainsBySource(client);          // 获取指定加速域名配置的基本信息         describeCdnDomainDetail(client, domainName);          describeDomainCertificateInfo(client, domainName);          // 查询CDN服务状态         describeCdnService(client);          // 查询CDN用户当前流量包         describeCdnUserResourcePackage(client);     }      /**      * DescribeDomainCertificateInfo    获取指定加速域名证书信息      */     private static DescribeDomainCertificateInfoResponse describeDomainCertificateInfo(IAcsClient client, String domainName) {         DescribeDomainCertificateInfoRequest request = new DescribeDomainCertificateInfoRequest();         // 加速域名         request.setDomainName(domainName);         System.out.println("------------------describeDomainCertificateInfo-------------------");         try {             DescribeDomainCertificateInfoResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());             throw new RuntimeException(e);         }         return null;     }      /**      *  DescribeCdnDomainDetail    获取指定加速域名配置的基本信息      */     private static DescribeCdnDomainDetailResponse describeCdnDomainDetail(IAcsClient client, String domainName) {         DescribeCdnDomainDetailRequest request = new DescribeCdnDomainDetailRequest();         // 加速域名         request.setDomainName(domainName);         System.out.println("------------------describeCdnDomainDetail-------------------");         try {             DescribeCdnDomainDetailResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());             throw new RuntimeException(e);         }         return null;     }      /**      *  DescribeDomainsBySource    查询用户名下,源站对应的所有域名名称列表。不支持模糊匹配      */     private static DescribeDomainsBySourceResponse describeDomainsBySource(IAcsClient client) {         DescribeDomainsBySourceRequest request = new DescribeDomainsBySourceRequest();         // 源站,多个源站用逗号(,)隔开         request.setSources(source);         System.out.println("------------------describeDomainsBySource-------------------");         try {             DescribeDomainsBySourceResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());             throw new RuntimeException(e);         }         return null;      }      /**      * DescribeUserDomains    查询用户名下所有的域名与状态      */     private static DescribeUserDomainsResponse describeUserDomains(IAcsClient client) {         DescribeUserDomainsRequest request = new DescribeUserDomainsRequest();         // 域名查询类型:         // fuzzy_match:模糊匹配         // pre_match:前匹配         // suf_match:后匹配         // full_match: 完全匹配         // 默认值:fuzzy_match         request.setDomainSearchType("fuzzy_match");         // 域名模糊匹配过滤         // request.setDomainName("oos");         System.out.println("------------------describeUserDomains-------------------");         try {             DescribeUserDomainsResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());             throw new RuntimeException(e);         }         return null;     }      /**      * DescribeCdnService    查询CDN服务状态      */     private static DescribeCdnServiceResponse describeCdnService(IAcsClient client) {         DescribeCdnServiceRequest request = new DescribeCdnServiceRequest();         System.out.println("------------------describeCdnService-------------------");         try {             DescribeCdnServiceResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());             throw new RuntimeException(e);         }         return null;     }      /**      * DescribeCdnUserResourcePackage    查询CDN用户当前流量包      */     private static DescribeCdnUserResourcePackageResponse describeCdnUserResourcePackage(IAcsClient client) {         DescribeCdnUserResourcePackageRequest request = new DescribeCdnUserResourcePackageRequest();         System.out.println("------------------describeCdnUserResourcePackage-------------------");         try {             DescribeCdnUserResourcePackageResponse response = client.getAcsResponse(request);             System.out.println(gson.toJson(response));             return 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());                             throw new RuntimeException(e);         }         return null;                             }      /**      * 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);     }                                                     }                                                    

执行结果

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

------------------describeUserDomains------------------- {     "requestId": "9E640176-CEBA-412D-B457-B49709CE7BF5",     "pageNumber": 1,     "pageSize": 20,     "totalCount": 4,     "domains": [         {             "domainName": "videoliveoss.****.club",             "cname": "videoliveoss.****.club.w.alikunlun.com",             "cdnType": "web",             "domainStatus": "offline",             "gmtCreated": "2017-11-02T08:14:23Z",             "gmtModified": "2017-12-31T20:19Z",             "description": "沪ICP备170****号-1",             "sslProtocol": "off",             "resourceGroupId": "rg-acfm****",             "sandbox": "",             "sources": [                 {                     "type": "oss",                     "content": "videolive1.****.aliyuncs.com",                     "port": 80,                     "priority": "20",                     "weight": "10"                 }             ]         }     ] } ------------------describeDomainsBySource------------------- {     "requestId": "D38EEB4C-1086-47A8-93B9-0A0AF12E4BD3",     "sources": "videolive1.****.aliyuncs.com",     "domainsList": [         {             "source": "videolive1.****.aliyuncs.com",             "domainInfos": [                 {                     "domainName": "videoliveoss.****.club",                     "domainCname": "videoliveoss.****.club.w.alikunlun.com",                     "createTime": "2017-11-02T16:14:23+08:00",                     "updateTime": "2017-12-31T20:19:40+08:00",                     "status": "offline"                 }             ],             "domains": [                 "videoliveoss.****.club"             ]         }     ] } ------------------describeCdnDomainDetail------------------- {     "requestId": "94CDE19C-B451-4C5A-92E7-D71DE9F04AEC",     "getDomainDetailModel": {         "domainName": "video005.aliyuntest.club",         "cname": "video005.****.club.w.alikunlun.com",         "domainStatus": "online",         "cdnType": "web",         "gmtCreated": "2019-09-10T02:43:31Z",         "gmtModified": "2019-09-10T10:45Z",         "resourceGroupId": "rg-acf****y",         "description": "",         "scope": "domestic",         "sourceModels": [             {                 "content": "video006.****.club",                 "type": "domain",                 "port": 80,                 "enabled": "online",                 "priority": "20",                 "weight": "10"             }         ]     } } ------------------describeDomainCertificateInfo------------------- {     "requestId": "A28DC42C-97A1-4D7A-8146-C601566E8264",     "certInfos": [         {             "domainName": "video005.****.club",             "certName": "",             "certDomainName": "",             "certExpireTime": "",             "certLife": "",             "certOrg": "",             "certType": "",             "serverCertificateStatus": "off",             "status": "",             "serverCertificate": "",             "certUpdateTime": "",             "certStartTime": "",             "domainCnameStatus": "ok"         }     ] } ------------------describeCdnService------------------- {     "requestId": "9C2F4F67-8388-4BAA-82A0-325ECEE3E2C9",     "instanceId": "1231579085529123",     "internetChargeType": "PayByTraffic",     "openingTime": "2017-03-22T06:02:12Z",     "changingChargeType": "PayByTraffic",     "changingAffectTime": "2017-03-22T06:02:11Z",     "operationLocks": [...] } ------------------describeCdnUserResourcePackage------------------- {     "requestId": "558919B3-FBED-4AA4-A920-9BB948487608",     "resourcePackageInfos": [...] }