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()));
}
}
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);
}
}
JAVA实现对阿里云DNS的解析管理的更多相关文章
- 阿里云DNS api接口 shell 更改DNS解析
可定时任务检查域名解析,调用alidns.sh更新DNS解析 #!/bin/bash # alidns.sh #https://www.cnblogs.com/elvi/p/11663910.html ...
- 通过python将阿里云DNS解析作为DDNS使用
通过python将阿里云DNS解析作为DDNS使用 脚本需要Python2.x运行 安装alidns python sdk sudo pip install aliyun-python-sdk-ali ...
- 阿里云 云解析使用方法/在阿里云ESC服务器解析域名并绑定服务器IP后上传文件通过域名访问步骤教程
第一步:登录阿里云官网,获取服务器ECS的指定公网IP地址. 1.输入阿里云官网账号进入首页,如下图: 2.点击进入"管理控制台",如下图: 3.点击"云服务器ECS&q ...
- 基于阿里云 DNS API 实现的 DDNS 工具
0.简要介绍 0.1 思路说明 AliDDNSNet 是基于 .NET Core 开发的动态 DNS 解析工具,借助于阿里云的 DNS API 来实现域名与动态 IP 的绑定功能.工具核心就是调用了阿 ...
- 【原】命令行增删改查阿里云 DNS
命令行解析阿里云 DNS 项目地址:https://github.com/liyongjian5179/alidns 首先需要获取阿里云账号账号的AccessKeyID及AccessKeySecret ...
- 五:用JAVA写一个阿里云VPC Open API调用程序
用JAVA写一个阿里云VPC Open API调用程序 摘要:用JAVA拼出来Open API的URL 引言 VPC提供了丰富的API接口,让网络工程是可以通过API调用的方式管理网络资源.用程序和软 ...
- 利用InformationSchema与阿里云交易和账单管理API实现MaxCompute费用对账分摊统计
利用MaxCompute InformationSchema与阿里云交易和账单管理API 实现MaxCompute费用对账分摊统计 一.需求场景分析 很多的企业用户选择MaxCompute按量付费模式 ...
- 通过阿里云域名动态解析 IP 地址
这两天在家里用树莓派折腾了一个家用服务器,主要用来做 mac 的 Time Machine ,还有就是当做下载机和 nas ,想着平时上班时间家里没人用网络,空着也是空着,就可以利用空闲带宽下个美剧啥 ...
- 没固定公网 IP 的公司内网实现动态域名解析( 阿里云万网解析 )
情景说明 前段时间应公司需求,需要将内网的服务映射到公网.由于公司使用的是类似家庭宽带的线路,没有固定的公网 IP 地址,所以决定使用域名来完成. 当时有几种方案: 1.花生壳:但是目前需要乱七八糟的 ...
随机推荐
- 2. Go中defer使用注意事项
1. 简介 defer 会在当前函数返回前执行传入的函数,它会经常被用于关闭文件描述符.关闭数据库连接以及解锁资源. 理解这句话主要在三个方面: 当前函数 返回前执行,当然函数可能没有返回值 传入的函 ...
- LuoguP5006 [yLOI2018] 大美江湖 题解
Content 题意实在是太过复杂了,因此请回到题面查看. Data Range 本部分和 Solution 部分变量的含义同题面. \(1\leqslant n,m\leqslant 100,0\l ...
- java 编程基础:【注解】 提取注解信息,利用自定义注解编写测试类,注解绑定事件
提取注解信息 使用注解修饰了类.方法.成员变量等成员之后,这些注解不会自己生效,必须由开发者提供相应工具来提取并处理注解信息. Java使用java.lang.annotation.Annotat ...
- 如何在服务器上新建一个svn的目录工程文件
如何在服务器上新建一个svn的目录工程文件
- Linux(centos)使用nc命令发送测试数据
安装 yum -y install nmap-ncat 简单使用 nc -lk 7777 # 开启一个本地7777的TCP协议端口,由客户端主动发起连接,一旦连接必须由服务端发起关闭 nc -vw 2 ...
- JAVA中数组(Array)、字符串(String)、集合(List、Set)相互转换
1.数组转List String[] arr = new String[]{"A", "B", "C"}; List list = Arra ...
- java类型转换拓展
数据类型拓展 在Java中二进制用0b开头,八进制用0开头,十六进制用0x表示 整数拓展 int i=10; int i2=010;//八进制 int i3=0x10;//十六进制0x,0-9,A- ...
- Description has only two Sentences(hdu3307)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- Web前端面试题整合,持续更新【可以收藏】
饭后闲来无事,把这几年带学生用的一些面试题整合一下,供上!拿走,不客气!应付一般公司的二面基本上是够用了.祝你早日拿到心仪的offer. css相关 1. 万能居中 1.margin: 0 auto; ...
- Java——HashMap集合详解
第一章 HashMap集合简介 1.1 介绍 HashMap基于哈希表的Map接口实现,是以key-value存储形式存在,即主要用来存放键值对.HashMap 的实现不是同步的,这意味着它不是线程安 ...