This topic describes the advanced configurations for the HarmonyOS SDK.
IP ranking configuration
If a domain name has multiple IP addresses, you can configure IP ranking. This allows the SDK to sort the IP addresses by connection speed so that your application uses the fastest IP node.
The following code is an example:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { httpdns } from '@aliyun/httpdns';
const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
httpdns.configService(ACCOUNT_ID, {
context: this.context,
// ************* IP ranking configuration begin *************
hostConfigsForIpRanking: [{
host: 'www.aliyun.com',
port: 80
}]
// ************* IP ranking configuration end *************
})
}
// Other code omitted.
}The host field of hostConfigsForIpRanking specifies the domain name whose IP addresses will be ranked.
The port field of hostConfigsForIpRanking specifies the port used for connection speed tests.
Fixed IP configuration
When a network transition occurs, the SDK clears its cache and then decides whether to trigger automatic resolution based on the Enable automatic resolution on network change setting.
If a domain name is configured with a static IP, its resolution results are treated as fixed. Therefore, during a network transition, the SDK does not clear the cache or trigger automatic resolution for this domain name.
The following code is an example:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { httpdns } from '@aliyun/httpdns';
const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
httpdns.configService(ACCOUNT_ID, {
context: this.context,
// ************* Fixed IP configuration begin *************
hostsWithFixedIp: ['www.aliyun.com']
// ************* Fixed IP configuration end *************
})
}
// Other code omitted.
}Custom TTL configuration
By default, the TTL for the SDK cache is based on the TTL value from the DNS server.
In some scenarios, you can use the custom TTL configuration to modify the TTL for the SDK cache.
The following code is an example:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { httpdns, ICacheTtlCustomizer, IpType } from '@aliyun/httpdns';
const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console'
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
httpdns.configService(ACCOUNT_ID, {
context: this.context,
// ************* Custom TTL configuration begin *************
ttlCustomizer: new CustomTtl()
// ************* Custom TTL configuration end *************
})
}
// Other code omitted.
}
// ************* Custom TTL implementation begin *************
class CustomTtl implements ICacheTtlCustomizer {
getCacheTtlInSec(host: string, type: IpType, serverTtlInSec: number): number {
if (host === 'www.aliyun.com') {
return 10 * 60;
}
return serverTtlInSec;
}
}
// ************* Custom TTL implementation end *************The ttlCustomizer field in the initialization configuration specifies the custom TTL implementation.
The custom TTL implementation requires only one method, getCacheTtlInSec. The SDK uses this method to retrieve the custom TTL when it caches resolution results.
In this method, you can return a custom TTL for specific domain names, as shown in the example code.