最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA實現(xiàn)對阿里云DNS的解析管理

 更新時間:2022年01月18日 08:28:23   作者:網(wǎng)無忌  
本文主要介紹了JAVA實現(xiàn)對阿里云DNS的解析管理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

1、阿里云DNS的SDK依賴

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>tea-openapi</artifactId>
    <version>0.0.19</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alidns20150109</artifactId>
    <version>2.0.1</version>
</dependency>

2、第一個方法:創(chuàng)建SDK客戶端實例

所有解析記錄的操作都要通過這個客戶端實例來進(jìn)行,所以要首先創(chuàng)建這個實例,需要阿里云的AccessKey(AppId和AppSecret)

/**
 * <p>
 * 創(chuàng)建客戶端實例
 * </p>
 * 
 * @return
 * @throws Exception
 */
private Client createClient() throws Exception{
    AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey參數(shù)
    if(api == null) throw new ErrException("未配置阿里云API參數(shù)!");
    Config config = new Config();
    config.accessKeyId = api.getAppId();
    config.accessKeySecret = api.getAppSecret();
    config.endpoint = "alidns.cn-beijing.aliyuncs.com";
    return new Client(config);
}

3、第二個方法:返回指定的記錄ID(RecordId)

在阿里云的SDK中,對解析記錄進(jìn)行修改和刪除時,都需要傳入 RecordId 這個參數(shù),所以提前寫一個獲取記錄ID的方法。

/**
 * <p>
 * 返回指定主機(jī)記錄的ID,不存在時返回null
 * </p>
 * 
 * @param DomainName
 * @param RR 記錄名稱
 * @return
 */
private String getRecId(Client client, String DomainName, String RR){
    String recId = null;
    try {
        DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
        request.setDomainName(DomainName);
        request.setRRKeyWord(RR);
        DescribeDomainRecordsResponse response = client.describeDomainRecords(request);
        if(response.getBody().getTotalCount() > 0){
            List<DescribeDomainRecordsResponseBodyDomainRecordsRecord> recs = response.getBody().getDomainRecords().getRecord();
            for(DescribeDomainRecordsResponseBodyDomainRecordsRecord rec: recs){
                if(rec.getRR().equalsIgnoreCase(RR)){
                    recId = rec.getRecordId();
                    break;
                }
            }
        }
    } catch (Exception e) {
    }
    return recId;
}

4、第三個方法:添加或修改指定的記錄

方便起見,這里我將添加和修改集成到了一個方法,相當(dāng)于SaveOrUpdate。

/**
 * <p>
 * 添加或修改解析記錄
 * </p>
 * 
 * @param DomainName 域名
 * @param RR 記錄名稱
 * @param Type 記錄類型(A、AAAA、MX、TXT、CNAME)
 * @param Value 記錄值
 */
public void update(String DomainName, String RR, String Type, String Value){
    try {
        if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
        if(EStr.isEmpty(RR)) throw new RuntimeException("主機(jī)記錄(RR)為空!");
        if(EStr.isEmpty(Type)) throw new RuntimeException("記錄類型(Type)為空!");
        if(EStr.isEmpty(Value)) throw new RuntimeException("記錄值(Value)為空!");
        Client client = createClient();
        String recId = getRecId(client, DomainName, RR);
        if(EStr.isNull(recId)){ //添加
            AddDomainRecordRequest request = new AddDomainRecordRequest();
            request.setDomainName(DomainName);
            request.setRR(RR);
            request.setType(Type);
            request.setValue(Value);
            AddDomainRecordResponse response = client.addDomainRecord(request);
            recId = response.getBody().getRecordId();
        }else{ //修改
            UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
            request.setRecordId(recId);
            request.setRR(RR);
            request.setType(Type);
            request.setValue(Value);
            UpdateDomainRecordResponse response = client.updateDomainRecord(request);
            recId = response.getBody().getRecordId();
        }
        renderJson(Result.success("recId", recId));
    } catch (Exception e) {
        renderJson(Result.fail(e.getMessage()));
    }
}

5、第四個方法:刪除指定的記錄

這個很簡單,根據(jù)查找到的RecordId直接刪除即可。

/**
 * <p>
 * 刪除記錄
 * </p>
 * 
 * @param DomainName
 * @param RR
 */
public void remove(String DomainName, String RR){
    try {
        if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
        if(EStr.isEmpty(RR)) throw new RuntimeException("主機(jī)記錄(RR)為空!");
        Client client = createClient();
        String recId = getRecId(client, DomainName, RR);
        if(EStr.isNull(recId)){
            renderJson(Result.success("recId", null));
        }else{
            DeleteDomainRecordRequest request = new DeleteDomainRecordRequest();
            request.setRecordId(recId);
            DeleteDomainRecordResponse response = client.deleteDomainRecord(request);
            renderJson(Result.success("recId", response.getBody().getRecordId()));
        }
    } catch (Exception e) {
        renderJson(Result.fail(e.getMessage()));
    }
}

