文档

初始化接口

更新时间:

获取服务实例

获取HTTPDNS服务实例,不同的AccountID会返回不同的实例。

接口定义

HttpDnsService getService(Context applicationContext, String accountID);

参数说明

参数

类型

是否必填

说明

applicationContext

Context

您的Android App Context。

accountID

String

您的AccountID,在EMAS控制台首页获取。

代码示例

HttpDnsService httpdns = HttpDns.getService(getApplicationContext(), accountId);

获取服务实例(鉴权方式)

接口定义

HttpDnsService httpdns = HttpDns.getService(applicationContext, accountID, secretKey);

参数说明

参数

类型

是否必填

说明

applicationContext

Context

Android App的Context。

accountID

String

系统分配的Account ID,当您开通HTTPDNS后,您可在EMAS控制台的概览页面获取到您对应的Account ID信息。

您也可以在下载的配置文件中获取AccountID。

secretKey

String

鉴权对应的secretKey。

代码示例

HttpDnsService httpdns = HttpDns.getService(getApplicationContext(), accountId, secretKey);

设置预解析域名

将App使用到的域名预设进来,以便于HTTPDNS进行预解析。

接口定义

void setPreResolveHosts(ArrayList<String> hostList);
void setPreResolveHosts(ArrayList<String> hostList, RequestIpType requestIpType);

参数说明

参数

类型

是否必填

说明

hostList

ArrayList<String>

预解析域名列表。

requestIpType

RequestIpType

预解析的IP类型, 默认为IPv4地址。

代码示例

// httpdns为初始化获取的服务实例,默认预解析域名为ipv4地址
httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")));
// httpdns为初始化获取的服务实例,预解析域名为ipv4地址
httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.v4);
// httpdns为初始化获取的服务实例,预解析域名为ipv6地址
httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.v6);
// httpdns为初始化获取的服务实例,预解析域名对应ipv4 ipv6地址
httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.taobao.com", "www.aliyun.com")), RequestIpType.both);

初始化配置

在获取服务实例之前,集中配置服务的一些属性。初始化配置是可选的,完全可以不进行配置,在获取服务之后,分别设置。注意:初始化配置必须在获取服务实例之前,否则不会生效。

接口定义

public class InitConfig {
    public static class Builder {

        /**
         * 配置是否允许返回过期IP
         * @param enableExpiredIp
         * @return
         */
        public Builder setEnableExpiredIp(boolean enableExpiredIp)

        /**
         * 配置是否允许本地持久化缓存解析结果
         * @param enableCacheIp
         * @return
         */
        public Builder setEnableCacheIp(boolean enableCacheIp)

        /**
         * 配置请求服务超时时间
         * @param timeout
         * @return
         */
        public Builder setTimeout(int timeout)

        /**
         * 配置是否启用Https
         * @param enableHttps
         * @return
         */
        public Builder setEnableHttps(boolean enableHttps)

        /**
         * 配置ipv4探测域名
         * @param ipProbeItems
         * @return
         */
        public Builder setIPRankingList(List<IPRankingBean> ipRankingList)

        /**
         * 配置region节点
         * @param region
         * @return
         */
        public Builder setRegion(String region)
          
        /**
         * 配置自定义ttl的逻辑
         * @param cacheTtlChanger 修改ttl的接口
         */
        public Builder configCacheTtlChanger(CacheTtlChanger cacheTtlChanger)

        /**
         * 配置固定IP域名列表
         * @param hostListWithFixedIp 固定IP域名列表
         */
        public Builder configHostWithFixedIp(List<String> hostListWithFixedIp)

        /**
         * 生成配置,并记录配置所属account
         * @param accountId
         * @return
         */
        public InitConfig buildFor(String accountId)
    }
}


/**
 * 修改ttl时长的接口
 * 用于用户定制ttl,以控制缓存的时长
 */
public interface CacheTtlChanger {

    /**
     * 根据域名、ip类型和服务的ttl返回定制的ttl
     * @param host 域名
     * @param type ip类型
     * @param ttl 服务下发的ttl 单位秒
     * @return 定制的ttl 单位秒
     */
    int changeCacheTtl(String host, RequestIpType type, int ttl);
}

配置方法说明

方法

参数

是否必须

说明

setEnableExpiredIp

boolean

默认允许,同设置是否允许返回TTL过期域名的IP功能。

setEnableCacheIp

boolean

默认不启用,同是否允许启用持久化缓存功能。

setTimeout

int

默认 15000 毫秒,同设置网络请求超时时间功能

setEnableHttps

boolean

默认不启用https,同设置是否使用HTTPS协议解析域名功能。

setIPRankingList

List<IIPRankingBean>

默认不探测,同启用IP优选功能。

setRegion

String

configCacheTtlChanger

CacheTtlChanger

配置接口,用于域名缓存的自定义ttl时长

configHostWithFixedIp

List<String>

配置固定IP的域名列表,告诉SDK这些域名的解析结果是固定的,便于SDK进行相关的优化,降低解析频次

buildFor

String

创建配置,只有调用此方法配置才会生效。参数accountId是Account ID,在阿里云控制台获取。

代码示例

CacheTtlChanger cacheTtlChanger = new CacheTtlChanger() {
  @Override
  public int changeCacheTtl(String host, RequestIpType type, int ttl) {
    // 如果我们想让域名 www.aliyun.com 的域名解析结果缓存 1小时
    if ("www.aliyun.com".equals(host)) {
      return 60 * 60;
    }
    return ttl;
  }
};

ArrayList<String> hostWithFixedIp = new ArrayList<>();
// 我们假设,www.aliyun.com域名的解析结果固定
hostWithFixedIp.add("www.aliyun.com");

//IP探测,假设探测端口号是80
List<IPRankingBean> ipRankingBeanList = new ArrayList<>();
ipRankingBeanList.add(new IPRankingBean("www.xxx.com", 80));

// 初始化配置
new InitConfig.Builder()
  // 配置初始的region
  .setRegion(currentRegion)
  // 配置是否启用https,默认http
  .setEnableHttps(enableHttps)
  // 配置服务请求的超时时长,毫秒
  .setTimeout(10 * 1000)
  // 配置是否启用本地缓存,默认不启用
  .setEnableCacheIp(true)
  // 配置是否允许返回过期IP,默认允许
  .setEnableExpiredIp(true)
  // 配置ipv4探测域名
  .setIPRankingList(ipRankingBeanList)
  // 定制某些域名的缓存时长
  .configCacheTtlChanger(cacheTtlChanger)
  // 设置解析结果固定的域名列表
  .configHostWithFixedIp(hostWithFixedIp)
  // 针对哪一个account配置
  .buildFor(accountID);
  • 本页导读 (0)
文档反馈