JAVA實(shí)現(xiàn)對阿里云DNS的解析管理
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、第一個(gè)方法:創(chuàng)建SDK客戶端實(shí)例
所有解析記錄的操作都要通過這個(gè)客戶端實(shí)例來進(jìn)行,所以要首先創(chuàng)建這個(gè)實(shí)例,需要阿里云的AccessKey(AppId和AppSecret)
/** * <p> * 創(chuàng)建客戶端實(shí)例 * </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、第二個(gè)方法:返回指定的記錄ID(RecordId)
在阿里云的SDK中,對解析記錄進(jìn)行修改和刪除時(shí),都需要傳入 RecordId 這個(gè)參數(shù),所以提前寫一個(gè)獲取記錄ID的方法。
/** * <p> * 返回指定主機(jī)記錄的ID,不存在時(shí)返回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、第三個(gè)方法:添加或修改指定的記錄
方便起見,這里我將添加和修改集成到了一個(gè)方法,相當(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、第四個(gè)方法:刪除指定的記錄
這個(gè)很簡單,根據(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,不存在時(shí)返回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)建客戶端實(shí)例 * </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實(shí)現(xiàn)對阿里云DNS的解析管理的文章就介紹到這了,更多相關(guān)JAVA 阿里云DNS 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中treemap和treeset實(shí)現(xiàn)紅黑樹
這篇文章主要為大家詳細(xì)介紹了java中treemap和treeset實(shí)現(xiàn)紅黑樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能
這篇文章介紹了Java使用easyExcel實(shí)現(xiàn)導(dǎo)入功能的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式
這篇文章主要介紹了ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java實(shí)現(xiàn)Html轉(zhuǎn)Pdf的方法
這篇文章主要介紹了Java實(shí)現(xiàn)Html轉(zhuǎn)Pdf的方法,實(shí)例分析了java基于ITextRenderer類操作頁面及系統(tǒng)自帶字體生成pdf文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Spring使用@Autowired為抽象父類注入依賴代碼實(shí)例
這篇文章主要介紹了Spring使用@Autowired為抽象父類注入依賴代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印
java開發(fā)中經(jīng)常遇到對圖片的處理,JDK中也提供了對應(yīng)的工具類,不過處理起來很麻煩,Thumbnailator是一個(gè)優(yōu)秀的圖片處理的開源Java類庫,處理效果遠(yuǎn)比Java API的好,這篇文章主要介紹了利用Thumbnailator如何輕松的實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印,需要的朋友可以參考下2017-01-01Java阻塞延遲隊(duì)列DelayQueue原理及使用詳解
這篇文章主要介紹了Java阻塞延遲隊(duì)列DelayQueue原理及使用詳解,阻塞隊(duì)列是一個(gè)支持兩個(gè)附加操作的隊(duì)列,這兩個(gè)附加的操作是:在隊(duì)列為空時(shí),從隊(duì)列中獲取元素的消費(fèi)者線程會一直等待直到隊(duì)列變?yōu)榉强?需要的朋友可以參考下2023-12-12