다른 부분 정리하느라 좀 늦어졌지만 세 번째로 RestClient에 대해서 정리하려고 한다.
프로젝트 내에서 사용중인 데이터베이스 중에서
Elastic Search 를 위해 사용 중인 라이브러리이다.
RestClient는 Elastic Search REST Client 인 High Level REST Client Library에 포함되어 있는 부분으로
Elastic Search 서버에 HTTP 요청을 위한 Client 용 객체이다.
나는 다음과 같이 사용 중이다.
RestClient를 생성하는 비즈니스 로직을 분석해보며
많이 놀랐다.
public RestClient build() {
if (this.failureListener == null) {
this.failureListener = new FailureListener();
}
CloseableHttpAsyncClient httpClient = (CloseableHttpAsyncClient)AccessController.doPrivileged(new PrivilegedAction<CloseableHttpAsyncClient>() {
public CloseableHttpAsyncClient run() {
return RestClientBuilder.this.createHttpClient();
}
});
RestClient restClient = new RestClient(httpClient, (long)this.maxRetryTimeout, this.defaultHeaders, this.nodes, this.pathPrefix, this.failureListener, this.nodeSelector, this.strictDeprecationMode);
httpClient.start();
return restClient;
}
private CloseableHttpAsyncClient createHttpClient() {
Builder requestConfigBuilder = RequestConfig.custom().setConnectTimeout(1000).setSocketTimeout(30000); // 설정 안했을때 기본값
if (this.requestConfigCallback != null) {
requestConfigBuilder = this.requestConfigCallback.customizeRequestConfig(requestConfigBuilder);
}
try {
final HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build()).setMaxConnPerRoute(10).setMaxConnTotal(30).setSSLContext(SSLContext.getDefault()).setTargetAuthenticationStrategy(new PersistentCredentialsAuthenticationStrategy()); // 설정 안했을때 기본값
if (this.httpClientConfigCallback != null) {
httpClientBuilder = this.httpClientConfigCallback.customizeHttpClient(httpClientBuilder);
}
return (CloseableHttpAsyncClient)AccessController.doPrivileged(new PrivilegedAction<CloseableHttpAsyncClient>() {
public CloseableHttpAsyncClient run() {
return httpClientBuilder.build();
}
});
} catch (NoSuchAlgorithmException var4) {
throw new IllegalStateException("could not create the default ssl context", var4);
RestClient Builder를 제공해주는데
이는 Spring RestTemplate (2번 포스팅의 주제였던)과 동일하게 RestConfig 값을 가지고 RestClient 객체를 만들어서
통신을 하게 구성이 되어있던 것이었다.
차이점이 하나 있다면
RestTemplate는 포스팅을 했던 대부분의 값의 기본값이 -1로 무제한이었다.
하지만 RestClient는 Builer 내에서 자체적으로 설정 값이 없으면 기본 값을 세팅해 준다.
즉, RestTemplate처럼 Connection Pooling이 되지 않거나 무한한 Timeout 값을 가지지는 않는다는 것이다.
항목 | 기본값 설정 여부 | 기본 설정 값 |
connectionRequestTimeout | X | X |
connectTimeout | O | 1000 |
socketTimeout | O | 30000 |
maxConnTotal | O | 30 |
maxConnPerRoute | O | 10 |
connTimeToLive | X | X |
물론 기본 값이 있지만 서비스의 특성에 맞춰서 적절하게 변경은 필수이다.
얼마나 관심이 없었으면...
동일 라이브러리가 사용되는 건지 몰랐을까

(반성하는 나)
'Programing > Spring' 카테고리의 다른 글
Spring Security Role 도입. Autority 써보기 (0) | 2022.06.20 |
---|---|
WAS (Spring Boot) - DB 성능 개선과 최적화 (4) - RedisTemplate (0) | 2022.06.02 |
Spring 양방향 Dependency Injection (0) | 2022.05.29 |
Rest 통신 기반의 CRUD Transaction 처리 해보기 (AOP, Generic) (3) | 2022.05.23 |
WAS (Spring Boot) - DB 성능 개선과 최적화 (2) - RestTemplate (0) | 2022.05.14 |