1.前期准备
1.登入阿里云控制台创建acesskeys。得到acesskey id.acesskey secret.以及获取的域名domainname:xxx.com
2.申请一个阿里云域名
3.sdk和所需jar包:aliyun-java-sdk-alidns-2.0.6.jar.aliyun-java-sdk-core-4.1.1.jar.gson-2.8.5.
环境:eclipse+maven
2.介绍
1.获取服务器ip.
2.阿里云为我们提供了云解析的api可以拿来用,获取到详细的域名解析记录,进而更改自己的域名解析值。
3.更新域名解析记录。
3.文件结构
4.代码实现
aliyun.java
public class Aliyun implements Serializable {
/** 序列号 */
private static final long serialVersionUID = 1L;
/** IPV4地址,当前电脑在公网的ip地址 */
private String ipV4;
/** 主机记录,如果要解析@.exmaple.com,主机记录要填写"@”,而不是空 */
private String rr;
/** 解析记录类型,参见解析记录类型格式 */
private String type;
/** 解析记录的ID,此参数在添加解析时会返回,在获取域名解析列表时会返回 */
private String recordId;
/** 生效时间,默认为600秒(10分钟),参见TTL定义说明。购买VIP可以升级为1秒生效。没必要1秒 */
DemolistDomains.java
package com.tony.ddns;
import java.util.List;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse.Domain;
import com.tony.ddns.pojo.Aliyun;
import com.tony.ddns.util.AliDdnsUtils;
/**
* 阿里sdk
*
* @author tony
*
*/
public class DemoListDomains {
/**
* 设置参数
*
* @param request
*/
public void setParam(DescribeDomainsRequest request, Aliyun yun) {
// 设置参数
request.putQueryParameter("RecordId", yun.getRecordId());
request.putQueryParameter("RR", yun.getRr());
request.putQueryParameter("Type", yun.getType());
request.putQueryParameter("Value", yun.getIpV4());
request.putQueryParameter("TTL", yun.getTTL());
}
/**
* 解析DNS信息
*/
public void analysisDns(Aliyun yun) {
String actionName = "UpdateDomainRecord";
DescribeDomainsRequest request = AliDdnsUtils.getRequest(actionName);
DescribeDomainsResponse response;
setParam(request, yun);
try {
response = AliDdnsUtils.getClient().getAcsResponse(request);
List<Domain> list = response.getDomains();
for (Domain domain : list) {
System.out.println(domain.getDomainName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
updatedomainrecord.java
package com.tony.ddns;
import java.util.List;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse.Record;
import com.tony.ddns.pojo.Aliyun;
import com.tony.ddns.util.AliDdnsUtils;
import com.tony.ddns.util.LocalPublicIpv4;
import com.tony.ddns.util.PropertiesUtil;
/**
* 调用阿里api,更新DNS域名解析
*
* @author tony
*
*/
public class UpdateDomainRecord {
/**
* 设置域名参数
*
* @param request
*/
public void setParam(DescribeDomainRecordsRequest request) {
String domainName = PropertiesUtil.getProperty("DomainName");
request.putQueryParameter("DomainName", domainName);
}
/**
* 解析DNS信息
*/
public void analysisDns() {
// 获取公网ip
LocalPublicIpv4 ip = new LocalPublicIpv4();
String ipV4 = ip.publicip();
// 获取解析的数据
String actionName = "DescribeDomainRecords";
DescribeDomainRecordsResponse response;
// 获取request
DescribeDomainRecordsRequest request = AliDdnsUtils.getRequestQuery(actionName);
// 设置request参数
setParam(request);
try {
response = AliDdnsUtils.getClient().getAcsResponse(request);
// 声明解析对象
DemoListDomains demo = new DemoListDomains();
// 获取阿里云的数据
List<Record> list = response.getDomainRecords();
if (list == null || list.isEmpty()) {
return;
}
// 更新ip
Record record = list.get(0);
Aliyun yun = new Aliyun();
// 进行判定记录是否需要更新
if (record.getValue().equals(ipV4)) {
// 不需要更新,继续下次循环
System.out.println("当前域名解析地址为:" + ipV4 + "不需要更新!");
} else {
System.out.println("更新域名:" + record.getDomainName());
// 进行替换关键数据
yun.setIpV4(ipV4);
yun.setRecordId(record.getRecordId());
yun.setRr(record.getRR());
yun.setTTL(record.getTTL());
yun.setType(record.getType());
System.out.println("域名更换ip开始");
demo.analysisDns(yun);
System.out.println("域名更换ip结束");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("域名更换异常");
}
}
}
apprun.java(主函数入口)
package com.tony.ddns;
/**
* 运行时入口
*
* @author tony
*
*/
public class AppRun {
public static void main(String[] args) {
System.out.println("开始ddns检查");
UpdateDomainRecord record = new UpdateDomainRecord();
record.analysisDns();
System.out.println("ddns运行结束");
}
}
ddns.properties(配置文件)
AccessKeyID=input your acesskeyid
AccessKeySecret=input your acesskeysecret
DomainName=input your domainname(xxx.com)
5.总结
通过阿里云SDK可以将域名实时解析并更新至阿里云。获取ip的api我使用的是2019.ip138.com这个api。源代码