本教程详细介绍如何使用Alibaba Cloud SDK for Java查看POLARDB集群(MySQL集群)的详细属性。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用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-polardb -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-polardb</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClusterAttributeRequest;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClusterAttributeResponse;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClustersRequest;
import com.aliyuncs.polardb.model.v20170801.DescribeDBClustersResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.List;


/**
 * DescribeDBClusterAttribute    查看指定POLARDB集群的详细属性
 * DescribeDBClusters    查询POLARDB集群列表或被RAM授权的集群列表
 */
public class DescribeDBClusterAttribute {

    public static void main(String[] args) {

        // 初始化公共请求参数
        IAcsClient client = Initialization();
        // 查询运行中的MySQL集群列表
        List<DescribeDBClustersResponse.DBCluster> dbClusters = DescribeDBClusters(client);
        // 获取需要的集群ID
        for (DescribeDBClustersResponse.DBCluster dbCluster : dbClusters){
            // 集群ID
            String dbClusterId = dbCluster.getDBClusterId();
            // 查看MySQL集群的详细属性
            DescribeDBClusterAttributeResponse describeDBClusterAttributeResponse = DescribeDBClusterAttribute(client, dbClusterId);
            // 处理返回参数和业务逻辑
            System.out.println(new Gson().toJson(describeDBClusterAttributeResponse));
        }
    }

    /**
     * DescribeDBClusterAttribute    查看指定POLARDB集群的详细属性
     */
    private static DescribeDBClusterAttributeResponse DescribeDBClusterAttribute(IAcsClient client, String dbClusterId) {
        DescribeDBClusterAttributeRequest request = new DescribeDBClusterAttributeRequest();
        // 集群ID
        request.setDBClusterId(dbClusterId);
        try {
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * DescribeDBClusters    查询运行中的MySQL集群列表
     */
    private static List<DescribeDBClustersResponse.DBCluster> DescribeDBClusters(IAcsClient client) {
        DescribeDBClustersRequest request = new DescribeDBClustersRequest();
        // 数据库类型,取值
        // MySQL
        // PostgreSQL
        // Oracle
        request.setDBType("MySQL");
        // 集群状态
        // Creating    创建中
        // Running    运行中
        // Deleting    释放中
        // Rebooting    重启中
        // DBNodeCreating    正在增加节点
        // DBNodeDeleting    正在删除节点
        // ClassChanging    正在变更节点规格
        // NetAddressCreating    正在创建网络连接
        // NetAddressDeleting    正在删除网络连接
        // NetAddressModifying    正在修改网络连接
        request.setDBClusterStatus("Running");
        try {
            DescribeDBClustersResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            return response.getItems();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
            throw new RuntimeException();
        }
    }

    /**
     * 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);
    }
}

执行结果

正确的执行结果类似如下:
---------------------查询运行中的MySQL集群列表-------------------
{
    "requestId": "30745EE3-82B0-4011-AEE3-9E1C892B97CE", 
    "pageNumber": 1, 
    "totalRecordCount": 0, 
    "pageRecordCount": 0, 
    "items": [...]
}
---------------------查询MySQL集群详细属性-------------------
{
    "DBVersion": "5.6",
    "LockMode": "Unlock",
    "DBClusterDescription": "test",
    "DBClusterNetworkType": "VPC",
    "DBClusterId": "pc-xxxxxxxxxxxxxx",
    "VSwitchId": "vsw-xxxxxxxxxxxxxx",
    "Engine": "POLARDB",
    "DBClusterStatus": "Running",
    "CreationTime": "2019-04-26T06:01:28Z",
    "MaintainTime": "18:00Z-19:00Z",
    "Tags": [],
    "VPCId": "vpc-xxxxxxxxxxxxxx",
    "ExpireTime": "",
    "Expired": false,
    "RequestId": "4E148395-950A-46F8-BFF8-274A64CD793B",
    "RegionId": "cn-qingdao",
    "DBType": "MySQL",
    "DBNodes": [
        {
            "CreationTime": "2019-04-26T22:01:28Z",
            "MaxIOPS": 8000,
            "DBNodeRole": "Writer",
            "MaxConnections": 1200,
            "DBNodeClass": "polar.mysql.x2.medium",
            "DBNodeStatus": "Running",
            "ZoneId": "cn-qingdao-c",
            "DBNodeId": "pi-xxxxxxxxxxxxxx"
        },
        {
            "CreationTime": "2019-04-26T22:01:28Z",
            "MaxIOPS": 8000,
            "DBNodeRole": "Reader",
            "MaxConnections": 1200,
            "DBNodeClass": "polar.mysql.x2.medium",
            "DBNodeStatus": "Running",
            "ZoneId": "cn-qingdao-c",
            "DBNodeId": "pi-xxxxxxxxxxxxxx"
        }
    ],
    "SQLSize": 0,
    "PayType": "Postpaid"
}