This is an automated email from the ASF dual-hosted git repository.
jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new 0011d5d965 refactor: replace Apache HttpClient with OkHttp in
NamingServer (#7957)
0011d5d965 is described below
commit 0011d5d965e6ea3d453d80fdf515f0b3160faa43
Author: aias00 <[email protected]>
AuthorDate: Mon Jan 26 16:55:30 2026 +0800
refactor: replace Apache HttpClient with OkHttp in NamingServer (#7957)
---
changes/en-us/2.x.md | 2 ++
changes/zh-cn/2.x.md | 3 +-
namingserver/pom.xml | 15 ----------
.../seata/namingserver/config/WebConfig.java | 34 ++++++++++++----------
.../namingserver/contants/NamingConstant.java | 2 ++
.../seata/namingserver/manager/NamingManager.java | 6 ++--
6 files changed, 26 insertions(+), 36 deletions(-)
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 39c893599d..f97944d6e8 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -47,6 +47,7 @@ Add changes here for all PR submitted to the 2.x branch.
### refactor:
+- [[#7957](https://github.com/apache/incubator-seata/pull/7957)] replace
Apache HttpClient with OkHttp in NamingServer
### doc:
@@ -62,6 +63,7 @@ Thanks to these contributors for their code commits. Please
report an unintended
- [funky-eyes](https://github.com/funky-eyes)
- [maple525866](https://github.com/maple525866)
- [neronsoda](https://github.com/neronsoda)
+- [aias00](https://github.com/Aias00)
Also, we receive many valuable issues, questions and advices from our
community. Thanks for you all.
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index cf9dc17c66..2b843db31b 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -46,6 +46,7 @@
### refactor:
+- [[#7957](https://github.com/apache/incubator-seata/pull/7957)] 在
NamingServer 用 OkHttp 替换 Apache HttpClient
### doc:
@@ -61,6 +62,6 @@
- [funky-eyes](https://github.com/funky-eyes)
- [maple525866](https://github.com/maple525866)
- [neronsoda](https://github.com/neronsoda)
-
+- [aias00](https://github.com/Aias00)
同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
diff --git a/namingserver/pom.xml b/namingserver/pom.xml
index 46268dc064..862ebb90ec 100644
--- a/namingserver/pom.xml
+++ b/namingserver/pom.xml
@@ -36,7 +36,6 @@
<spring-framework-for-server.version>6.2.8</spring-framework-for-server.version>
<snakeyaml-for-server.version>2.0</snakeyaml-for-server.version>
<tomcat-embed.version>11.0.12</tomcat-embed.version>
- <httpclient.version>5.4.3</httpclient.version>
</properties>
<dependencyManagement>
@@ -160,30 +159,16 @@
<version>${project.version}</version>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- <version>${httpclient.version}</version>
- </dependency>
-
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpasyncclient</artifactId>
- </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
diff --git
a/namingserver/src/main/java/org/apache/seata/namingserver/config/WebConfig.java
b/namingserver/src/main/java/org/apache/seata/namingserver/config/WebConfig.java
index 4bb419555a..563a9b7c21 100644
---
a/namingserver/src/main/java/org/apache/seata/namingserver/config/WebConfig.java
+++
b/namingserver/src/main/java/org/apache/seata/namingserver/config/WebConfig.java
@@ -17,40 +17,42 @@
package org.apache.seata.namingserver.config;
import jakarta.servlet.Filter;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.classic.HttpClients;
-import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
+import okhttp3.Dispatcher;
+import okhttp3.OkHttpClient;
import org.apache.seata.namingserver.filter.ConsoleRemotingFilter;
import org.apache.seata.namingserver.manager.NamingManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
-import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
+import java.util.concurrent.TimeUnit;
+
import static
org.apache.seata.namingserver.contants.NamingConstant.DEFAULT_CONNECTION_MAX_PER_ROUTE;
import static
org.apache.seata.namingserver.contants.NamingConstant.DEFAULT_CONNECTION_MAX_TOTAL;
import static
org.apache.seata.namingserver.contants.NamingConstant.DEFAULT_REQUEST_TIMEOUT;
+import static
org.apache.seata.namingserver.contants.NamingConstant.DEFAULT_WRITE_TIMEOUT;
@Configuration
public class WebConfig {
@Bean
public RestTemplate restTemplate() {
- // Create a connection manager with custom settings
- PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager();
- connectionManager.setMaxTotal(DEFAULT_CONNECTION_MAX_TOTAL); //
Maximum total connections
-
connectionManager.setDefaultMaxPerRoute(DEFAULT_CONNECTION_MAX_PER_ROUTE); //
Maximum connections per route
- // Create an HttpClient with the connection manager
- CloseableHttpClient httpClient =
-
HttpClients.custom().setConnectionManager(connectionManager).build();
- // Create a request factory with the HttpClient
- HttpComponentsClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory(httpClient);
- requestFactory.setConnectTimeout(DEFAULT_REQUEST_TIMEOUT); //
Connection timeout in milliseconds
- requestFactory.setReadTimeout(DEFAULT_REQUEST_TIMEOUT); // Read
timeout in milliseconds
+ Dispatcher dispatcher = new Dispatcher();
+ dispatcher.setMaxRequests(DEFAULT_CONNECTION_MAX_TOTAL);
+ dispatcher.setMaxRequestsPerHost(DEFAULT_CONNECTION_MAX_PER_ROUTE);
+
+ OkHttpClient client = new OkHttpClient.Builder()
+ .dispatcher(dispatcher)
+ .connectTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS)
+ .readTimeout(DEFAULT_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS)
+ .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
+ .build();
+
// Create and return a RestTemplate with the custom request factory
- return new RestTemplate(requestFactory);
+ return new RestTemplate(new OkHttp3ClientHttpRequestFactory(client));
}
@Bean
diff --git
a/namingserver/src/main/java/org/apache/seata/namingserver/contants/NamingConstant.java
b/namingserver/src/main/java/org/apache/seata/namingserver/contants/NamingConstant.java
index bc4c75efd9..6d0852a7df 100644
---
a/namingserver/src/main/java/org/apache/seata/namingserver/contants/NamingConstant.java
+++
b/namingserver/src/main/java/org/apache/seata/namingserver/contants/NamingConstant.java
@@ -21,6 +21,8 @@ public interface NamingConstant {
String CONSOLE_PATTERN = "^/api/.*/console/.*";
int DEFAULT_REQUEST_TIMEOUT = 5000;
+
+ int DEFAULT_WRITE_TIMEOUT = 5000;
int DEFAULT_CONNECTION_MAX_TOTAL = 100;
diff --git
a/namingserver/src/main/java/org/apache/seata/namingserver/manager/NamingManager.java
b/namingserver/src/main/java/org/apache/seata/namingserver/manager/NamingManager.java
index 895b2a1e9d..a2f13a79d8 100644
---
a/namingserver/src/main/java/org/apache/seata/namingserver/manager/NamingManager.java
+++
b/namingserver/src/main/java/org/apache/seata/namingserver/manager/NamingManager.java
@@ -22,8 +22,6 @@ import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import jakarta.annotation.PostConstruct;
import okhttp3.Response;
-import org.apache.http.entity.ContentType;
-import org.apache.http.protocol.HTTP;
import org.apache.seata.common.NamingServerConstants;
import org.apache.seata.common.metadata.Cluster;
import org.apache.seata.common.metadata.ClusterRole;
@@ -224,7 +222,7 @@ public class NamingManager {
params.put(CONSTANT_GROUP, vGroup);
params.put(NamingServerConstants.CONSTANT_UNIT, actualUnitName);
Map<String, String> header = new HashMap<>();
- header.put(HTTP.CONTENT_TYPE,
ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
+ header.put("Content-Type", "application/x-www-form-urlencoded");
try (Response httpResponse = HttpClientUtil.doGet(httpUrl, params,
header, 3000)) {
if (httpResponse == null || httpResponse.code() != 200) {
@@ -257,7 +255,7 @@ public class NamingManager {
params.put(CONSTANT_GROUP, vGroup);
params.put(NamingServerConstants.CONSTANT_UNIT, unitName);
Map<String, String> header = new HashMap<>();
- header.put(HTTP.CONTENT_TYPE,
ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
+ header.put("Content-Type", "application/x-www-form-urlencoded");
try (Response httpResponse = HttpClientUtil.doGet(httpUrl, params,
header, 3000)) {
if (httpResponse == null || httpResponse.code() != 200) {
LOGGER.warn("remove vGroup in old cluster failed");
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]