前期准备
- 一个动态公网IPv4/v6地址(v4需要到宽带所属营业厅自行付费申请,v6则保证公网IP即可)
- 域名(腾讯云)
- 一个Springboot项目
Maven依赖引入
代码语言:javascript复制 <!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- hutool工具库 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0</version>
</dependency>
<!-- 腾讯云 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-dnspod</artifactId>
<version>3.1.322</version>
</dependency>
本地公网IPv4获取
代码语言:javascript复制笔者电脑网卡比较多,一个个测试比较麻烦且可能出现BUG,所以偷了个懒直接调用别人写好的接口点击查询本地IPv4地址
静态工具类 IPUtils.java
public class IPUtils {
public static String ip(){
HttpResponse httpres = HttpUtil.createGet("https://api.vvhan.com/api/getIpInfo").execute();
HashMap hashmap = JSONUtil.toBean(httpres.body().toString(), HashMap.class);
return hashmap.get("ip").toString();
}
}
pojo实体类与静态配置文件
代码语言:javascript复制TencentCloud.java
@Data
@Component
@ConfigurationProperties(prefix = "tencentcloud")
public class TencentCloud {
private String secretId;
private String secretKey;
}
代码语言:javascript复制AnalysisInfo.java
@Data
@Component
@ConfigurationProperties(prefix = "analysisinfo")
public class AnalysisInfo {
private String domain;
private String subDomain;
private Long recordId;
private String recordLine;
private String recordLineId;
private String url;
}
代码语言:javascript复制application.properties(请自行修改值)
#腾讯云api密钥信息(打开链接获取:https://console.cloud.tencent.com/cam/capi)
tencentcloud.secret-id=腾讯云secret-id
tencentcloud.secret-key=腾讯云secret-key
#DNS解析默认配置信息
analysisinfo.domain=“你的域名”
analysisinfo.sub-domain=“主机记录值”
analysisinfo.record-id="打开链接查询https://cloud.tencent.com/document/api/1427/56166"
analysisinfo.record-line=默认
analysisinfo.record-line-id=0
analysisinfo.url=dnspod.tencentcloudapi.com
service接口以及实现类
代码语言:javascript复制DnsService.java 接口类
public interface DnsService {
public String dnsAnalysis() throws TencentCloudSDKException;
}
代码语言:javascript复制DnsServiceImpl.java 接口实现类
@Service("dnsService")
public class DnsServiceImpl implements DnsService {
@Autowired
private TencentCloud tencentCloud;
@Autowired
private AnalysisInfo analysisInfo;
public String dnsAnalysis() throws TencentCloudSDKException {
Credential cred = new Credential(tencentCloud.getSecretId(), tencentCloud.getSecretKey());
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(analysisInfo.getUrl());
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
DnspodClient client = new DnspodClient(cred, "", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
ModifyDynamicDNSRequest req = new ModifyDynamicDNSRequest();
req.setDomain(analysisInfo.getDomain());
req.setSubDomain(analysisInfo.getSubDomain());
req.setRecordId(analysisInfo.getRecordId());
req.setRecordLine(analysisInfo.getRecordLine());
req.setRecordLineId(analysisInfo.getRecordLineId());
req.setValue(IPUtils.ip());
// req.setTtl(600L);
// 返回的resp是一个ModifyDynamicDNSResponse的实例,与请求对象对应
ModifyDynamicDNSResponse resp = client.ModifyDynamicDNS(req);
// return ModifyDynamicDNSResponse.toJsonString(resp);
return "修改解析记录值成功!";
}
}
controller控制器
代码语言:javascript复制DnsController.java
@RestController
@RequestMapping("dnscontroller")
public class DnsController {
@Autowired
private DnsService dnsService;
@GetMapping("/dns")
public String dns() throws TencentCloudSDKException {
return dnsService.dnsAnalysis();
}
}
定时器
代码语言:javascript复制笔者后续会放入cloud服务中写自己的Demo,所以采用api定时调用的形式。
DNSTimerConfig.java
@Configuration
@EnableScheduling
public class DNSTimerConfig {
@Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
public static void go() {
System.out.println("开始");
HttpResponse httpres = HttpUtil.createGet("http://127.0.0.1:8080/dnscontroller/dns").execute();
System.out.println(httpres.body().toString());
}
}