You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Azure NetworkManager客户端的超时、重试次数及重试延迟

配置Azure NetworkManager的连接超时、重试次数及重试间隔

你的Azure NetworkManager连接代码中,默认重试次数为3次且间隔较长,自定义的RetryStrategy未生效,核心原因是没有将重试策略正确注入到Azure SDK的请求处理管线(HttpPipeline)中。同时,连接超时时间需要通过自定义HttpClient来配置,以下是完整的解决方案:

1. 配置自定义HttpClient(设置超时参数)

通过HttpClient.create()构建自定义客户端,可分别设置连接超时(建立TCP连接的时间限制)和响应超时(等待服务器返回数据的时间限制):

// 配置超时:连接超时10秒,响应超时30秒,可根据业务需求调整
HttpClient httpClient = HttpClient.create()
        .connectionTimeout(Duration.ofSeconds(10))
        .responseTimeout(Duration.ofSeconds(30));

2. 实现自定义重试策略

自定义RetryStrategy控制重试次数和间隔,再封装为RetryPolicy:

// 自定义重试规则:最多重试1次,每次固定间隔2秒
RetryStrategy customRetryStrategy = new RetryStrategy() {
    @Override
    public int getMaxRetries() {
        return 1;
    }

    @Override
    public Duration calculateRetryDelay(int retryAttempts) {
        // 可根据重试次数实现递增间隔,例如:Duration.ofSeconds(2 * retryAttempts)
        return Duration.ofSeconds(2);
    }

    // 可选:自定义重试触发条件,仅对特定错误重试
    @Override
    public boolean shouldRetry(HttpResponse response, Throwable error) {
        // 示例:仅对5xx服务器错误或网络异常重试
        if (error != null) {
            return true;
        }
        return response.statusCode() >= 500 && response.statusCode() < 600;
    }
};

RetryPolicy customRetryPolicy = new RetryPolicy(customRetryStrategy);

3. 构建包含重试策略的HttpPipeline

将自定义HttpClient和重试策略整合到HttpPipeline中,这是让重试策略生效的关键步骤:

HttpPipeline pipeline = new HttpPipelineBuilder()
        .httpClient(httpClient)
        .policies(customRetryPolicy) // 将重试策略添加到管线策略链
        .build();

4. 将配置应用到NetworkManager

通过withHttpPipeline()方法将自定义管线注入到NetworkManager配置中,替换默认管线:

private void connect() {
    // 1. 配置带超时的HttpClient
    HttpClient httpClient = HttpClient.create()
            .connectionTimeout(Duration.ofSeconds(10))
            .responseTimeout(Duration.ofSeconds(30));

    // 2. 构建TokenCredential
    TokenCredential tokenCredential = new ClientSecretCredentialBuilder()
            .clientSecret(azureCredentials.getDecryptedDeviceLoginPassword())
            .clientId(azureCredentials.getDeviceLoginUsername())
            .tenantId(tenantId)
            .httpClient(httpClient)
            .build();

    // 3. 自定义重试策略
    RetryStrategy customRetryStrategy = new RetryStrategy() {
        @Override
        public int getMaxRetries() {
            return 1;
        }

        @Override
        public Duration calculateRetryDelay(int retryAttempts) {
            return Duration.ofSeconds(2);
        }

        @Override
        public boolean shouldRetry(HttpResponse response, Throwable error) {
            if (error != null) {
                return true;
            }
            return response.statusCode() >= 500 && response.statusCode() < 600;
        }
    };
    RetryPolicy customRetryPolicy = new RetryPolicy(customRetryStrategy);

    // 4. 构建带重试策略的HttpPipeline
    HttpPipeline pipeline = new HttpPipelineBuilder()
            .httpClient(httpClient)
            .policies(customRetryPolicy)
            .build();

    // 5. 初始化NetworkManager并应用配置
    AzureProfile azureProfile = new AzureProfile(...);
    NetworkManager networkManager = NetworkManager.configure()
            .withLogLevel(HttpLogDetailLevel.BODY)
            .withHttpPipeline(pipeline) // 注入自定义管线
            .authenticate(tokenCredential, azureProfile);

    // 执行请求
    networkManager.resourceManager().providers().getByName("Microsoft.Compute");
}

关键注意事项

  • 重试策略必须通过HttpPipeline注入,直接创建RetryPolicy不会自动生效,因为Azure SDK的请求处理完全依赖管线中的策略链。
  • 若需要保留SDK默认的其他核心策略(如日志、认证),可在构建HttpPipeline时添加默认策略,例如:.policies(customRetryPolicy, new UserAgentPolicy(), new BearerTokenAuthenticationPolicy(tokenCredential))。

内容的提问来源于stack exchange,提问作者Lior Elayev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 07:27:31