JAVA实现对阿里云DNS的解析管理
作者:网无忌 时间:2023-11-24 13:25:15
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()));
}
}
6、完整代码
package itez.alidns.controller;
import java.util.List;
import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.AddDomainRecordRequest;
import com.aliyun.alidns20150109.models.AddDomainRecordResponse;
import com.aliyun.alidns20150109.models.DeleteDomainRecordRequest;
import com.aliyun.alidns20150109.models.DeleteDomainRecordResponse;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;
import com.aliyun.teaopenapi.models.Config;
import itez.core.wrapper.controller.ControllerDefine;
import itez.core.wrapper.controller.EController;
import itez.kit.EStr;
import itez.kit.exception.ErrException;
import itez.kit.restful.Result;
import itez.plat.main.model.CompWx;
import itez.plat.main.service.CompWxService;
import itez.weixin.api.ApiConfigKit.ConfigType;
/**
* <p>
* 阿里云DNS解析
* 示例:http://localhost/alidns/update?DomainName=domain.com&RR=test&Type=A&Value=8.8.8.8
* </p>
*
* <p>Copyright(C) 2017-2022 <a href="http://www.itez.com.cn">上游科技</a></p>
*
* @author <a href="mailto:netwild@qq.com">Z.Mingyu</a>
* @date 2022年1月12日 下午2:38:31
*/
@ControllerDefine(key = "/alidns", summary = "阿里云DNS解析", view = "/")
public class AliDnsController extends EController {
/**
* <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()));
}
}
/**
* <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()));
}
}
/**
* <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;
}
/**
* <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);
}
}
来源:https://www.cnblogs.com/netWild/p/15815757.html
标签:JAVA,阿里云,DNS
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring MVC参数传递中文乱码解决方法分享
2023-05-11 02:10:34
C# WinForm开发中使用XML配置文件实例
2022-09-07 23:04:03
浅析Java中Split函数的用法技巧
2023-02-19 09:13:27
解析C#设计模式编程中备忘录模式的运用
2023-06-10 11:40:00
![](https://img.aspxhome.com/file/2023/8/71768_0s.jpg)
Android SQLite基本用法详解
2023-07-02 05:14:58
![](https://img.aspxhome.com/file/2023/4/138404_0s.jpg)
Android中Button实现点击换图案及颜色
2022-06-28 00:42:10
![](https://img.aspxhome.com/file/2023/8/120128_0s.jpg)
WinFrom中label背景透明的实现方法
2023-05-08 13:05:40
UGUI ScrollRect滑动定位优化详解
2021-12-06 09:56:13
![](https://img.aspxhome.com/file/2023/5/94605_0s.jpg)
8种android 对话框(Dialog)使用方法详解
2023-07-09 18:23:44
为何Linq的Distinct实在是不给力
2023-05-11 17:03:18
![](https://img.aspxhome.com/file/2023/8/113798_0s.png)
Android仿ios年龄、生日、性别滚轮效果
2022-02-22 11:59:03
![](https://img.aspxhome.com/file/2023/2/137412_0s.jpg)
flutter中的布局和响应式app方法示例
2023-02-19 19:14:52
![](https://img.aspxhome.com/file/2023/3/137283_0s.jpg)
c#只读字段和常量的区别,以及静态构造函数的使用实例
2023-10-31 09:52:20
mybatisPlus返回Map类型的集合
2022-01-31 13:37:07
c# HttpClient设置超时的步骤
2023-05-07 10:42:29
带你了解Java数据结构和算法之无权无向图
2023-12-24 10:54:05
![](https://img.aspxhome.com/file/2023/0/92700_0s.jpg)
C#中值类型和引用类型的区别深度分析
2022-07-21 21:29:26
C#多线程用法详解
2022-07-09 10:51:08
![](https://img.aspxhome.com/file/2023/6/119556_0s.jpg)
android中SQLite使用及特点
2023-07-24 23:33:28
![](https://img.aspxhome.com/file/2023/5/126675_0s.png)
Java异常 Exception类及其子类(实例讲解)
2022-04-05 13:40:16