本教程详细介绍如何使用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 -->
         <dependency>
             <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-bssopenapi -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-bssopenapi</artifactId>
             <version>1.4.2</version>
         </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.bssopenapi.model.v20171214.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询产品信息和价格
 */
public class QueryProductInfoAndPriceDemo {

    /**
     * 查询产品概览信息
     */
    private QueryProductListResponse queryProductList(QueryProductListRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 查询产品详细模块信息
     */
    private DescribePricingModuleResponse describePricingModule(DescribePricingModuleRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 查询价格
     */
    private GetPayAsYouGoPriceResponse getPayAsYouGoPrice(GetPayAsYouGoPriceRequest request, IAcsClient client) {
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    public static void main(String[] args) {
        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-qingdao",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        QueryProductInfoAndPriceDemo queryProductInfoAndPriceDemo = new QueryProductInfoAndPriceDemo();

        // 查询产品概览信息
        QueryProductListRequest queryProductListRequest = new QueryProductListRequest();
        // 设置查询页码
        queryProductListRequest.setPageNum(1);
        // 设置页大小
        queryProductListRequest.setPageSize(50);
        // 设置是否返回总数
        queryProductListRequest.setQueryTotalCount(true);
        QueryProductListResponse queryProductListResponse = queryProductInfoAndPriceDemo.queryProductList(queryProductListRequest, client);
        log_print("queryProductList", queryProductListResponse);

        // 查询产品详细模块信息
        DescribePricingModuleRequest describePricingModuleRequest = new DescribePricingModuleRequest();
        // 产品代码
        describePricingModuleRequest.setProductCode("ecs");
        // 订阅类型。Subscription:预付费,PayAsYouGo:后付费
        describePricingModuleRequest.setSubscriptionType("PayAsYouGo");
        // 产品类型
        describePricingModuleRequest.setProductType("");
        DescribePricingModuleResponse describePricingModuleResponse = queryProductInfoAndPriceDemo.describePricingModule(describePricingModuleRequest, client);
        log_print("describePricingModule", describePricingModuleResponse);

        // 查询价格
        GetPayAsYouGoPriceRequest getPayAsYouGoPriceRequest = new GetPayAsYouGoPriceRequest();
        getPayAsYouGoPriceRequest.setProductCode("ecs");
        getPayAsYouGoPriceRequest.setSubscriptionType("PayAsYouGo");
        // 计价模块列表
        List<GetPayAsYouGoPriceRequest.ModuleList> moduleLists = new ArrayList<>();

        // 实例规格
        GetPayAsYouGoPriceRequest.ModuleList moduleList1 = new GetPayAsYouGoPriceRequest.ModuleList();
        moduleList1.setModuleCode("InstanceType");
        moduleList1.setPriceType("Hour");
        moduleList1.setConfig("InstanceType:ecs.g5.xlarge,IoOptimized:IoOptimized,ImageOs:linux");
        moduleLists.add(moduleList1);

        // 系统盘规格
        GetPayAsYouGoPriceRequest.ModuleList moduleList2 = new GetPayAsYouGoPriceRequest.ModuleList();
        moduleList2.setModuleCode("SystemDisk");
        moduleList2.setPriceType("Hour");
        moduleList2.setConfig("SystemDisk.Category:cloud_efficiency,SystemDisk.Size:55");
        moduleLists.add(moduleList2);

        // 带宽规格
        GetPayAsYouGoPriceRequest.ModuleList moduleList3 = new GetPayAsYouGoPriceRequest.ModuleList();
        moduleList3.setModuleCode("InternetMaxBandwidthOut");
        moduleList3.setPriceType("Hour");
        moduleList3.setConfig("InternetMaxBandwidthOut:1024");
        moduleLists.add(moduleList3);

        getPayAsYouGoPriceRequest.setModuleLists(moduleLists);
        GetPayAsYouGoPriceResponse payAsYouGoPriceResponse = queryProductInfoAndPriceDemo.getPayAsYouGoPrice(getPayAsYouGoPriceRequest, client);
        log_print("getPayAsYouGoPrice", payAsYouGoPriceResponse);
    }
}

运行结果

正确运行结果类似如下:
-------------------------------queryProductList-------------------------------
{
    "requestId": "EAE92F84-222B-49DE-84A6-A8C8CBAD041C",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "totalCount": 457,
        "pageNum": 1,
        "pageSize": 500,
        "productList": [
            {
                "productCode": "bandwidth_increce",
                "productName": "增量带宽",
                "productType": "",
                "subscriptionType": "Subscription"
            },
            {
                "productCode": "bwp",
                "productName": "NAT共享带宽包(按量付费)",
                "productType": "",
                "subscriptionType": "PayAsYouGo"
            },
            ......
            ......
            ......
            {
                "productCode": "cdn",
                "productName": "内容分发网络CDN(按量付费)",
                "productType": "CDN",
                "subscriptionType": "PayAsYouGo"
            }
        ]
    }
}
-------------------------------describePricingModule-------------------------------
{
    "requestId": "38A8AC31-2FAD-4B26-A4F1-2AA09DE8C93D",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "moduleList": [
            {
                "moduleCode": "DataDisk",
                "moduleName": "数据盘",
                "priceType": "Hour",
                "currency": "CNY",
                "configList": [
                    "DataDisk.Size",
                    "Region",
                    "DataDisk.Category"
                ]
            },
            {
                "moduleCode": "InstanceType",
                "moduleName": "实例",
                "priceType": "Hour",
                "currency": "CNY",
                "configList": [
                    "ImageOs",
                    "IoOptimized",
                    "Region",
                    "InstanceType"
                ]
            },
            {
                "moduleCode": "SystemDisk",
                "moduleName": "系统盘",
                "priceType": "Hour",
                "currency": "CNY",
                "configList": [
                    "Region",
                    "SystemDisk.Category",
                    "SystemDisk.Size"
                ]
            },
            {
                "moduleCode": "InternetMaxBandwidthOut",
                "moduleName": "公网带宽",
                "priceType": "Hour",
                "currency": "CNY",
                "configList": [
                    "InternetMaxBandwidthOut",
                    "Region"
                ]
            },
            {
                "moduleCode": "InternetTrafficOut",
                "moduleName": "公网流量",
                "priceType": "Usage",
                "currency": "CNY",
                "configList": [
                    "Region",
                    "InternetTrafficOut"
                ]
            }
        ],
        "attributeList": [
            {
                "code": "Region",
                "name": "地域",
                "values": [
                    {
                        "type": "single_string",
                        "name": "华北 1",
                        "value": "cn-qingdao-cm5-a01"
                    },
                    {
                        "type": "single_string",
                        "name": "华北 2",
                        "value": "cn-beijing-btc-a01"
                    },
                    ......
                    ......
                    ......
                ]
            },
            {
                "code": "DataDisk.Category",
                "name": "数据盘种类",
                "values": [
                    {
                        "type": "single_string",
                        "name": "SSD 云盘",
                        "value": "cloud_ssd"
                    },
                    {
                        "type": "single_string",
                        "name": "高效云盘",
                        "value": "cloud_efficiency"
                    },
                    {
                        "type": "single_string",
                        "name": "普通云盘",
                        "value": "cloud"
                    },
                    {
                        "type": "single_string",
                        "name": "本地 SSD 盘",
                        "value": "ephemeral_ssd"
                    }
                ]
            },
            ......
            ......
            ......
        ]
    }
}
-------------------------------getPayAsYouGoPrice-------------------------------
{
    "requestId": "A9D1FC7D-9C3B-492C-9404-11F30ADDFD45",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "currency": "CNY",
        "moduleDetails": [
            {
                "moduleCode": "InstanceType",
                "originalCost": 0.685483,
                "invoiceDiscount": 0.685483,
                "costAfterDiscount": 0.0,
                "unitPrice": 0.0
            },
            {
                "moduleCode": "SystemDisk",
                "originalCost": 0.025873,
                "invoiceDiscount": 0.025873,
                "costAfterDiscount": 0.0,
                "unitPrice": 0.0
            },
            {
                "moduleCode": "InternetMaxBandwidthOut",
                "originalCost": 0.030913,
                "invoiceDiscount": 0.030913,
                "costAfterDiscount": 0.0,
                "unitPrice": 0.0
            }
        ],
        "promotionDetails": [
            {
                "promotionName": "xxxxxxx",
                "promotionId": 1001199213
            }
        ]
    }
}