티스토리 뷰
CloseableHttpClient를 생성할때 생성하려면 아래와 같이 기본 설정으로 생성할 수 있다.
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
기본 설정으로 HttpClient를 생성할 경우, 기본값이 아래와 같기 때문에 performance가 않나올 수 있다.
- this.pool = new CPool(new InternalConnectionFactory(this.configData, connFactory), 2, 20, timeToLive, tunit);
때문에 아래 예제 코드와 같이 사용자가 설정을 직접 정의하여 HttpClient를 생성하는것도 고려해야 한다.
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@Slf4j
public class CustomHttpExecutor {
private final CloseableHttpClient httpClient;
public CustomHttpExecutor(RequestConfig requestConfig) {
this.httpClient = createDefaultHttpClient(requestConfig, defaultConnectionManager());
}
private static CloseableHttpClient createDefaultHttpClient(RequestConfig requestConfig, PoolingHttpClientConnectionManager connectionManager) {
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
}
private PoolingHttpClientConnectionManager defaultConnectionManager() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(50);
return cm;
}
public String getForString(String url) throws IOException {
HttpGet request = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(request)) {
int httpStatus = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
log.debug("Http get is success. : url({}), httpStatus({}, {}), responseBody({})",
url, response.getStatusLine().getReasonPhrase(), httpStatus, responseBody);
response.close();
return responseBody;
} catch (IOException e) {
log.warn("Http get is failed. : url({})", url);
throw e;
}
}
}
Apache의 httpClient는 Spring의 RestClient와 함께 사용될 수 있다.
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
private RestTemplate getRestTemplate() {
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(poolingHttpClientConnectionManager)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectionRequestTimeout(200)
.setConnectTimeout(200)
.setSocketTimeout(200)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build())
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(50);
poolingHttpClientConnectionManager.setMaxTotal(100);
factory.setHttpClient(httpClient);
return new RestTemplate(factory);
}
** 주요 설정 :
- MaxTotal은 HttpClient에서 사용하는 전체 Thread 수 이다.
- Route는 URL Domain으로 구분된다.
- MaxPerRoute는 URL Domain별로 사용될 수 있는 최대 Thread 수 이다.
- 전체 MaxPerRoute 수가 MaxTotal을 초과하면 각 Route는 MaxTotal 내에서 경쟁한다.
example)
Total Max Thread = 100
-------------------------------------
Route1 (www.naver.com/...) 50
-------------------------------------
Route2 (www.daum.com/...) 50
-------------------------------------
Route3 (www.coupang.com/...) 50
-------------------------------------
** 성능개선 참고 사이트 :
- http://hc.apache.org/httpclient-3.x/performance.html#Reuse_of_HttpClient_instance
'Programming > 유용한 라이브러리' 카테고리의 다른 글
JsonPath (0) | 2020.04.03 |
---|---|
MapStruct (0) | 2019.08.30 |
Guava AsyncEventBus (0) | 2019.02.07 |
Guava (0) | 2019.02.07 |
Flyway 설정 (0) | 2019.01.18 |
- Total
- Today
- Yesterday
- scikit-learn
- Registrar
- Sprint RetryTemplate
- Property
- Embeddable Mapping
- Mapping
- SmartLifecycle
- Spring JDBC Template
- docker
- Query DSL
- DI
- java EqualsAndHashCode
- RetryTemplate
- spring spel
- Discriminate Mapping
- 복합키 Mapping
- guava
- JPA
- Akka
- java generic
- @Primary
- Embedded Mapping
- JPA Criteria
- Join Table
- Spring
- Charles proxy
- Criteria
- Typesafe Config
- java Equals
- Spring Registrar
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |