阿里云动态域名解析
Posted mangues
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云动态域名解析相关的知识,希望对你有一定的参考价值。
1.如果不想用花生壳这类的工具,可以利用阿里云的动态域名解析api来动态解析域名。
2.原理就是定期检查ip是否改变,如果改变就利用api重新解析域名
一、pom.xml
<dependencies>
<!--retrofit-->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.retrofit/converter-gson -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.2.0</version>
</dependency>
<!--ok-http-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alidns</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
二、
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import http.RetroFitHelper;
import java.util.List;
public class start
private static IAcsClient client = null;
static
String regionId = "cn-hangzhou"; //必填固定值,必须为“cn-hanghou”
String accessKeyId = ""; // your accessKey
String accessKeySecret = "";// your accessSecret
//或者设置为空,然后下边有从阿里云获取的方法,只是不建议每次都去阿里云获取。
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
// 若报Can not find endpoint to access异常,请添加以下此行代码
// DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Alidns", "alidns.aliyuncs.com");
client = new DefaultAcsClient(profile);
public static void main(String[] args) throws Exception
String domain = "xxx.com"; //顶级域名
String rr = "yyy";//子域名
DescribeDomainRecordsResponse.Record record = GetRecordId(domain, rr);
String recordId = record.getRecordId();
String ip = record.getValue();
if (recordId==null|| recordId == "") return;
if (ip==null|| ip == "") return;
String curIp = RetroFitHelper.getCurIp(); //获取对外ip地址
if (!ip.equals(curIp))
UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
request.setRecordId(recordId);
request.setRR(record.getRR());
request.setType(record.getType());
request.setValue(curIp);
request.setTTL(record.getTTL());
request.setLine(record.getLine());
request.setPriority(10l);
UpdateDomainRecordResponse response = client.getAcsResponse(request);
System.out.println(response.getRecordId());
private static DescribeDomainRecordsResponse.Record GetRecordId(String domainString,String rr)
DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
request.setDomainName(domainString);
request.setRRKeyWord(rr);
DescribeDomainRecordsResponse response;
// describeRegionsRequest.setProtocol(ProtocolType.HTTPS); //指定访问协议
// describeRegionsRequest.setAcceptFormat(FormatType.JSON); //指定api返回格式
// describeRegionsRequest.setMethod(MethodType.POST); //指定请求方法
// describeRegionsRequest.setRegionId("cn-hangzhou");//指定要访问的Region,仅对当前请求生效,不改变client的默认设置。
try
response = client.getAcsResponse(request);
List<DescribeDomainRecordsResponse.Record> list = response.getDomainRecords();
for (DescribeDomainRecordsResponse.Record record : list)
if (domainString.equals(record.getDomainName()) && rr.equals(record.getRR()))
return record;
catch (Exception e)
e.printStackTrace();
return null;
以上是关于阿里云动态域名解析的主要内容,如果未能解决你的问题,请参考以下文章
Powershell 调用阿里云 云解析API 实现动态域名解析