

阿里云 API JDK 使用,阿里jdk阿里云 API JDK 使用通過(guò)阿里云API 獲取ECS 系統(tǒng)狀態(tài)阿里云提供了非常完善的API SDK 和文檔OpenAPI Explorer 快速檢索、可視化調(diào)試 API、在線(xiàn)命令行工具、同步動(dòng)態(tài)生成可執(zhí)行 SDK Example 代碼 Java SDK使用手冊(cè) for 云監(jiān)控......
通過(guò)阿里云API 獲取ECS 系統(tǒng)狀態(tài)
阿里云提供了非常完善的API SDK 和文檔
OpenAPI Explorer 快速檢索、可視化調(diào)試 API、在線(xiàn)命令行工具、同步動(dòng)態(tài)生成可執(zhí)行 SDK Example 代碼 Java SDK使用手冊(cè) for 云監(jiān)控 API 網(wǎng)關(guān)錯(cuò)誤代碼表 檢查API 錯(cuò)誤信息
Aliyun JDK的 使用
1. 添加依賴(lài)
根據(jù)需要使用到的API接口,添加對(duì)應(yīng)的包.最新版本需要自己去 JDK 頁(yè)面查看
aliyunjavasdkcore 核心,必須引入
aliyunjavasdkcms 云監(jiān)控API(獲取CUP/MEM等信息,需要訪(fǎng)問(wèn))
aliyunjavasdkecs ECS API(可以創(chuàng)建,查詢(xún)ECS 實(shí)例,我用來(lái)查詢(xún)名下的所有實(shí)例) pom.xml
dependency
groupIdcom.aliyun/groupId
artifactIdaliyunjavasdkcore/artifactId
version4.4.2/version
/dependency
dependency
groupIdcom.aliyun/groupId
artifactIdaliyunjavasdkcms/artifactId
version7.0.4/version
/dependency
dependency
groupIdcom.aliyun/groupId
artifactIdaliyunjavasdkecs/artifactId
version4.16.11/version
/dependency
2. 申請(qǐng)AccessKey
訪(fǎng)問(wèn) AccessKey管理頁(yè)面 創(chuàng)建成功后會(huì)獲得最重要的數(shù)據(jù)AccessKeyId 和 AccessKeySecret 注意保存CSV文件,這個(gè)數(shù)據(jù)只會(huì)顯示一次.別直接關(guān)閉提示窗
3. 創(chuàng)建請(qǐng)求
OpenAPI Explorer 可以生成簡(jiǎn)單SDK源碼,非常適合用來(lái)測(cè)試 這里我創(chuàng)建一個(gè)查詢(xún) DescribeMetricLastDemo.java
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 DescribeMetricLastDemo {
public static void main(String[] args) {
/** 創(chuàng)建配置文件
* regionId: 你的ECS的地域信息,我的是在華東杭州所以是cnhangzhou,可以在API頁(yè)面查到其他地域的字符串,
* accessKeyId 和 accessSecret 則是上一步申請(qǐng)的字符串
*/
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
// 創(chuàng)建發(fā)快遞請(qǐng)求的客戶(hù)端
IAcsClient client = new DefaultAcsClient(profile);
//我需要查詢(xún)指定監(jiān)控對(duì)象的最新監(jiān)控?cái)?shù)據(jù)
//根據(jù)文檔,我需要使用 DescribeMetricLastRequest 進(jìn)行請(qǐng)求,基本上JDK里面,API名字+Request就是需要調(diào)用的對(duì)象
DescribeMetricLastRequest request = new DescribeMetricLastRequest();
//命名空間,表明監(jiān)控?cái)?shù)據(jù)所屬產(chǎn)品,如 “acsecsdashboard”,“acsrdsdashboard”等
request.setNamespace(acsecsdashboard);
//監(jiān)控項(xiàng)名稱(chēng)。 根據(jù)預(yù)設(shè)監(jiān)控項(xiàng)參考,CUP 信息對(duì)應(yīng)的是 CPUUtilization
request.setMetricName(CPUUtilization);
try {
//發(fā)快遞請(qǐng)求,得到對(duì)應(yīng)response
DescribeMetricLastResponse 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());
}
}
}
阿里云API JDK 小結(jié):
API名稱(chēng) + Request/Response就是需要使用的對(duì)象
使用都是分成2步,創(chuàng)建Request然后設(shè)置參數(shù),然后通過(guò)IAcsClient發(fā)快遞
IAcsClient 創(chuàng)建后可以多次調(diào)用
特別聲明:以上文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),不代表ESG跨境電商觀(guān)點(diǎn)或立場(chǎng)。如有關(guān)于作品內(nèi)容、版權(quán)或其它問(wèn)題請(qǐng)于作品發(fā)表后的30日內(nèi)與ESG跨境電商聯(lián)系。
二維碼加載中...
使用微信掃一掃登錄
使用賬號(hào)密碼登錄
平臺(tái)顧問(wèn)
微信掃一掃
馬上聯(lián)系在線(xiàn)顧問(wèn)
小程序
ESG跨境小程序
手機(jī)入駐更便捷
返回頂部