Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[task] optimize okhttp3 connection factory #2539

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7dc0d74
[type:feature] optimize okhttp3 connection factory
Aias00 Aug 16, 2024
4ac5388
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 16, 2024
6387176
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
yuluo-yx Aug 16, 2024
144b8dc
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
yuluo-yx Aug 16, 2024
af3be5c
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 17, 2024
ab72dc0
[type:feature] optimize okhttp3 connection factory
Aias00 Aug 17, 2024
9c2ec89
Merge remote-tracking branch 'origin/feature/optimize_okhttp3_connect…
Aias00 Aug 17, 2024
b98de51
[type:feature] optimize okhttp3 connection factory
Aias00 Aug 17, 2024
90be542
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
yuluo-yx Aug 17, 2024
f7e550a
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
yuluo-yx Aug 17, 2024
bd07b01
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 17, 2024
ec6cd88
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 17, 2024
2351134
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
yuluo-yx Aug 18, 2024
a87821a
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 20, 2024
02b4a9d
[type:feature] remove httpclient4 dependency import
Aias00 Aug 20, 2024
b2722d3
Merge branch 'apache:master' into feature/optimize_okhttp3_connection…
Aias00 Aug 20, 2024
93d7ff5
[type:feature] kingbase spotless apply
Aias00 Aug 20, 2024
dc6bfea
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 21, 2024
9888cdd
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 21, 2024
230e23e
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 21, 2024
fbab487
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 21, 2024
140f54a
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 21, 2024
9d34e5d
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 22, 2024
71d987d
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 22, 2024
b2dfe03
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 22, 2024
7ef4c76
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 22, 2024
4f8feb3
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 22, 2024
9565039
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 23, 2024
d2b7d75
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 23, 2024
e949ca6
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 23, 2024
9b7bdc6
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 24, 2024
cad1d46
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 26, 2024
1c01829
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 28, 2024
4af1929
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 29, 2024
3b9c79d
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 30, 2024
be9fad9
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 30, 2024
ca65d05
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 31, 2024
c67112b
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 31, 2024
81b561a
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Aug 31, 2024
588eb83
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 2, 2024
f022e6c
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 3, 2024
ea25803
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 4, 2024
d91a2f9
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 4, 2024
2a00f26
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 4, 2024
7db4bfa
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 5, 2024
9a7feec
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 6, 2024
9654ac4
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 7, 2024
19ed19c
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 8, 2024
1700e00
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 9, 2024
3502c0a
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 12, 2024
58504bd
Merge branch 'master' into feature/optimize_okhttp3_connection_factory
Aias00 Sep 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions collector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
</dependency>
<!-- http -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<!--network-->
<dependency>
Expand Down Expand Up @@ -182,6 +182,12 @@
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
<version>${consul-api.version}</version>
</dependency>
<!-- nacos -->
Expand Down
4 changes: 2 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
</dependency>
<!-- Use apache.http.conn.util instead of sun.net.util -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<!-- caffeine-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ interface HttpClientConstants {

int READ_TIME_OUT = 6 * 1000;
int WRITE_TIME_OUT = 6 * 1000;
int CONNECT_TIME_OUT = 6 * 1000;
int CONNECT_TIME_OUT = 15;
int SOCKET_TIME_OUT = 20;
int CHUNK_SIZE = 8196;
int MAX_IDLE_CONNECTIONS = 20;
int KEEP_ALIVE_TIMEOUT = 30 * 1000;
Expand All @@ -67,6 +68,8 @@ interface HttpClientConstants {
int HTTPCLIENT_KEEP_ALIVE_DURATION = 30 * 1000;
int HTTP_CLIENT_CONNECTION_MANAGER_CLOSE_WAIT_TIME_MS = 1000;
int HTTP_CLIENT_CONNECTION_MANAGER_CLOSE_IDLE_TIME_S = 30;
int HTTP_CLIENT_MAX_CONNECT_TOTAL = 200;
int HTTP_CLIENT_MAX_CONNECT_PRE_ROUTE = 200;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.Enumeration;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.core5.net.InetAddressUtils;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.http.conn.util.InetAddressUtils;
import org.springframework.util.StringUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

import java.util.Collections;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.apache.hertzbeat.common.constants.NetworkConstants;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
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.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.util.Timeout;
import org.apache.hertzbeat.common.constants.NetworkConstants.HttpClientConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
Expand All @@ -41,21 +46,30 @@ public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
restTemplate.setInterceptors(Collections.singletonList(new HeaderRequestInterceptor()));
return restTemplate;
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {

return new OkHttp3ClientHttpRequestFactory(
new OkHttpClient.Builder()
.readTimeout(NetworkConstants.HttpClientConstants.READ_TIME_OUT, TimeUnit.SECONDS)
.writeTimeout(NetworkConstants.HttpClientConstants.WRITE_TIME_OUT, TimeUnit.SECONDS)
.connectTimeout(NetworkConstants.HttpClientConstants.CONNECT_TIME_OUT, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(
NetworkConstants.HttpClientConstants.MAX_IDLE_CONNECTIONS,
NetworkConstants.HttpClientConstants.KEEP_ALIVE_TIMEOUT,
TimeUnit.SECONDS)
).build()
);
public ClientHttpRequestFactory httpComponentsClientHttpRequestFactory() {
ConnectionConfig connectionConfig = ConnectionConfig
.custom()
.setConnectTimeout(Timeout.of(HttpClientConstants.CONNECT_TIME_OUT, TimeUnit.SECONDS))
.setSocketTimeout(Timeout.of(HttpClientConstants.SOCKET_TIME_OUT, TimeUnit.SECONDS))
.setValidateAfterInactivity(Timeout.of(HttpClientConstants.MAX_IDLE_CONNECTIONS, TimeUnit.SECONDS))
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(HttpClientConstants.CONNECT_TIME_OUT, TimeUnit.SECONDS)
.setResponseTimeout(HttpClientConstants.SOCKET_TIME_OUT, TimeUnit.SECONDS)
.build();
HttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder
.create()
.setDefaultConnectionConfig(connectionConfig)
.setMaxConnTotal(HttpClientConstants.HTTP_CLIENT_MAX_CONNECT_TOTAL)
.setMaxConnPerRoute(HttpClientConstants.HTTP_CLIENT_MAX_CONNECT_PRE_ROUTE)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.build();
return new HttpComponentsClientHttpRequestFactory(httpClient);
}

}
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<aliYun-sdk-java-sms.version>2.0.24</aliYun-sdk-java-sms.version>
<caffeine.version>2.9.3</caffeine.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient5.version>5.3.1</httpclient5.version>

<lombok.version>1.18.28</lombok.version>
<slf4j.version>2.0.9</slf4j.version>
Expand Down Expand Up @@ -271,9 +272,9 @@
<version>${commons-net}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Loading