前期准备
- 一个动态公网IPv4/v6地址(v4需要到宽带所属营业厅自行付费申请,v6则保证公网IP即可)
- 域名(腾讯云)
- 一个Springboot项目
Maven依赖引入
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<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获取
笔者电脑网卡比较多,一个个测试比较麻烦且可能出现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实体类与静态配置文件
TencentCloud.java
@Data
@Component
@ConfigurationProperties(prefix = "tencentcloud")
public class TencentCloud {
private String secretId;
private String secretKey;
}
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;
}
application.properties(请自行修改值)
tencentcloud.secret-id=腾讯云secret-id
tencentcloud.secret-key=腾讯云secret-key
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接口以及实现类
DnsService.java 接口类
public interface DnsService {
public String dnsAnalysis() throws TencentCloudSDKException;
}
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);
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());
ModifyDynamicDNSResponse resp = client.ModifyDynamicDNS(req);
return "修改解析记录值成功!";
}
}
controller控制器
DnsController.java
@RestController
@RequestMapping("dnscontroller")
public class DnsController {
@Autowired
private DnsService dnsService;
@GetMapping("/dns")
public String dns() throws TencentCloudSDKException {
return dnsService.dnsAnalysis();
}
}
定时器
笔者后续会放入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());
}
}