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

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、第一个方法:创建SDK客户端实例

所有解析记录的操作都要通过这个客户端实例来进行,所以要首先创建这个实例,需要阿里云的AccessKey(AppId和AppSecret)

  
/**
* <p>
* 创建客户端实例
* </p>
*
* @return
* @throws Exception
*/
private Client createClient() throws Exception{
AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey参数
if(api == null) throw new ErrException("未配置阿里云API参数!");
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 这个参数,所以提前写一个获取记录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、第四个方法:删除指定的记录

这个很简单,根据查找到的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()));
}
}
全部评论

相关推荐

点赞 评论 收藏
分享
04-16 10:27
已编辑
美团_Saas_后端开发
今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,拿到美团offer那会感觉自己天都亮了。没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务