From 0e7668fed85b49f7510d5e4d9ff65cbf94558d32 Mon Sep 17 00:00:00 2001 From: suhaoh Date: Mon, 28 Oct 2024 18:33:21 +0900 Subject: [PATCH] =?UTF-8?q?[41]=20rest=20template=20=EC=9D=84=20connection?= =?UTF-8?q?=20pool=20=EB=A1=9C=20=EA=B4=80=EB=A6=AC=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 + .../commerce_site/config/AppConfig.java | 13 ---- .../config/RestTemplateConfig.java | 71 +++++++++++++++++++ src/main/resources/application.yml | 8 ++- 4 files changed, 81 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/org/example/commerce_site/config/AppConfig.java create mode 100644 src/main/java/org/example/commerce_site/config/RestTemplateConfig.java diff --git a/build.gradle b/build.gradle index 6cb9412..5b06fb6 100644 --- a/build.gradle +++ b/build.gradle @@ -47,6 +47,9 @@ dependencies { runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' // Project Common Dependency End ---------------------------------------- + // HttpComponent for restTemplate connection pool + implementation 'org.apache.httpcomponents.client5:httpclient5:5.3' + implementation 'org.apache.httpcomponents.core5:httpcore5:5.3' // QueryDsl implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' diff --git a/src/main/java/org/example/commerce_site/config/AppConfig.java b/src/main/java/org/example/commerce_site/config/AppConfig.java deleted file mode 100644 index 12fa81f..0000000 --- a/src/main/java/org/example/commerce_site/config/AppConfig.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.example.commerce_site.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.client.RestTemplate; - -@Configuration -public class AppConfig { - @Bean - public RestTemplate restTemplate() { - return new RestTemplate(); - } -} diff --git a/src/main/java/org/example/commerce_site/config/RestTemplateConfig.java b/src/main/java/org/example/commerce_site/config/RestTemplateConfig.java new file mode 100644 index 0000000..a00c891 --- /dev/null +++ b/src/main/java/org/example/commerce_site/config/RestTemplateConfig.java @@ -0,0 +1,71 @@ +package org.example.commerce_site.config; + +import static org.springframework.http.HttpStatus.*; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +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.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.example.commerce_site.common.exception.CustomException; +import org.example.commerce_site.common.exception.ErrorCode; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.ResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Configuration +public class RestTemplateConfig { + @Bean + RestTemplate restTemplate() { + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(100); + connectionManager.setDefaultMaxPerRoute(30); + + RequestConfig requestConfig = RequestConfig.custom() + .setResponseTimeout(5000, TimeUnit.MILLISECONDS) + .build(); + + CloseableHttpClient httpClient = HttpClientBuilder.create() + .setDefaultRequestConfig(requestConfig) + .setConnectionManager(connectionManager) + .build(); + + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); + factory.setConnectTimeout(3000); + factory.setHttpClient(httpClient); + + RestTemplate restTemplate = new RestTemplate(factory); + + restTemplate.setErrorHandler(new ResponseErrorHandler() { + @Override + public boolean hasError(ClientHttpResponse response) throws IOException { + return response.getStatusCode().is4xxClientError() || response.getStatusCode().is5xxServerError(); + } + + @Override + public void handleError(ClientHttpResponse response) throws IOException { + log.error("Error occurred : {} ", response.getStatusCode()); + switch (response.getStatusCode()) { + case UNAUTHORIZED: + throw new CustomException(ErrorCode.UNAUTHORIZED); + case FORBIDDEN: + throw new CustomException(ErrorCode.ACCESS_DENIED); + case NOT_FOUND: + throw new CustomException(ErrorCode.NOT_FOUND); + default: + throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR); + } + } + }); + + return restTemplate; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 72dfb0c..7e09bd7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -71,5 +71,11 @@ auth: logging: level: org: + apache: + http: DEBUG + tomcat.util.net: DEBUG springframework: - security: DEBUG \ No newline at end of file + security: DEBUG + sun: + security: + ssl: DEBUG \ No newline at end of file