6、完整代碼

package itez.alidns.controller;
import java.util.List;
import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.AddDomainRecordRequest;
import com.aliyun.alidns20150109.models.AddDomainRecordResponse;
import com.aliyun.alidns20150109.models.DeleteDomainRecordRequest;
import com.aliyun.alidns20150109.models.DeleteDomainRecordResponse;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;
import com.aliyun.teaopenapi.models.Config;
 
import itez.core.wrapper.controller.ControllerDefine;
import itez.core.wrapper.controller.EController;
import itez.kit.EStr;
import itez.kit.exception.ErrException;
import itez.kit.restful.Result;
import itez.plat.main.model.CompWx;
import itez.plat.main.service.CompWxService;
import itez.weixin.api.ApiConfigKit.ConfigType;
 
/**
 * <p>
 * 阿里云DNS解析
 * 示例:http://localhost/alidns/update?DomainName=domain.com&RR=test&Type=A&Value=8.8.8.8
 * </p>
 * 
 * <p>Copyright(C) 2017-2022 <a >上游科技</a></p>
 * 
 * @author        <a href="mailto:netwild@qq.com">Z.Mingyu</a>
 * @date        2022年1月12日 下午2:38:31
 */
@ControllerDefine(key = "/alidns", summary = "阿里云DNS解析", view = "/")
public class AliDnsController extends EController {
        
    /**
     * <p>
     * 添加或修改解析記錄
     * </p>
     * 
     * @param DomainName 域名
     * @param RR 記錄名稱
     * @param Type 記錄類型(A、AAAA、MX、TXT、CNAME)
     * @param Value 記錄值
     */
    public void update(String DomainName, String RR, String Type, String Value){
        try {
            if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
            if(EStr.isEmpty(RR)) throw new RuntimeException("主機(jī)記錄(RR)為空!");
            if(EStr.isEmpty(Type)) throw new RuntimeException("記錄類型(Type)為空!");
            if(EStr.isEmpty(Value)) throw new RuntimeException("記錄值(Value)為空!");
            Client client = createClient();
            String recId = getRecId(client, DomainName, RR);
            if(EStr.isNull(recId)){ //添加
                AddDomainRecordRequest request = new AddDomainRecordRequest();
                request.setDomainName(DomainName);
                request.setRR(RR);
                request.setType(Type);
                request.setValue(Value);
                AddDomainRecordResponse response = client.addDomainRecord(request);
                recId = response.getBody().getRecordId();
            }else{ //修改
                UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
                request.setRecordId(recId);
                request.setRR(RR);
                request.setType(Type);
                request.setValue(Value);
                UpdateDomainRecordResponse response = client.updateDomainRecord(request);
                recId = response.getBody().getRecordId();
            }
            renderJson(Result.success("recId", recId));
        } catch (Exception e) {
            renderJson(Result.fail(e.getMessage()));
        }
    }
    
    /**
     * <p>
     * 刪除記錄
     * </p>
     * 
     * @param DomainName
     * @param RR
     */
    public void remove(String DomainName, String RR){
        try {
            if(EStr.isEmpty(DomainName)) throw new RuntimeException("域名(DomainName)為空!");
            if(EStr.isEmpty(RR)) throw new RuntimeException("主機(jī)記錄(RR)為空!");
            Client client = createClient();
            String recId = getRecId(client, DomainName, RR);
            if(EStr.isNull(recId)){
                renderJson(Result.success("recId", null));
            }else{
                DeleteDomainRecordRequest request = new DeleteDomainRecordRequest();
                request.setRecordId(recId);
                DeleteDomainRecordResponse response = client.deleteDomainRecord(request);
                renderJson(Result.success("recId", response.getBody().getRecordId()));
            }
        } catch (Exception e) {
            renderJson(Result.fail(e.getMessage()));
        }
    }
 
    /**
     * <p>
     * 返回指定主機(jī)記錄的ID,不存在時返回null
     * </p>
     * 
     * @param DomainName
     * @param RR 記錄名稱
     * @return
     */
    private String getRecId(Client client, String DomainName, String RR){
        String recId = null;
        try {
            DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
            request.setDomainName(DomainName);
            request.setRRKeyWord(RR);
            DescribeDomainRecordsResponse response = client.describeDomainRecords(request);
            if(response.getBody().getTotalCount() > 0){
                List<DescribeDomainRecordsResponseBodyDomainRecordsRecord> recs = response.getBody().getDomainRecords().getRecord();
                for(DescribeDomainRecordsResponseBodyDomainRecordsRecord rec: recs){
                    if(rec.getRR().equalsIgnoreCase(RR)){
                        recId = rec.getRecordId();
                        break;
                    }
                }
            }
        } catch (Exception e) {
        }
        return recId;
    }
    
