本教程详细介绍如何使用Alibaba Cloud SDK for Java查询RDS实例(SQL Server)的基本信息和配置信息。

前提条件

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

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

操作示例

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.rds.model.v20140815.*; import com.google.gson.Gson; import java.util.List;  /**  * DescribeDBInstances            查看RDS实例列表或被RAM授权的实例列表  * DescribeAccounts            查看实例的帐号信息  * DescribeDBInstanceIpHostname    查询RDS实例的底层ECS实例的hostname  * DescribeDBInstanceHAConfig    查询RDS实例高可用模式和数据复制方式  * DescribeDBInstanceMonitor    查询监控频率  * DescribeDBInstanceNetInfo    查看实例的所有连接地址信息  */ public class TestQueryRDS {      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);     }      public static void main(String[] args) {         // 初始化请求参数配置client。         IAcsClient client = Initialization();         // 查询RDS实例列表(和详细信息),获取实例ID。         List<DescribeDBInstancesResponse.DBInstance> items = DescribeDBInstances(client);         for (DescribeDBInstancesResponse.DBInstance dbInstance : items) {             String dbInstanceId = dbInstance.getDBInstanceId();             // 查看实例账号信息。             String accounts = DescribeAccounts(dbInstanceId, client);             // 查询RDS实例的底层ECS实例的hostname。             String IpHostnameInfos = DescribeDBInstanceIpHostname(client, dbInstanceId);             // 查询RDS实例高可用模式和数据复制方式。             String dbInstanceHAConfig = DescribeDBInstanceHAConfig(client, dbInstanceId);             // 查询监控频率。             String Period = DescribeDBInstanceMonitor(client, dbInstanceId);             // 查看实例的所有连接地址信息             String DBInstanceNetInfos = DescribeDBInstanceNetInfo(client, dbInstanceId);              System.out.println("--------------------Items实例信息--------------------");             System.out.println(new Gson().toJson(dbInstance));             System.out.println("--------------------accounts实例帐号信息--------------------");             System.out.println(accounts);             System.out.println("--------------------IpHostnameInfosRDS实例的底层ECS实例的hostname--------------------");             System.out.println(IpHostnameInfos);             System.out.println("--------------------RDS实例高可用模式和数据复制方式--------------------");             System.out.println(dbInstanceHAConfig);             System.out.println("--------------------Period监控的采集数据间隔--------------------");             System.out.println(Period);             System.out.println("--------------------DBInstanceNetInfos实例的连接地址信息列表--------------------");             System.out.println(DBInstanceNetInfos);         }     }      /**      * DescribeDBInstanceNetInfo    查看实例的所有连接地址信息      */     private static String DescribeDBInstanceNetInfo(IAcsClient client, String dbInstanceId) {         DescribeDBInstanceNetInfoRequest request = new DescribeDBInstanceNetInfoRequest();         request.setDBInstanceId(dbInstanceId);         // 连接地址类型,取值:         // Normal:普通连接地址;         // ReadWriteSplitting:读写分离连接地址。         // 默认返回所有类型连接地址。         request.setDBInstanceNetRWSplitType("Normal");         try {             DescribeDBInstanceNetInfoResponse response = client.getAcsResponse(request);             return new Gson().toJson(response.getDBInstanceNetInfos());         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     }      /**      * DescribeDBInstanceMonitor    查询监控频率      */     private static String DescribeDBInstanceMonitor(IAcsClient client, String dbInstanceId) {         DescribeDBInstanceMonitorRequest request = new DescribeDBInstanceMonitorRequest();         request.setDBInstanceId(dbInstanceId);         try {             DescribeDBInstanceMonitorResponse response = client.getAcsResponse(request);             return new Gson().toJson(response.getPeriod());         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     }      /**      * DescribeDBInstanceHAConfig    查询RDS实例高可用模式和数据复制方式。      */     public static String DescribeDBInstanceHAConfig(IAcsClient client, String dbInstanceId) {         DescribeDBInstanceHAConfigRequest request = new DescribeDBInstanceHAConfigRequest();         request.setDBInstanceId(dbInstanceId);         try {             DescribeDBInstanceHAConfigResponse response = client.getAcsResponse(request);             return new Gson().toJson(response);         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     }      /**      * DescribeDBInstanceIpHostname    接口查询RDS实例的底层ECS实例的hostname。      */     public static String DescribeDBInstanceIpHostname(IAcsClient client, String dbInstanceId) {         DescribeDBInstanceIpHostnameRequest request = new DescribeDBInstanceIpHostnameRequest();         request.setDBInstanceId(dbInstanceId);          try {             DescribeDBInstanceIpHostnameResponse response = client.getAcsResponse(request);             return new Gson().toJson(response.IpHostnameInfos());         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     }      /**      * DescribeAccounts    查看实例的帐号信息。      *      * @return 账号信息列表。      */     public static String DescribeAccounts(String dbInstanceId, IAcsClient client) {         DescribeAccountsRequest request = new DescribeAccountsRequest();         // RDS实例ID。         request.setDBInstanceId(dbInstanceId);         try {             DescribeAccountsResponse response = client.getAcsResponse(request);             return new Gson().toJson(response.getAccounts());         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     }      /**      * DescribeDBInstances    查看RDS实例列表      *      * @return 由实例信息组成的数组。      */     public static List<DescribeDBInstancesResponse.DBInstance> DescribeDBInstances(IAcsClient client) {          DescribeDBInstancesRequest request = new DescribeDBInstancesRequest();         // 可用区ID。         request.setZoneId("cn-hangzhou-i");         // 实例的过期状态,取值:         // rue:已过期。         // False:未过期         request.setExpired("False");         // 实例类型,取值:         // Primary:主实例;         // Readonly:只读实例;         // Guard:灾备实例;         // Temp:临时实例。         request.setDBInstanceType("Primary");         // 实例的网络类型         request.setInstanceNetworkType("VPC");         // 数据库类型,取值:         // MySQL;         // SQLServer;         // PostgreSQL;         // PPAS;         // MariaDB。         // 默认返回所有数据库类型。         request.setEngine("SQLServer");         try {             DescribeDBInstancesResponse response = client.getAcsResponse(request);             return response.getItems();         } catch (ClientException e) {             System.out.println("ErrCode:" + e.getErrCode());             System.out.println("ErrMsg:" + e.getErrMsg());             System.out.println("RequestId:" + e.getRequestId());             return null;         }     } }

执行

正确的返回结果类似如下:

--------------------Items实例信息-------------------- {     "insId": 1,      "dBInstanceId": "rm-bp162x151xxxxx",      "payType": "Postpaid",      "dBInstanceType": "Primary",      "regionId": "cn-hangzhou",      "expireTime": "",      "dBInstanceStatus": "Running",      "engine": "SQLServer",      "dBInstanceNetType": "Intranet",      "connectionMode": "Standard",      "lockMode": "Unlock",      "dBInstanceClass": "mssql.x4.medium.e2",      "instanceNetworkType": "VPC",      "vpcCloudInstanceId": "rm-bp162x15xxxxx",      "lockReason": "",      "zoneId": "cn-hangzhou-i",      "mutriORsignle": false,      "createTime": "2019-09-10T06:39:07Z",      "engineVersion": "2017_ent_ag",      "vpcId": "vpc-bp1gnu8brxxxxx",      "vSwitchId": "vsw-bp1j5miwxxxxx",      "resourceGroupId": "rg-acfmxazb4ph6aiy",      "readOnlyDBInstanceIds": [...] } --------------------accounts实例帐号信息-------------------- [     {         "dBInstanceId": "rm-bp162xxxxx",          "accountName": "test1122",          "accountStatus": "Available",          "accountType": "Normal",          "accountDescription": "",          "privExceeded": "0",          "databasePrivileges": [...]     } ] --------------------IpHostnameInfosRDS实例的底层ECS实例的hostname-------------------- {     "IpHostnameInfos":"172.16.xx.xx,sdxxxxxxxxB;172.16.xx.xx,sdxxxxxxxxA",     "RequestId":"67CD4719-51E3-4A76-A38C-02F45FAE7E36",     "DBInstanceId":"rm-uf6wjk5xxxxxxx" } --------------------RDS实例高可用模式和数据复制方式-------------------- {     "requestId": "E041B918-A8E0-4AD0-82FD-7916B2143962",      "dBInstanceId": "rm-bp162x15xxxxx",      "hostInstanceInfos": [         {...},          {...}     ] } --------------------Period监控的采集数据间隔-------------------- "300" --------------------DBInstanceNetInfos实例的连接地址信息列表-------------------- [     {         "upgradeable": "Disabled",          "connectionString": "rm-bp162x15xxxxx.sqlserver.rds.aliyuncs.com",          "iPAddress": "172.16.xx.xx",          "iPType": "Private",          "port": "1433",          "vPCId": "vpc-bp1gnxxxxx",          "vSwitchId": "vsw-bp1j5miw2xxxxx",          "connectionStringType": "Normal",          "securityIPGroups": [...],          "dBInstanceWeights": [...]     } ]