全部产品
云市场
云游戏

实现动态域名解析DDNS

更新时间:2020-07-14 14:32:04

本教程详细介绍如何使用Alibaba Cloud SDK for Java实现动态域名解析(DDNS)。

前提条件

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)。
    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-alidns</artifactId>
    4. <version>2.0.10</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>aliyun-java-sdk-core</artifactId>
    9. <version>4.3.2</version>
    10. </dependency>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:

  1. import com.aliyuncs.DefaultAcsClient;
  2. import com.aliyuncs.IAcsClient;
  3. import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsRequest;
  4. import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse;
  5. import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
  6. import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
  7. import com.aliyuncs.exceptions.ClientException;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. import com.google.gson.Gson;
  10. import java.io.BufferedReader;
  11. import java.io.InputStreamReader;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.util.List;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. public class DDNS {
  18. /**
  19. * 获取主域名的所有解析记录列表
  20. */
  21. private DescribeSubDomainRecordsResponse describeSubDomainRecords(DescribeSubDomainRecordsRequest request, IAcsClient client) {
  22. try{
  23. // 调用SDK发送请求
  24. return client.getAcsResponse(request);
  25. } catch (ClientException e) {
  26. e.printStackTrace();
  27. // 发生调用错误,抛出运行时异常
  28. throw new RuntimeException();
  29. }
  30. }
  31. /**
  32. * 获取当前主机公网IP
  33. */
  34. private String getCurrenHostIP() {
  35. // 这里使用jsonip.com第三方接口获取本地IP
  36. String jsonip = "https://jsonip.com";
  37. // 接口返回结果
  38. String result = "";
  39. BufferedReader in = null;
  40. try {
  41. // 使用HttpURLConnection网络请求第三方接口
  42. URL url = new URL(jsonip);
  43. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  44. urlConnection.setRequestMethod("GET");
  45. urlConnection.connect();
  46. in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  47. String line;
  48. while((line = in.readLine()) != null) {
  49. result += line;
  50. }
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. // 使用finally块来关闭输入流
  55. finally {
  56. try {
  57. if (in != null) {
  58. in.close();
  59. }
  60. } catch (Exception e2) {
  61. e2.printStackTrace();
  62. }
  63. }
  64. // 正则表达式,提取xxx.xxx.xxx.xxx,将IP地址从接口返回结果中提取出来
  65. String rexp = "(\\d{1,3}\\.){3}\\d{1,3}";
  66. Pattern pat = Pattern.compile(rexp);
  67. Matcher mat = pat.matcher(result);
  68. String res = "";
  69. while (mat.find()) {
  70. res = mat.group();
  71. break;
  72. }
  73. return res;
  74. }
  75. /**
  76. * 修改解析记录
  77. */
  78. private UpdateDomainRecordResponse updateDomainRecord(UpdateDomainRecordRequest request,IAcsClient client) {
  79. try {
  80. // 调用SDK发送请求
  81. return client.getAcsResponse(request);
  82. } catch (ClientException e) {
  83. e.printStackTrace();
  84. // 发生调用错误,抛出运行时异常
  85. throw new RuntimeException();
  86. }
  87. }
  88. private static void log_print(String functionName, Object result) {
  89. Gson gson = new Gson();
  90. System.out.println("-------------------------------" + functionName + "-------------------------------");
  91. System.out.println(gson.toJson(result));
  92. }
  93. public static void main(String[] args) {
  94. // 设置鉴权参数,初始化客户端
  95. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",// 地域ID
  96. "your-access-key-id",// 您的AccessKey ID
  97. "your-access-key-secret");// 您的AccessKey Secret
  98. IAcsClient client = new DefaultAcsClient(profile);
  99. DDNS ddns = new DDNS();
  100. //查询指定二级域名的最新解析记录
  101. DescribeSubDomainRecordsRequest describeSubDomainRecordsRequest = new DescribeSubDomainRecordsRequest();
  102. describeSubDomainRecordsRequest.setSubDomain("example.com");
  103. DescribeSubDomainRecordsResponse describeSubDomainRecordsResponse = ddns.describeSubDomainRecords(describeSubDomainRecordsRequest, client);
  104. log_print("describeSubDomainRecords",describeSubDomainRecordsResponse);
  105. List<DescribeSubDomainRecordsResponse.Record> domainRecords = describeSubDomainRecordsResponse.getDomainRecords();
  106. //最新的一条解析记录
  107. if (domainRecords.size() != 0) {
  108. DescribeSubDomainRecordsResponse.Record record = domainRecords.get(0);
  109. // 记录ID
  110. String recordId = record.getRecordId();
  111. // 记录值
  112. String recordsValue = record.getValue();
  113. // 当前主机公网IP
  114. String currentHostIP = ddns.getCurrenHostIP();
  115. System.out.println("-------------------------------当前主机公网IP为:"+currentHostIP+"-------------------------------");
  116. if (!currentHostIP.equals(recordsValue)) {
  117. // 修改解析记录
  118. UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest();
  119. // 主机记录
  120. updateDomainRecordRequest.setRR("@");
  121. // 记录ID
  122. updateDomainRecordRequest.setRecordId(recordId);
  123. // 将主机记录值改为当前主机IP
  124. updateDomainRecordRequest.setValue(currentHostIP);
  125. // 解析记录类型
  126. updateDomainRecordRequest.setType("A");
  127. UpdateDomainRecordResponse updateDomainRecordResponse = ddns.updateDomainRecord(updateDomainRecordRequest, client);
  128. log_print("updateDomainRecord",updateDomainRecordResponse);
  129. }
  130. }
  131. }
  132. }

运行结果

正确运行结果类似如下:

  1. -------------------------------describeSubDomainRecords-------------------------------{
  2. "requestId":"7C283E2C-72DF-4A45-AA13-A4D4FA65025F",
  3. "totalCount":1,
  4. "pageNumber":1,
  5. "pageSize":20,
  6. "domainRecords":[
  7. {
  8. "domainName":"example.com",
  9. "recordId":"200061448941xxxxx",
  10. "rR":"@",
  11. "type":"A",
  12. "value":"1.1.1.1",
  13. "tTL":600,
  14. "line":"default",
  15. "status":"ENABLE",
  16. "locked":false,
  17. "weight":1
  18. }
  19. ]
  20. }
  21. -------------------------------当前主机公网IP为:123.123.123.123--------------------------------------------------------------updateDomainRecord-------------------------------
  22. {
  23. "requestId":"D7DE138C-CC80-485C-AE08-0A47BBEC8060",
  24. "recordId":"200061448941xxxxx"
  25. }

相关文档