日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA实现对阿里云DNS的解析管理

發(fā)布時間:2023/12/8 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA实现对阿里云DNS的解析管理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、阿里云DNS的SDK依賴

<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.0.19</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alidns20150109</artifactId>
<version>2.0.1</version>
</dependency>

2、第一個方法:創(chuàng)建SDK客戶端實例

所有解析記錄的操作都要通過這個客戶端實例來進行,所以要首先創(chuàng)建這個實例,需要阿里云的AccessKey(AppId和AppSecret)

/**
* <p>
* 創(chuàng)建客戶端實例
* </p>
*
* @return
* @throws Exception
*/
private Client createClient() throws Exception{
AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey參數(shù)
if(api == null) throw new ErrException("未配置阿里云API參數(shù)!");
Config config = new Config();
config.accessKeyId = api.getAppId();
config.accessKeySecret = api.getAppSecret();
config.endpoint = "alidns.cn-beijing.aliyuncs.com";
return new Client(config);
}

3、第二個方法:返回指定的記錄ID(RecordId)

在阿里云的SDK中,對解析記錄進行修改和刪除時,都需要傳入 RecordId 這個參數(shù),所以提前寫一個獲取記錄ID的方法。

/**
* <p>
* 返回指定主機記錄的ID,不存在時返回null
* </p>
*
* @param DomainName
* @param RR 記錄名稱
* @return
*/
private String getRecId(Client client, String DomainName, String RR){
String recId = null;
try {
DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
request.setDomainName(DomainName);
request.setRRKeyWord(RR);
DescribeDomainRecordsResponse response = client.describeDomainRecords(request);
if(response.getBody().getTotalCount() > 0){
List<DescribeDomainRecordsResponseBodyDomainRecordsRecord> recs = response.getBody().getDomainRecords().getRecord();
for(DescribeDomainRecordsResponseBodyDomainRecordsRecord rec: recs){
if(rec.getRR().equalsIgnoreCase(RR)){
recId = rec.getRecordId();
break;
}
}
}
} catch (Exception e) {
}
return recId;
}

4、第三個方法:添加或修改指定的記錄

方便起見,這里我將添加和修改集成到了一個方法,相當于SaveOrUpdate。

/**
* <p>
* 添加或修改解析記錄
* </p>
*
* @param DomainName 域名
* @param RR 記錄名稱
* @param Type 記錄類型(A、AAAA、MX、TXT、CNAME)
* @param Value 記錄值
*/
public void update(String DomainName, String RR, String Type, String Value){
try {
if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
if(EStr.isEmpty(RR)) throw new RuntimeException("主機記錄(RR)為空!");
if(EStr.isEmpty(Type)) throw new RuntimeException("記錄類型(Type)為空!");
if(EStr.isEmpty(Value)) throw new RuntimeException("記錄值(Value)為空!");
Client client = createClient();
String recId = getRecId(client, DomainName, RR);
if(EStr.isNull(recId)){ //添加
AddDomainRecordRequest request = new AddDomainRecordRequest();
request.setDomainName(DomainName);
request.setRR(RR);
request.setType(Type);
request.setValue(Value);
AddDomainRecordResponse response = client.addDomainRecord(request);
recId = response.getBody().getRecordId();
}else{ //修改
UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
request.setRecordId(recId);
request.setRR(RR);
request.setType(Type);
request.setValue(Value);
UpdateDomainRecordResponse response = client.updateDomainRecord(request);
recId = response.getBody().getRecordId();
}
renderJson(Result.success("recId", recId));
} catch (Exception e) {
renderJson(Result.fail(e.getMessage()));
}
}

5、第四個方法:刪除指定的記錄

這個很簡單,根據(jù)查找到的RecordId直接刪除即可。

/**
* <p>
* 刪除記錄
* </p>
*
* @param DomainName
* @param RR
*/
public void remove(String DomainName, String RR){
try {
if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
if(EStr.isEmpty(RR)) throw new RuntimeException("主機記錄(RR)為空!");
Client client = createClient();
String recId = getRecId(client, DomainName, RR);
if(EStr.isNull(recId)){
renderJson(Result.success("recId", null));
}else{
DeleteDomainRecordRequest request = new DeleteDomainRecordRequest();
request.setRecordId(recId);
DeleteDomainRecordResponse response = client.deleteDomainRecord(request);
renderJson(Result.success("recId", response.getBody().getRecordId()));
}
} catch (Exception e) {
renderJson(Result.fail(e.getMessage()));
}
}

總結

以上是生活随笔為你收集整理的JAVA实现对阿里云DNS的解析管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。