重試機(jī)制
更新時(shí)間:
本節(jié)主要介紹V2.0 SDK 的重試機(jī)制。
V2.0 SDK 對(duì) OpenAPI 請(qǐng)求中遇到網(wǎng)絡(luò)問(wèn)題的情況會(huì)進(jìn)行重試,對(duì)于業(yè)務(wù)報(bào)錯(cuò)則不會(huì)重試。在請(qǐng)求中通過(guò)運(yùn)行時(shí)參數(shù)(RuntimeOption)配置重試,優(yōu)先級(jí)為 RuntimeOption > 默認(rèn);默認(rèn)不會(huì)進(jìn)行重試,如果打開(kāi)重試配置未配置重試次數(shù),則默認(rèn)最多重試3次:
import com.aliyun.ecs20140526.models.DescribeRegionsRequest;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
public class Sample {
public static void main(String[] args) throws Exception {
Config config = new com.aliyun.teaopenapi.models.Config();
// 從環(huán)境變量獲取RAM賬號(hào)的AccessKey ID
config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
// 從環(huán)境變量獲取RAM賬號(hào)的AccessKey ID
config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// 地域ID
config.setRegionId("<regionId>");
com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
// 運(yùn)行時(shí)參數(shù)重試設(shè)置,僅對(duì)使用了該運(yùn)行時(shí)參數(shù)實(shí)例的請(qǐng)求有效
RuntimeOptions runtimeOptions = new RuntimeOptions();
// 開(kāi)啟自動(dòng)重試機(jī)制
runtimeOptions.autoretry = true;
// 設(shè)置自動(dòng)重試次數(shù)
runtimeOptions.maxAttempts = 3;
DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions);
}
}
文檔內(nèi)容是否對(duì)您有幫助?