🎨 #3824 【基础架构】升级到 Apache HttpClient 5.x 作为默认 HTTP 客户端

This commit is contained in:
Copilot
2026-01-06 11:20:58 +08:00
committed by GitHub
parent 987001214d
commit e46da01cc8
20 changed files with 313 additions and 24 deletions

View File

@@ -114,6 +114,52 @@
- **微信开放平台**`weixin-java-open`)主要用于第三方平台,代公众号或小程序进行开发和管理
---------------------------------
### HTTP 客户端支持
本项目同时支持多种 HTTP 客户端实现,默认推荐使用 **Apache HttpClient 5.x**(最新稳定版本)。
#### 支持的 HTTP 客户端类型
| HTTP 客户端 | 说明 | 配置值 | 推荐程度 |
|------------|------|--------|---------|
| Apache HttpClient 5.x | Apache HttpComponents Client 5.x最新版本 | `HttpComponents` | ⭐⭐⭐⭐⭐ 推荐 |
| Apache HttpClient 4.x | Apache HttpClient 4.x向后兼容 | `HttpClient` | ⭐⭐⭐⭐ 兼容 |
| OkHttp | Square OkHttp 客户端 | `OkHttp` | ⭐⭐⭐ 可选 |
| Jodd-http | Jodd 轻量级 HTTP 客户端 | `JoddHttp` | ⭐⭐ 可选 |
#### 配置方式
**Spring Boot 配置示例:**
```properties
# 使用 HttpClient 5.x推荐MP/CP/Channel/QiDian 模块默认)
wx.mp.config-storage.http-client-type=HttpComponents
# 使用 HttpClient 4.x兼容模式MiniApp 模块默认)
wx.mp.config-storage.http-client-type=HttpClient
# 使用 OkHttp
wx.mp.config-storage.http-client-type=OkHttp
# 使用 Jodd-http
wx.mp.config-storage.http-client-type=JoddHttp
```
**注意**:如果使用 Multi-Starter`wx-java-mp-multi-spring-boot-starter`),枚举值需使用大写下划线格式:
```properties
# Multi-Starter 配置格式
wx.mp.config-storage.http-client-type=HTTP_COMPONENTS # 注意使用大写下划线
```
**注意事项:**
1. **MiniApp 模块**已提供 `HttpComponents`HttpClient 5.x类型的配置选项但当前默认仍为 HttpClient 4.x如需启用 HttpClient 5.x请确保所使用的集成模块`wx-java-miniapp-spring-boot-starter``wx-java-miniapp-solon-plugin`)版本已支持该选项
2. **MP、Channel、QiDian 模块**已完整支持 HttpClient 5.x默认推荐使用
3. **CP 模块**的支持情况取决于具体使用的 Starter 版本,请参考对应模块文档
4. 如需使用 OkHttp 或 Jodd-http需在项目中添加对应的依赖scope为provided
5. HttpClient 4.x 和 HttpClient 5.x 可以共存,按需配置即可
---------------------------------
### 版本说明

View File

