阿里云域名动态解析

server/2024/10/21 10:01:22/

前景说明:

你有一个阿里云的域名,想让它解析到你家用宽带动态ip上。

解决思路:

1、定时查看宽带的ip;

2、发现变化时,通过阿里云提供的sdk修改域名解析。

一、阿里云控制台创建AccessKey

官方文档:创建阿里云AccessKey_访问控制(RAM)-阿里云帮助中心 (aliyun.com)

二、阿里云 云解析

官方文档:云解析_SDK中心-阿里云OpenAPI开发者门户 (aliyun.com)

官方接口:UpdateDomainRecord_云解析_API调试-阿里云OpenAPI开发者门户 (aliyun.com)

DescribeDomainRecordInfo_云解析_API调试-阿里云OpenAPI开发者门户

三、上代码

1、pom.xml引包
   <dependencies><dependency><groupId>com.aliyun</groupId><artifactId>alidns20150109</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>credentials-java</artifactId><version>0.3.0</version></dependency></dependencies>
2、获取外网ip的方法
package com.chhuang;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Pattern;public class ExternalIPUtil {/*** IP 地址校验的正则表达式*/private static final Pattern IPV4_PATTERN = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");/*** 获取 IP 地址的服务列表*/private static final String[] IPV4_SERVICES = {"http://checkip.amazonaws.com/","https://ipv4.icanhazip.com/","http://bot.whatismyipaddress.com/"// and so on ...};//    线程池的 ExecutorService.invokeAny(callables) 方法用于并发执行多个线程,并拿到最快的执行成功的线程的返回值,
//    只要有一个执行成功,其他失败的任务都会忽略。但是如果全部失败,
//    例如本机根本就没有连外网,那么就会抛出 ExecutionException 异常。public static String get() throws ExecutionException, InterruptedException {List<Callable<String>> callables = new ArrayList<>();for (String ipService : IPV4_SERVICES) {callables.add(() -> get(ipService));}ExecutorService executorService = Executors.newCachedThreadPool();try {// 返回第一个成功获取的 IPreturn executorService.invokeAny(callables);} finally {executorService.shutdown();}}private static String get(String url) throws IOException {try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {String ip = in.readLine();if (IPV4_PATTERN.matcher(ip).matches()) {return ip;} else {throw new IOException("invalid IPv4 address: " + ip);}}}
}
3、使用云解析
package com.chhuang;import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.DescribeDomainRecordInfoRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordInfoResponse;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;import java.util.Random;/*** Hello world!**/
public class App {private static final String APP_ALIBABA_ID = "你自己的ALIBABA_CLOUD_ACCESS_KEY_ID";private static final String SECURITY_ALIBABA_KEY = "你自己的ALIBABA_CLOUD_ACCESS_KEY_SECRET";/*** 使用AK&SK初始化账号Client* @return Client* @throws Exception*/public static Client createClient() throws Exception {// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。Config config = new Config().setAccessKeyId(APP_ALIBABA_ID).setAccessKeySecret(SECURITY_ALIBABA_KEY);// Endpoint 请参考 https://api.aliyun.com/product/Alidnsconfig.endpoint = "alidns.cn-hangzhou.aliyuncs.com";return new Client(config);}public static void main( String[] args ) throws Exception {while (true) {Random random = new Random();Thread.sleep((random.nextInt(600) + 600) * 1000);// 获取外网ipString ip = ExternalIPUtil.get();System.out.println("ip=" + ip);// 更新域名解析updateDomain(ip);}}private static void updateDomain(String ip) throws Exception {Client client = App.createClient();// 获取解析记录的详细信息DescribeDomainRecordInfoRequest describeDomainRecordInfoRequest = new DescribeDomainRecordInfoRequest().setRecordId("你自己域名解析记录的recordId");RuntimeOptions runtime = new RuntimeOptions();try {// 复制代码运行请自行打印 API 的返回值DescribeDomainRecordInfoResponse response = client.describeDomainRecordInfoWithOptions(describeDomainRecordInfoRequest, runtime);String value = response.getBody().getValue();// 判断 ip 是否需要修改if (!ip.equals(value)) {UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest().setRecordId("你自己域名解析记录的recordId") // 通过阿里云后台,DescribeDomainRecords 获取解析记录.setRR("www").setType("A").setValue(ip).setTTL(600L);// 复制代码运行请自行打印 API 的返回值client.updateDomainRecordWithOptions(updateDomainRecordRequest, runtime);}} catch (TeaException error) {// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。// 错误 messageerror.printStackTrace();// 诊断地址System.out.println(error.getData().get("Recommend"));Common.assertAsString(error.message);} catch (Exception _error) {_error.printStackTrace();TeaException error = new TeaException(_error.getMessage(), _error);// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。// 诊断地址System.out.println(error.getData().get("Recommend"));Common.assertAsString(error.message);}}
}


http://www.ppmy.cn/server/12312.html

相关文章

P1039 [NOIP2003 提高组] 侦探推理

注意换行符&#xff01;&#xff01;&#xff01; 如果你使用getchar()系列函数读入&#xff0c;并且用换行符判定是否结束&#xff0c;则换行符会导致你WA掉&#xff01; linux下换行符为’\n’&#xff0c;windows下换行符为’\r\n’&#xff0c;如果数据是windows下造的&a…

SCSS全局配置 vue项目(二)

目录 1、先要查看node版本 2、安装对应的node-sass、sass-loader版本 2.1根据项目使用的node版本安装对应的node-sass版本 2.2根据node-sass版本选择兼容的sass-loader版本&#xff0c;不然项目无法正常运行 3、在 vue.config.js 中配置&#xff1a; 4、在组件中…

Spring源码中的抽象工厂模式

Spring 框架中广泛运用了抽象工厂模式来实现其核心组件的创建与管理。以下是源码分析&#xff1a; 源码分析&#xff1a; 1. BeanFactory 与其实现 org.springframework.beans.factory.BeanFactory 是 Spring 中最基础的工厂接口&#xff0c;它代表了抽象工厂模式中的“抽象…

Java深度优先搜索与广度优先搜索知识点(含面试大厂题和源码)

深度优先搜索&#xff08;Depth-First Search&#xff0c;DFS&#xff09;和广度优先搜索&#xff08;Breadth-First Search&#xff0c;BFS&#xff09;是两种常用的图遍历算法&#xff0c;它们在解决图和树结构的问题中非常有用&#xff0c;如路径搜索、最短路径、网络爬虫、…

每天学习一个Linux命令之scp

每天学习一个Linux命令之scp 在Linux系统中&#xff0c;scp&#xff08;secure copy&#xff09;命令用于在本地和远程服务器之间安全地传输文件和目录。它是基于SSH协议的&#xff0c;通过加密和认证保证传输的安全性。本文将详细介绍scp命令及其所有可用选项的用法。 命令格…

STM32 学习13 低功耗模式与唤醒

STM32 学习13 低功耗模式与唤醒 一、介绍1. STM32低功耗模式功能介绍2. 常见的低功耗模式&#xff08;1&#xff09;**睡眠模式 (Sleep Mode)**:&#xff08;2&#xff09;**停止模式 (Stop Mode)**:&#xff08;3&#xff09;**待机模式 (Standby Mode)**: 二、睡眠模式1. 进入…

基于模糊控制的纯跟踪横向控制在倒车中的应用及实现

文章目录 1. 引言2. Pure Pursuit在倒车场景的推导3. 模糊控制器的设计3.1 基础知识3.2 预瞄距离系数k的模糊控制器设计 4. 算法和仿真实现 1. 引言 Pure Pursuit是一种几何跟踪控制算法&#xff0c;也被称为纯跟踪控制算法。他的思想就是基于当前车辆的后轮中心的位置&#x…

SpringBoot + kotlin 协程小记

前言&#xff1a; Kotlin 协程是基于 Coroutine 实现的&#xff0c;其设计目的是简化异步编程。协程提供了一种方式&#xff0c;可以在一个线程上写起来像是在多个线程中执行。 协程的基本概念&#xff1a; 协程是轻量级的&#xff0c;不会创建新的线程。 协程会挂起当前的协…