Skip to content

Commit

Permalink
use okhttp instead of HttpURLConnection in rest template (#1542)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <[email protected]>
  • Loading branch information
tomsun28 authored Feb 8, 2024
1 parent 08f81cb commit 38e47f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
<groupId>com.usthe.sureness</groupId>
<artifactId>spring-boot-starter-sureness</artifactId>
</dependency>
<!-- okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<!-- h2 database-->
<dependency>
<groupId>com.h2database</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,40 @@

package org.dromara.hertzbeat.manager.config;

import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
* restTemplate config
* todo 连接池
* @author tom
*
* @author tom
*/
@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.setInterceptors(Collections.singletonList(new HeaderRequestInterceptor()));
return restTemplate;
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(5000);
return factory;
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build();
return new OkHttp3ClientHttpRequestFactory(client);
}

}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<slf4j.version>1.7.36</slf4j.version>
<xml.bind.version>2.3.0</xml.bind.version>
<snake.yaml.version>1.33</snake.yaml.version>
<okhttp.version>4.12.0</okhttp.version>
<kafka-clients.version>3.4.0</kafka-clients.version>

<mysql.version>8.0.30</mysql.version>
Expand Down Expand Up @@ -286,6 +287,12 @@
<artifactId>tencentcloud-sdk-java-sms</artifactId>
<version>${tencentcloud-sdk-java-sms.version}</version>
</dependency>
<!-- okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<!-- caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
Expand Down

0 comments on commit 38e47f0

Please sign in to comment.