好物优选点击查看详情 京东购买

暂无内容

Java利用阿里云API实现动态域名解析DDNS

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

前提条件

  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。

  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)。

    <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-alidns</artifactId> <version>2.0.10</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.3.2</version> </dependency>

代码示例

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

import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsRequest; import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse; import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest; import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class DDNS { /** * 获取主域名的所有解析记录列表 */ private DescribeSubDomainRecordsResponse describeSubDomainRecords(DescribeSubDomainRecordsRequest request, IAcsClient client) { try{ // 调用SDK发送请求 return client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); // 发生调用错误,抛出运行时异常 throw new RuntimeException(); } } /** * 获取当前主机公网IP */ private String getCurrenHostIP() { // 这里使用jsonip.com第三方接口获取本地IP String jsonip = "https://jsonip.com"; // 接口返回结果 String result = ""; BufferedReader in = null; try { // 使用HttpURLConnection网络请求第三方接口 URL url = new URL(jsonip); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line; while((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } // 正则表达式,提取xxx.xxx.xxx.xxx,将IP地址从接口返回结果中提取出来 String rexp = "(\\d{1,3}\\.){3}\\d{1,3}"; Pattern pat = Pattern.compile(rexp); Matcher mat = pat.matcher(result); String res = ""; while (mat.find()) { res = mat.group(); break; } return res; } /** * 修改解析记录 */ private UpdateDomainRecordResponse updateDomainRecord(UpdateDomainRecordRequest request,IAcsClient client) { try { // 调用SDK发送请求 return client.getAcsResponse(request); } catch (ClientException e) { e.printStackTrace(); // 发生调用错误,抛出运行时异常 throw new RuntimeException(); } } private static void log_print(String functionName, Object result) { Gson gson = new Gson(); System.out.println("-------------------------------" + functionName + "-------------------------------"); System.out.println(gson.toJson(result)); } public static void main(String[] args) { // 设置鉴权参数,初始化客户端 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",// 地域ID "your-access-key-id",// 您的AccessKey ID "your-access-key-secret");// 您的AccessKey Secret IAcsClient client = new DefaultAcsClient(profile); DDNS ddns = new DDNS(); //查询指定二级域名的最新解析记录 DescribeSubDomainRecordsRequest describeSubDomainRecordsRequest = new DescribeSubDomainRecordsRequest(); describeSubDomainRecordsRequest.setSubDomain("example.com"); DescribeSubDomainRecordsResponse describeSubDomainRecordsResponse = ddns.describeSubDomainRecords(describeSubDomainRecordsRequest, client); log_print("describeSubDomainRecords",describeSubDomainRecordsResponse); List<DescribeSubDomainRecordsResponse.Record> domainRecords = describeSubDomainRecordsResponse.getDomainRecords(); //最新的一条解析记录 if (domainRecords.size() != 0) { DescribeSubDomainRecordsResponse.Record record = domainRecords.get(0); // 记录ID String recordId = record.getRecordId(); // 记录值 String recordsValue = record.getValue(); // 当前主机公网IP String currentHostIP = ddns.getCurrenHostIP(); System.out.println("-------------------------------当前主机公网IP为:"+currentHostIP+"-------------------------------"); if (!currentHostIP.equals(recordsValue)) { // 修改解析记录 UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest(); // 主机记录 updateDomainRecordRequest.setRR("@"); // 记录ID updateDomainRecordRequest.setRecordId(recordId); // 将主机记录值改为当前主机IP updateDomainRecordRequest.setValue(currentHostIP); // 解析记录类型 updateDomainRecordRequest.setType("A"); UpdateDomainRecordResponse updateDomainRecordResponse = ddns.updateDomainRecord(updateDomainRecordRequest, client); log_print("updateDomainRecord",updateDomainRecordResponse); } } } }

运行结果

正确运行结果类似如下:

-------------------------------describeSubDomainRecords-------------------------------{ "requestId":"7C283E2C-72DF-4A45-AA13-A4D4FA65025F", "totalCount":1, "pageNumber":1, "pageSize":20, "domainRecords":[ { "domainName":"example.com", "recordId":"200061448941xxxxx", "rR":"@", "type":"A", "value":"1.1.1.1", "tTL":600, "line":"default", "status":"ENABLE", "locked":false, "weight":1 } ] } -------------------------------当前主机公网IP为:123.123.123.123--------------------------------------------------------------updateDomainRecord------------------------------- { "requestId":"D7DE138C-CC80-485C-AE08-0A47BBEC8060", "recordId":"200061448941xxxxx" }

API文档 – 云解析 DNS – 阿里云https://help.aliyun.com/document_detail/29738.html

原文链接:https://blog.csdn.net/ztx114/article/details/121257127?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168476299816800182144302%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168476299816800182144302&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-30-121257127-null-null.blog_rank_default&utm_term=NAS%E3%80%81%E7%BE%A4%E6%99%96%E3%80%81%E9%98%BF%E9%87%8C%E4%BA%91%E3%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E3%80%81%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E3%80%81ipv6%E3%80%81ddns%E3%80%81%E8%BD%BB%E9%87%8F%E7%BA%A7%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E9%93%81%E5%A8%81%E9%A9%AC%E3%80%81%E5%A8%81%E8%81%94%E9%80%9A%E3%80%81DSM%E3%80%81DSM6.0%E3%80%81%E7%BE%A4%E6%99%96nas%E3%80%81%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E8%9C%97%E7%89%9B%E6%98%9F%E9%99%85%E3%80%81%E9%BB%91%E7%BE%A4%E6%99%96%E3%80%81docker%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81%E5%9F%9F%E5%90%8D%E6%B3%A8%E5%86%8C%E3%80%81%E5%AE%9D%E5%A1%94%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E3%80%81nginx%E3%80%81frp%E3%80%81%E5%8A%A8%E6%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享