    /**
     * <p>
     * 創(chuàng)建客戶端實例
     * </p>
     * 
     * @return
     * @throws Exception
     */
    private Client createClient() throws Exception{
        AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey參數(shù)
        if(api == null) throw new ErrException("未配置阿里云API參數(shù)!");
        Config config = new Config();
        config.accessKeyId = api.getAppId();
        config.accessKeySecret = api.getAppSecret();
        config.endpoint = "alidns.cn-beijing.aliyuncs.com";
        return new Client(config);
    }  
}

到此這篇關(guān)于JAVA實現(xiàn)對阿里云DNS的解析管理的文章就介紹到這了,更多相關(guān)JAVA 阿里云DNS 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot實現(xiàn)對文件進(jìn)行壓縮下載功能

    Spring Boot實現(xiàn)對文件進(jìn)行壓縮下載功能

    在Web應(yīng)用中,文件下載功能是一個常見的需求,特別是當(dāng)你需要提供用戶下載各種類型的文件時,本文將演示如何使用Spring Boot框架來實現(xiàn)一個簡單而強(qiáng)大的文件下載功能,需要的朋友跟隨小編一起學(xué)習(xí)吧
    2023-09-09
  • 解釋:int型默認(rèn)值為0的問題

    解釋:int型默認(rèn)值為0的問題

    這篇文章主要介紹了解釋:int型默認(rèn)值為0的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringMvc之HandlerMapping詳解

    SpringMvc之HandlerMapping詳解

    這篇文章主要介紹了SpringMvc之HandlerMapping詳解,Handler可以理解為具體干活的,也就是我們的業(yè)務(wù)處理邏輯,Handler最終是要通過url 來訪問到,這樣url 與Handler之間就有一個映射關(guān)系了,需要的朋友可以參考下
    2023-08-08
  • Java中static作用詳解

    Java中static作用詳解

    這篇文章主要介紹了Java中static作用,static表示“全局”或者“靜態(tài)”的意思,用來修飾成員變量和成員方法,也可以形成靜態(tài)static代碼塊,需要的朋友可以參考下
    2015-09-09
  • 史上最通俗理解的Java死鎖代碼演示

    史上最通俗理解的Java死鎖代碼演示

    這篇文章主要給大家介紹了關(guān)于Java死鎖代碼演示的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java實現(xiàn)銀行家算法(Swing界面)

    java實現(xiàn)銀行家算法(Swing界面)

    這篇文章主要為大家詳細(xì)介紹了銀行家算法的java代碼實現(xiàn),Swing寫的界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 完美解決idea沒有tomcat server選項的問題

    完美解決idea沒有tomcat server選項的問題

    這篇文章主要介紹了完美解決idea沒有tomcat server選項的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java設(shè)計模式中的原型模式講解

    Java設(shè)計模式中的原型模式講解

    原型模式是用于創(chuàng)建重復(fù)的對象,同時又能保證性能。這種類型的設(shè)計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,今天通過本文給大家介紹下Java?原型設(shè)計模式,感興趣的朋友一起看看吧
    2023-04-04
  • springboot基于Mybatis mysql實現(xiàn)讀寫分離

    springboot基于Mybatis mysql實現(xiàn)讀寫分離

    這篇文章主要介紹了springboot基于Mybatis mysql實現(xiàn)讀寫分離,需要的朋友可以參考下
    2019-06-06
  • Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    Java?Spring?boot?配置JDK和MAVEN開發(fā)環(huán)境的過程

    本文詳細(xì)介紹了如何配置JDK和Maven環(huán)境,包括JDK的安裝與環(huán)境變量設(shè)置,Maven的下載、配置環(huán)境變量和設(shè)置阿里云倉庫,最后簡述了在IntelliJ?IDEA中配置JDK和Maven的步驟,本教程適合Java開發(fā)新手進(jìn)行開發(fā)環(huán)境的搭建,確保順利進(jìn)行Java項目的開發(fā)
    2024-11-11

最新評論

普格县| 泗水县| 新郑市| 宁远县| 日喀则市| 玉山县| 南丹县| 梧州市| 图木舒克市| 乌什县| 肇源县| 天镇县| 承德市| 体育| 顺平县| 迭部县| 广元市| 丹江口市| 河西区| 白朗县| 南阳市| 抚顺市| 策勒县| 乐昌市| 建瓯市| 离岛区| 宁陵县| 邯郸市| 彭山县| 高安市| 太原市| 揭东县| 乌拉特中旗| 长子县| 炉霍县| 额尔古纳市| 龙陵县| 松原市| 贵阳市| 滦南县| 萍乡市|