์Šคํ”„๋ง RestTemplate ํƒ€์ž„์•„์›ƒ ์„ค์ •์„ ํ•˜๋Š”๋ฐ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์ด ์„ค์ •ํ•œ๋Œ€๋กœ ์ ์šฉ๋˜์ง€ ์•Š๋Š” ๋“ฏ ํ•˜์—ฌ ํ…Œ์ŠคํŠธ ํ•ด๋ด„..

๋ณดํ†ต HttpComponentsClientHttpRequestFactory ์™€ SimpleClientHttpRequestFactory ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์ •์„ ํ•จ

์Šคํ”„๋ง์˜ RestTemplate ๊ธฐ๋ณธ์ ์œผ๋กœ SimpleClientHttpRequestFactory๋ฅผ ์‚ฌ์šฉํ•จ

SimpleClientHttpRequestFactory๋ฅผ ์ด์šฉํ•ด์„œ ์„ค์ •์„ ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ์„ธํŒ…ํ•œ ๊ฐ’์— ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•จ.

ํ•˜์ง€๋งŒ HttpComponentsClientHttpRequestFactory ๋ฅผ ์ด์šฉํ•˜๋ฉด ์„ค์ •ํ•œ ์‹œ๊ฐ„๋ณด๋‹ค 4๋ฐฐ ๊ธด ์‹œ๊ฐ„์ด ํ๋ฅธ ๋’ค์—์•ผ ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜์˜€์Œ

@Test
public void ํƒ€์ž„์•„์›ƒ_ํ…Œ์ŠคํŠธ() {

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout((int) TimeUnit.SECONDS.toMillis(10));
factory.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5)); // 4๋ฐฐ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ ๋’ค์—์•ผ ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ
factory.setConnectionRequestTimeout(5 * 1000);

// SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// factory.setReadTimeout((int) TimeUnit.SECONDS.toMillis(10));
// factory.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5)); // ์„ธํŒ…ํ•œ ์‹œ๊ฐ„๋Œ€๋กœ ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ

// RequestConfig config = RequestConfig.custom()
// .setSocketTimeout((int) TimeUnit.SECONDS.toMillis(1))
// .setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5))
// .setConnectionRequestTimeout((int) TimeUnit.SECONDS.toMillis(10))
// .build();
// CloseableHttpClient client = HttpClientBuilder
// .create()
// .setDefaultRequestConfig(config)
// .build();
// HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);

RestTemplate restTemplate = new RestTemplate(factory);

// 1. connect timeout ํ…Œ์ŠคํŠธ
long start = System.currentTimeMillis();
try {
ResponseEntity<String> result = restTemplate.getForEntity("https://abc.com:81/test", String.class);
} catch (ResourceAccessException e) {
log.error("ํƒ€์ž„์•„์›ƒ!! {}", TimeUtils.printDuration(System.currentTimeMillis() - start), e);
}

}


์•„์ง ์›์ธ์€ ํ™•์ธํ•˜์ง€ ๋ชปํ•จ.