Skip to content

Commit

Permalink
[41] rest template 을 connection pool 로 관리하기
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsuha committed Oct 28, 2024
1 parent d62f2a3 commit 0e7668f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/org/example/commerce_site/config/AppConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 7 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ auth:
logging:
level:
org:
apache:
http: DEBUG
tomcat.util.net: DEBUG
springframework:
security: DEBUG
security: DEBUG
sun:
security:
ssl: DEBUG

0 comments on commit 0e7668f

Please sign in to comment.