如何知道解析得到的IP列表里哪个IP连通性最好?

问题描述

一个域名可能对应多个IP,HTTPDNS解析会返回所有可用的IP,但是没有排序。

解决方案

可以使用IP优选接口来对结果IP进行排序,以保证得到最优连通性的IP,接口示例代码如下:

Android
ArrayList<IPRankingBean> list = new ArrayList<IPRankingBean>();
list.add(new IPRankingBean("www.aliyun.com", 8080));
new InitConfig.Builder().setIPRankingList(list);
说明

2.3.2版本新增的方法。

iOS:
 HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:XXXX];
 NSDictionary *IPRankingDatasource = @{
                                 @"www.abc.com" : @80,
                                 @"www.cba.com" : @443,
                                 @"www.bac.com" : @443
                                 };
 [httpdns setIPRankingDatasource:IPRankingDatasource];
Harmony:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { httpdns } from '@aliyun/httpdns';

const ACCOUNT_ID = '这里需要替换为阿里云HTTPDNS控制台的Account ID'
        
export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    httpdns.configService(ACCOUNT_ID, {
      context: this.context,
      // ************* IP优选配置 begin *************
      hostConfigsForIpRanking: [{
        host: 'www.aliyun.com',
        port: 80
      }]
      // ************* IP优选配置 end *************
    })
  }
  // 省略其它代码
}

更多信息

域名探测是socket连接。会逐个连接传入的IP列表,对IP列表进行动态排序,以保证第一个IP是可用性较好的IP。

适用于

  • HTTPDNS