OpenFeign中的HttpClient替换(替换成带有连接池的)

OpenFeign中的HttpClient替换

OpenFeign中的HttpClient如果不做任何特殊的配置,默认是采用JDK自带的HttpURLConnection发送HTTP请求,由于JDK的HttpURLConnection没有连接池,性能和效率上是比较低的。

例如当我们遇到连接超时的错误时,也能看到是HttpURLConnection抛出的异常~这也能进一步证明默认采用的是HttpURLConnection

在这里插入图片描述

另外官方也建议我们使用Apache HttpClient 5如下图
在这里插入图片描述
因此实际开发中,切换成Apache HttpClient 5是很有必要的。

引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
	<groupId>org.apache.httpcomponents.client5</groupId>
	<artifactId>httpclient5</artifactId>
	<version>5.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-hc5 -->
<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-hc5</artifactId>
	<!--如果SpringBoot2.x版本的这里版本要换成11.x的-->
	<version>13.3</version>
</dependency>

开启配置说明

feign:
  httpclient:
    hc5:
      enabled: true # 这是2.x的写法
  client:
    config:
      default:
        connectTimeout: 2000
        readTimeout: 2000
# --------------- 以下是SpringBoot 3.0 写法 ---------------
spring:
  application:
    name: dist-service
  cloud:
    openfeign:
      httpclient:
        hc5:
          enabled: true

再来看超时的报错,已经全部是HttpClient 5

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lambda.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值