-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[41] rest template 을 connection pool 로 관리하기
- Loading branch information
Showing
4 changed files
with
81 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/main/java/org/example/commerce_site/config/AppConfig.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/main/java/org/example/commerce_site/config/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters