SpringBoot动态域名解析的实现

2023-01-03 08:22:10 浏览数 (1)

前期准备

  1. 一个动态公网IPv4/v6地址(v4需要到宽带所属营业厅自行付费申请,v6则保证公网IP即可)
  2. 域名(腾讯云)
  3. 一个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获取

笔者电脑网卡比较多,一个个测试比较麻烦且可能出现BUG,所以偷了个懒直接调用别人写好的接口点击查询本地IPv4地址

静态工具类 IPUtils.java

代码语言:javascript复制
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

代码语言:javascript复制
@Data
@Component
@ConfigurationProperties(prefix = "tencentcloud")
public class TencentCloud {
    private String secretId;
    private String secretKey;
}

AnalysisInfo.java

代码语言:javascript复制
@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(请自行修改值)

代码语言:javascript复制
#腾讯云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接口以及实现类

DnsService.java 接口类

代码语言:javascript复制
public interface DnsService {
    public String dnsAnalysis() throws TencentCloudSDKException;
}

DnsServiceImpl.java 接口实现类

代码语言:javascript复制
@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控制器

DnsController.java

代码语言:javascript复制
@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

代码语言:javascript复制
@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());
    }

}

0 人点赞