@@ -0,0 +1,203 @@
# HttpClient 升级指南
## 概述
从 WxJava 4.7.x 版本开始,项目开始支持并推荐使用 **Apache HttpClient 5.x**HttpComponents Client 5同时保持对 HttpClient 4.x 的向后兼容。
## 为什么升级?
1. **Apache HttpClient 5.x 是最新稳定版本**:提供更好的性能和更多的功能
2. **HttpClient 4.x 已经进入维护模式**:不再积极开发新功能
3. **更好的安全性**HttpClient 5.x 包含最新的安全更新和改进
4. **向前兼容**:为未来的开发做好准备
## 支持的 HTTP 客户端
| HTTP 客户端 | 版本 | 配置值 | 状态 | 说明 |
|------------|------|--------|------|------|
| Apache HttpClient 5.x | 5.5 | `HttpComponents` | ⭐ 推荐 | 最新稳定版本 |
| Apache HttpClient 4.x | 4.5.13 | `HttpClient` | ✅ 支持 | 向后兼容 |
| OkHttp | 4.12.0 | `OkHttp` | ✅ 支持 | 需自行添加依赖 |
| Jodd-http | 6.3.0 | `JoddHttp` | ✅ 支持 | 需自行添加依赖 |
## 模块支持情况
| 模块 | HttpClient 5.x 支持 | 默认客户端 |
|------|-------------------|-----------|
| weixin-java-mp公众号 | ✅ 是 | HttpComponents (5.x) |
| weixin-java-cp企业微信 | ⚠️ 视集成方式而定 | 参考对应 starter 配置 |
| weixin-java-channel视频号 | ✅ 是 | HttpComponents (5.x) |
| weixin-java-qidian企点 | ✅ 是 | HttpComponents (5.x) |
| weixin-java-miniapp小程序 | ✅ 是 | HttpClient (4.x) |
| weixin-java-pay支付 | ✅ 是 | HttpComponents (5.x) |
| weixin-java-open开放平台 | ✅ 是 | HttpComponents (5.x) |
**注意**
- **weixin-java-miniapp 模块**已在核心 SDK 中提供 HttpClient 5.x`HttpComponents`)支持,但默认仍使用 HttpClient 4.x`HttpClient`)。如需启用 HttpClient 5.x可通过配置 `http-client-type=HttpComponents` 显式指定。
- **weixin-java-cp 模块**的支持情况取决于具体使用的 Starter 版本,请参考对应模块文档。
## 对现有项目的影响
### 对新项目
- **无需任何修改**,直接使用最新版本即可
- 支持 HttpClient 5.x 的模块会自动使用 HttpComponents (5.x)
### 对现有项目
- **向后兼容**:不需要修改任何代码
- HttpClient 4.x 依然可用,项目同时包含两个版本的依赖
- 如果希望继续使用 HttpClient 4.x只需在配置中显式指定
## 迁移步骤
### 1. 更新 WxJava 版本
`pom.xml` 中更新版本:
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>最新版本</version> <!-- 请参考 Maven Central 或项目 README 获取最新版本号 -->
</dependency>
```
### 2. 检查配置(可选)
#### Spring Boot 项目
`application.properties``application.yml` 中:
```properties
# 使用 HttpClient 5.x推荐无需配置已经是默认值
wx.mp.config-storage.http-client-type=HttpComponents
# 或者继续使用 HttpClient 4.x
wx.mp.config-storage.http-client-type=HttpClient
```
#### 纯 Java 项目
```java
// 使用 HttpClient 5.x推荐
WxMpService wxMpService = new WxMpServiceHttpComponentsImpl();
// 或者继续使用 HttpClient 4.x
WxMpService wxMpService = new WxMpServiceHttpClientImpl();
```
### 3. 测试应用
升级后,建议进行全面测试以确保一切正常工作。
## 常见问题
### Q: 升级后会不会破坏现有代码?
A: 不会。项目保持完全向后兼容HttpClient 4.x 的所有实现都保持不变。
### Q: 我需要修改代码吗?
A: 大多数情况下不需要。如果希望继续使用 HttpClient 4.x只需在配置中指定 `http-client-type=HttpClient` 即可。
### Q: MiniApp 模块支持 HttpClient 5.x 吗?
A: 支持。MiniApp 模块在核心 SDK 中已经提供了基于 HttpClient 5.x`HttpComponents`)的支持,但默认仍会使用 HttpClient 4.x`HttpClient`)以保持向后兼容。如果你使用的是框架集成(例如 Spring Boot Starter 或 Solon Plugin可以通过显式配置 `http-client-type=HttpComponents` 来启用 HttpClient 5.x。
### Q: 我可以在同一个项目中同时使用两个版本吗?
A: 可以。不同的模块可以配置使用不同的 HTTP 客户端。例如MP 模块使用 HttpClient 5.xMiniApp 模块默认使用 HttpClient 4.x但也可以按需配置为 HttpClient 5.x。
### Q: 如何排除不需要的依赖?
A: 如果只想使用一个版本,可以在 `pom.xml` 中排除另一个:
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>最新版本</version>
<exclusions>
<!-- 排除 HttpClient 4.x -->
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</exclusion>
</exclusions>
</dependency>
```
## 配置参考
### Spring Boot 完整配置示例
```properties
# 公众号配置
wx.mp.app-id=your_app_id
wx.mp.secret=your_secret
wx.mp.token=your_token
wx.mp.aes-key=your_aes_key
# HTTP 客户端配置
wx.mp.config-storage.http-client-type=HttpComponents # HttpComponents, HttpClient, OkHttp, JoddHttp
# HTTP 代理配置(可选)
wx.mp.config-storage.http-proxy-host=proxy.example.com
wx.mp.config-storage.http-proxy-port=8080
wx.mp.config-storage.http-proxy-username=proxy_user
wx.mp.config-storage.http-proxy-password=proxy_pass
# 超时配置(可选)
wx.mp.config-storage.connection-timeout=5000
wx.mp.config-storage.so-timeout=5000
wx.mp.config-storage.connection-request-timeout=5000
```
## 技术细节
### HttpClient 4.x 与 5.x 的主要区别
1. **包名变更**
- HttpClient 4.x: `org.apache.http.*`
- HttpClient 5.x: `org.apache.hc.client5.*`, `org.apache.hc.core5.*`
2. **API 改进**
- HttpClient 5.x 提供更现代的 API 设计
- 更好的异步支持
- 改进的连接池管理
3. **性能优化**
- HttpClient 5.x 包含多项性能优化
- 更好的资源管理
### 项目中的实现
WxJava 项目通过策略模式支持多种 HTTP 客户端:
```
weixin-java-common/
├── util/http/
│ ├── apache/ # HttpClient 4.x 实现
│ ├── hc/ # HttpClient 5.x (HttpComponents) 实现
│ ├── okhttp/ # OkHttp 实现
│ └── jodd/ # Jodd-http 实现
```
每个模块都有对应的 Service 实现类:
- `*ServiceHttpClientImpl` - 使用 HttpClient 4.x
- `*ServiceHttpComponentsImpl` - 使用 HttpClient 5.x
- `*ServiceOkHttpImpl` - 使用 OkHttp
- `*ServiceJoddHttpImpl` - 使用 Jodd-http
## 反馈与支持
如果在升级过程中遇到问题,请:
1. 查看 [项目 Wiki](https://github.com/binarywang/WxJava/wiki)
2. 在 [GitHub Issues](https://github.com/binarywang/WxJava/issues) 中搜索或提交问题
3. 加入技术交流群(见 README.md
## 总结
-**推荐使用 HttpClient 5.x**:性能更好,功能更强
-**向后兼容**:可以继续使用 HttpClient 4.x
-**灵活配置**:支持多种 HTTP 客户端,按需选择
-**平滑迁移**:无需修改代码,仅需配置即可

View File

@@ -136,6 +136,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<httpclient.version>4.5.13</httpclient.version>
<httpclient5.version>5.5</httpclient5.version>
<jetty.version>9.4.57.v20241219</jetty.version> <!-- 这个不能用10以上的版本不支持jdk8-->
</properties>
<dependencyManagement>
@@ -157,13 +158,14 @@
<version>4.12.0</version>
<scope>provided</scope>
</dependency>
<!-- HttpClient 5.x - 默认依赖(推荐使用) -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.5</version>
<scope>provided</scope>
<version>${httpclient5.version}</version>
</dependency>
<!-- HttpClient 4.x - 默认依赖(为了保持向后兼容) -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>

View File

@@ -7,7 +7,11 @@ package com.binarywang.solon.wxjava.channel.enums;
*/
public enum HttpClientType {
/**
* HttpClient
* HttpClient.
*/
HttpClient
HttpClient,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -73,7 +73,7 @@ public class WxChannelProperties {
/**
* http客户端类型
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机

View File

@@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -79,7 +79,7 @@ public class WxMpProperties {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机.

View File

@@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -74,7 +74,7 @@ public class WxQidianProperties {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机.

View File

@@ -9,6 +9,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.channel.api.WxChannelService;
import me.chanjar.weixin.channel.api.impl.WxChannelServiceHttpClientImpl;
import me.chanjar.weixin.channel.api.impl.WxChannelServiceHttpComponentsImpl;
import me.chanjar.weixin.channel.api.impl.WxChannelServiceImpl;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.config.impl.WxChannelDefaultConfigImpl;
@@ -84,6 +85,9 @@ public abstract class AbstractWxChannelConfiguration {
case HTTP_CLIENT:
wxChannelService = new WxChannelServiceHttpClientImpl();
break;
case HTTP_COMPONENTS:
wxChannelService = new WxChannelServiceHttpComponentsImpl();
break;
default:
wxChannelService = new WxChannelServiceImpl();
break;

View File

@@ -8,7 +8,7 @@ package com.binarywang.spring.starter.wxjava.channel.enums;
*/
public enum HttpClientType {
/**
* HttpClient
* HttpClient.
*/
HTTP_CLIENT,
// WxChannelServiceOkHttpImpl 实现经测试无法正常完成业务固暂不支持OK_HTTP方式
@@ -16,4 +16,8 @@ public enum HttpClientType {
// * OkHttp.
// */
// OK_HTTP,
/**
* HttpComponents.
*/
HTTP_COMPONENTS,
}

View File

@@ -7,7 +7,11 @@ package com.binarywang.spring.starter.wxjava.channel.enums;
*/
public enum HttpClientType {
/**
* HttpClient
* HttpClient.
*/
HttpClient
HttpClient,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -85,7 +85,7 @@ public class WxChannelProperties {
/**
* http客户端类型
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机

View File

@@ -8,7 +8,7 @@ package com.binarywang.spring.starter.wxjava.miniapp.enums;
*/
public enum HttpClientType {
/**
* HttpClient.
* HttpClient (Apache HttpClient 4.x).
*/
HttpClient,
/**
@@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents (Apache HttpClient 5.x).
*/
HttpComponents,
}

View File

@@ -27,7 +27,7 @@
#wx.mp.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379
#wx.mp.config-storage.redis.sentinel-name=mymaster
# http客户端配置
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
wx.mp.config-storage.http-client-type=HttpComponents # http客户端类型: HttpComponents(Apache HttpClient 5.x推荐), HttpClient(Apache HttpClient 4.x), OkHttp, JoddHttp
wx.mp.config-storage.http-proxy-host=
wx.mp.config-storage.http-proxy-port=
wx.mp.config-storage.http-proxy-username=

View File

@@ -80,7 +80,7 @@ public class WxMpProperties {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机.

View File

@@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents.
*/
HttpComponents,
}

View File

@@ -72,7 +72,7 @@ public class WxQidianProperties {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;
/**
* http代理主机.

View File

@@ -24,20 +24,13 @@
<artifactId>okhttp</artifactId>
<scope>provided</scope>
</dependency>
<!-- HttpClient 5.x - 默认依赖(推荐使用) -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</dependency>
<!-- HttpClient 4.x - 默认依赖(为了保持向后兼容) -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
@@ -52,6 +45,15 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>