当前位置: 首页 > news >正文

OkHttp 简单配置

OkHttpClient 的简单配置,包含重试,线程池
@Configuration
public class OkHttpConfig {@Bean("deSourceOkHttp")public OkHttpClient okHttpClient() {return new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).addInterceptor(new SmartRetryInterceptor(3,1000,true)).connectionPool(new ConnectionPool(50, 1, TimeUnit.MINUTES)).addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("Accept", "application/json").addHeader("Content-Type", "application/json").build())).build();}
}

重试

public class SmartRetryInterceptor implements Interceptor {private final int maxRetries;private final long baseDelayMs;private final boolean enableJitter;public SmartRetryInterceptor(int maxRetries, long baseDelayMs, boolean enableJitter) {this.maxRetries = maxRetries;this.baseDelayMs = baseDelayMs;this.enableJitter = enableJitter;}@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();Response response = null;IOException exception = null;for (int i = 0; i <= maxRetries; i++) {try {response = chain.proceed(request);if (isSuccessfulOrShouldNotRetry(response)) {return response;}} catch (IOException e) {exception = e;}// 如果是幂等请求或满足特定条件,才重试if (isIdempotent(request) && i < maxRetries) {long delay = baseDelayMs * (1 << i); // 指数退避if (enableJitter) {delay += new Random().nextInt((int) (baseDelayMs * 0.5));}try {Thread.sleep(delay);} catch (InterruptedException e) {Thread.currentThread().interrupt();throw new IOException("重试中断", e);}} else {break;}}if (response != null) {return response;}throw exception;}private boolean isSuccessfulOrShouldNotRetry(Response response) {return response.isSuccessful() || !shouldRetry(response.code());}private boolean shouldRetry(int code) {return retryhttpCpde.containsKey(code);}private boolean isIdempotent(Request request) {String method = request.method();return "GET".equals(method) || "POST".equals(method) ;}}

http://www.lryc.cn/news/576097.html

相关文章:

  • pandas---使用教程
  • 解构SAP RISE与Cloud ERP授权新政:从许可模式到迁移策略的深度指南
  • (一)miniconda安装配置
  • Dubbo服务调用超时问题解决方案
  • Hyperledger Fabric 入门笔记(二十)Fabric V2.5 测试网络进阶之Tape性能测试
  • Linux tcp_info:监控TCP连接的秘密武器
  • 【RAG面试题】如何获取准确的语义表示
  • MCP-安全(代码实例)
  • ubuntu安装达梦数据库
  • Java8方法引用:简洁高效的编程利器
  • algorithm ——————》双指针(移动0 复写0 快乐数 装水问题 以及数组中找几个数和为指定的元组)
  • TCP四层模型:网络协议核心解密
  • WPF 3D 开发全攻略:实现3D模型创建、旋转、平移、缩放
  • HTTP协议中Connection: Keep-Alive和Keep-Alive: timeout=60, max=100的作用
  • Linux入门攻坚——49、高可用HA之corosync/pacemaker(2)
  • Linux命令行操作基础
  • 关于css的height:100%
  • JAVA-泛型通配符的上界和下界
  • UUDS—常见NRC及其含义
  • 中国双非高校经费TOP榜数据分析
  • ROS:录制相机、IMU、GNSS等设备数据
  • gRPC技术解析与python示例
  • 楼宇自控系统以智能化管控,全方位满足建筑节约、安全与可靠运行需求
  • 像素之外的智慧:Adobe AI在动态影像与云端协作中的进阶应用
  • 如何设置 Java 的环境变量
  • 23种设计模式——单例模式的暗黑面
  • LLaMA-Factory 对 omnisql 进行 ppo dpo grpo nl2sql任务 实现难度 时间 全面对比
  • 【.net core】【sqlsugar】在where条件查询时使用原生SQL
  • spring-ai 1.0.0 学习(十八)——MCP Server
  • 修复opensuse 风滚草rabbitmq的Error: :plugins_dir_does_not_exist问题