本文介绍如何使用 httpdns 服务访问 bos。
基于 bos 的使用情况来看,目前移动端上传的问题主要集中在如下几个方面:
(1)dns 解析失败,请求出错;
(2)域名劫持,数据在传输中有篡改,给用户带来诸如网络钓鱼、隐私窃取等网络访问风险;
(3)弱网下,速度慢,上传数据可能有超时。
若使用 httpdns 服务访问 bos,有以下优势:
(1)安全防劫持,可有效降低由域名劫持引起的成功率下降问题;
(2)精准调度可提供最优接入点,降低用户访问时延;
(3)域名解析结果变更时,httpdns服务没有传统dns服务多级缓存的影响,能够更快的令移动端获取新的解析结果,避免多层缓存的影响,可有效缩短域名切换的生效时间。
因此,如果您对 bos 的请求成功率、延迟、故障止损效速度较为敏感,您可以使用 httpdns 服务访问 bos。
使用 httpdns 服务访问 bos,您需要开通 httpdns 服务,并添加项目依赖,重载 okhttpdns,并调整 bosclient 配置。
注意事项:
- httpdns 虽然有一定免费额度(目前每自然月有 300 万次免费域名解析次数),超过初始额度后会产生一定的费用,通过 d 的参数配置,可以减少对httpdns server的访问,例如:缓存过期处理策略、使用https协议或http协议等。
- app刚启动时,默认httpdns降级标志为是。sdk、网络库自动使用dns解析的方式发起请求。这属于正常情况,不会影响后续使用httpdns服务来访问bos。
*.bcebos.com,或者业务使用的自定义域名或者 cdn 域名;我们提供了方便的远程依赖便于用户使用 httpdns。
java
repositories{
  jcenter()
  maven {
      url "https://raw.githubusercontent.com/bdhttpdns/bdhttpdnssdk/master"
  }
}java
dependencies{
  compile 'com.baidu:httpdns:1.3'
}设置刚刚开通 httpdns 服务的账号和密码,对常用域名进行预加载,避免初次请求时缓存不命中。同时,还可以设置多种处理策略,以更好地完成业务需要,参考移动域名解析 android_sdk 设置接口。
static class okhttpdns implements dns {
    private static okhttpdns instance = null;
    private bdhttpdns httpdns;
    
    private okhttpdns(context context) {
        this.httpdns = bdhttpdns.getservice(context);
        this.httpdns.setaccountid(account);
        this.httpdns.setsecret(secret);
        // 预加载域名
        arraylist preresolvehosts = new arraylist();
        preresolvehosts.add("bj.bcebos.com");
        this.httpdns.setpreresolvehosts(preresolvehosts);
    }
    
    public static okhttpdns getinstance(context context) {
        if(instance == null) {
            instance = new okhttpdns(context);
        }
        return instance;
    }
    
    @override
    public list lookup(final string hostname) throws unknownhostexception {
        // 通过同步解析接口获取ip
        final bdhttpdnsresult httpdnsresult = httpdns.syncresolve(hostname, false);
        log.v("dns", "httpdns resolve type: "   httpdnsresult.getresolvetype());
        // 优先使用ipv6结果,对于ipv6结果,需要在ip前后增加[]字符
        // 若不存在ipv6结果,则使用ipv4结果
        arraylist ipv6list = httpdnsresult.getipv6list();
        arraylist ipv4list = httpdnsresult.getipv4list();
        string ip = null;
        if (ipv6list != null && !ipv6list.isempty()) {
            ip = "["   ipv6list.get(0)   "]";
        } else if (ipv4list != null && !ipv4list.isempty()) {
            ip = ipv4list.get(0);
        } else {
            log.v("dns", "get empty iplist from httpdns, use origin url");
        }
        if(ip != null) {
            final string finalip = ip;
            //如果ip不为null,直接使用该ip进行网络请求
            list inetaddresses = arrays.aslist(inetaddress.getallbyname(ip));
            log.d("dns", "inetaddresses:"   inetaddresses);
            return inetaddresses;
        }
        //如果返回null,走系统dns服务解析域名
        return dns.system.lookup(hostname);
    }
}      使用 setdns。
java
bosclientconfiguration config = new bosclientconfiguration();
config.setcredentials(new defaultbcecredentials(accesskeyid, secretaccesskey));
config.setendpoint(bos_endpoint);
config.setprotocol(protocol.http);
config.setdns(okhttpdns.getinstance(getapplicationcontext()));
以上流程完成后,正常使用bos就可以体验httpdns域名解析服务了。