日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

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、第一個方法:創建SDK客戶端實例

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

/**
 * <p>
 * 創建客戶端實例
 * </p>
 * 
 * @return
 * @throws Exception
 */
private Client createClient() throws Exception{
	AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey參數
	if(api == null) throw new ErrException("未配置阿里云API參數!");
	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中,對解析記錄進行修改和刪除時,都需要傳入 RecordId 這個參數,所以提前寫一個獲取記錄ID的方法。

/**
 * <p>
 * 返回指定主機記錄的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、第三個方法:添加或修改指定的記錄

方便起見,這里我將添加和修改集成到了一個方法,相當于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("主機記錄(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、第四個方法:刪除指定的記錄

這個很簡單,根據查找到的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("主機記錄(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 href="http://www.itez.com.cn">上游科技</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("主機記錄(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("主機記錄(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>
	 * 返回指定主機記錄的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>
	 * 創建客戶端實例
	 * </p>
	 * 
	 * @return
	 * @throws Exception
	 */
	private Client createClient() throws Exception{
		AliConfig api = APIKit.getAliConfig(); //返回阿里云的AccessKey參數
		if(api == null) throw new ErrException("未配置阿里云API參數!");
		Config config = new Config();
		config.accessKeyId = api.getAppId();
		config.accessKeySecret = api.getAppSecret();
		config.endpoint = "alidns.cn-beijing.aliyuncs.com";
		return new Client(config);
	}
	
}

分享到:
標簽:阿里 DNS
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定