
๊ณต๊ณต๊ธฐ๊ด ์ธ๋ถ API๋ฅผ
RestTemplate์ผ๋ก ๋ถ๋ฅด๋ค๊ฐ ํธ๋ํฝ์ด ๋์ ํ์์์๊ณผ ๋คํธ์ํฌ ์ค๋ฅ๊ฐ ์ฆ์์ก๋ค. ์๋ต์ ๊ธฐ๋ค๋ฆฌ๋ ๋์ ์ค๋ ๋๊ฐ ๋ฌถ์ฌ ์๋ ๋ธ๋กํน ๊ตฌ์กฐ๋ผ์ ์ธ๋ถ API๊ฐ ๋๋ ค์ง๋ฉด ์๋น์ค ์ ์ฒด๊ฐ ๊ฐ์ด ๋๋ ค์ก๋ค. ๊ทธ๋์ WebClient๋ก ๋ฐ๊พธ๋ฉด์ ํจ๊ป ๋ค๋ฃจ๊ฒ ๋ HttpClient์ ReactorClientHttpConnector๊ฐ ๊ฐ๊ฐ ๋ฌด์์ด๊ณ ์ด๋ป๊ฒ ์ฎ๋์ง ์ ๋ฆฌํด ๋ณธ๋ค.
WebClient โ ReactorClientHttpConnector โ HttpClient (Reactor Netty)
๊ณ ์์ค API ๋์ ์๋ ์ด๋ํฐ ์ ์์ค ๋คํธ์ํฌ ์ค์
ByteBuf๋ก ๋ฐ์์ JSON์ ์ง์ ํ์ฑํด์ผ ํ๋คโ ๏ธ Java 11์ ๋ค์ด๊ฐ java.net.http.HttpClient์ ์ด๋ฆ๋ง ๊ฐ๊ณ ๋ค๋ฅธ ํด๋์ค๋ค. ์ด ๊ธ์ HttpClient๋ reactor.netty.http.client.HttpClient๋ค.
| ๊ตฌ๋ถ | ๐ป WebClient | ๐ก HttpClient |
|---|---|---|
| ์์ค | ๊ณ ์์ค | ์ ์์ค |
| ๊ธฐ๋ฐ | Spring WebFlux | Reactor Netty |
| ์ง๋ ฌํ | ์๋ (Jackson ๋ฑ) | ์ง์ ํ์ฑ |
| ์ธ๋ถ ์ค์ | ์ปค๋ฅํฐ๋ฅผ ๊ฑฐ์ณ์ | ์์ ๋กญ๊ฒ |
| ๋ฐฐ์ฐ๊ธฐ | ๋ณดํต | ์ด๋ ต๋ค |
| ์ด๋์ | ์ผ๋ฐ์ ์ธ REST API ํธ์ถ | ๋คํธ์ํฌ ์ธ๋ถ ์กฐ์ ์ด๋ ํน์ํ ์ํฉ |
| ๐ป WebClient | ๐ก HttpClient | |
|---|---|---|
| โ ์ฅ์ | ์ง๋ ฌํ๋ฅผ ์์์ ํด ์ค๋ค | ์ปค๋ฅ์ ํ๊ณผ ํ์์์๊ณผ TLS์ ํ๋ก์๊น์ง ์กฐ์ ํ ์ ์๋ค |
| ํํฐ์ ์ฌ์๋์ ๋ฐฑํ๋ ์ ๋ฅผ ์ฝ๊ฒ ๋ถ์ธ๋ค | ๋๊ท๋ชจ ํธ๋ํฝ์ด๋ ํน์ ํ๋กํ ์ฝ์ ๋์ํ ์ ์๋ค | |
| ์ฝ๋๊ฐ ์งง๋ค | ||
| โ ๏ธ ๋จ์ | ๋คํธ์ํฌ ์ธ๋ถ ์ค์ ์ ๊ฒฐ๊ตญ HttpClient์ ๊ธฐ๋๋ค | JSON ํ์ฑ๊ณผ ์๋ฌ ์ฒ๋ฆฌ์ ์ฌ์๋๋ฅผ ์ง์ ์ง์ผ ํ๋ค |
| ๋ฆฌ์กํฐ๋ธ ํ๋ก๊ทธ๋๋ฐ์ ๋ฐ๋ก ์ตํ์ผ ํ๋ค | ์ฝ๋๊ฐ ๋ณต์กํด์ง๋ค |
์ปค๋ฅ์ ์ ์ฌ์ฌ์ฉํ๋ ค๊ณ ํ์ ๋ฐ๋ก ์ ์ํ๋ค.
@Bean
public ConnectionProvider connectionProvider() {
return ConnectionProvider.builder("test-pool")
.maxConnections(100)
.pendingAcquireMaxCount(500)
.pendingAcquireTimeout(Duration.ofSeconds(30))
.build();
}
@Bean
public HttpClient httpClient(ConnectionProvider connectionProvider) {
SslContext sslContext;
try {
sslContext = SslContextBuilder.forClient().build();
} catch (SSLException e) {
throw new CustomRuntimeException(ErrorEnum.SSL_CONTEXT_ERROR);
}
return HttpClient.create(connectionProvider)
.secure(spec -> spec.sslContext(sslContext).handshakeTimeout(Duration.ofSeconds(30)))
.responseTimeout(Duration.ofSeconds(90))
.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(90))
.addHandlerLast(new WriteTimeoutHandler(5)));
}
@Bean
public WebClient testWebClient(HttpClient httpClient) {
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory("https://example.com/test");
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient)) // ๐ ์ฌ๊ธฐ์ ์ฐ๊ฒฐ
.uriBuilderFactory(factory)
.exchangeStrategies(ExchangeStrategies.builder()
.codecs(c -> c.defaultCodecs().maxInMemorySize(10 * 1024 * 1024)) // 10MB
.build())
.build();
}
DataBufferLimitException์ด ๋์ ํฐ ์๋ต์ ๋ฐ๋ API๋ maxInMemorySize๋ฅผ ๋๋ ธ๋คEncodingMode.VALUES_ONLY๋ URI ํ
ํ๋ฆฟ์ ๊ทธ๋๋ก ๋๊ณ URI ๋ณ์์ ๋ฃ๋ ๊ฐ๋ง ์ธ์ฝ๋ฉํ๋ค. ๊ณต๊ณต API ์๋น์ค ํค์ฒ๋ผ ํน์๋ฌธ์๊ฐ ๋ ๊ฐ์ด ์๋์ ๋ค๋ฅด๊ฒ ์ธ์ฝ๋ฉ๋๋ ๋ฌธ์ ๋ฅผ ํผํ ๋ ์์ฃผ ์ด๋คuriBuilderFactory๋ฅผ ๋ฃ์ผ๋ฉด ๊ธฐ๋ณธ ์ฃผ์๋ ์ด ํฉํ ๋ฆฌ๊ฐ ์ ํ๋ค. ๊ทธ๋์ baseUrl์ ๋ฐ๋ก ๋์ง ์์๋ค@Slf4j
@Component
@RequiredArgsConstructor
public class TestProvider {
private final WebClient testWebClient;
public Mono<TestDTO> fetch() {
return testWebClient.get()
.uri(uriBuilder -> uriBuilder
.queryParam("serviceKey", "testKey")
.queryParam("pageNo", 1)
.queryParam("numOfRows", 1000)
.queryParam("returnType", "JSON")
.build())
.retrieve()
.bodyToMono(TestDTO.class)
.doOnError(TimeoutException.class, e -> log.error("โณ TimeoutException: {}", e.getMessage()))
.doOnError(IllegalStateException.class, e -> log.error("โณ IllegalStateException: {}", e.getMessage()))
.doOnError(WebClientResponseException.class, e -> log.error("โณ WebClientResponseException: {}", e.getMessage()))
.retryWhen(Retry.fixedDelay(5, Duration.ofSeconds(5))
.filter(t -> t instanceof TimeoutException
|| t instanceof IllegalStateException
|| t instanceof WebClientResponseException)
.doBeforeRetry(signal -> log.warn("๐ [ํ
์คํธ ์๋ํํฐ {}ํ์ฐจ ์ฌ์๋] ์ด์ : {}",
signal.totalRetries() + 1, signal.failure().getMessage())));
}
}
retryWhen์ผ๋ก ํ์์์์ด๋ ์๋ต ์ค๋ฅ๊ฐ ๋๋ฉด 5์ด ๊ฐ๊ฒฉ์ผ๋ก 5๋ฒ๊น์ง ๋ค์ ๋ถ๋ฅธ๋คโ ๏ธ WebClientResponseException์๋ 400๋ฒ๋ ์ค๋ฅ๋ ๋ค์ด๊ฐ๋ค. ์์ฒญ์ด ์๋ชป๋ผ์ ๋ 400๋ฒ๋๋ ๋ค์ ๋ถ๋ฌ๋ ๋๊ฐ์ด ์คํจํ๋ ์ฌ์๋๋ 500๋ฒ๋์ ํ์์์์๋ง ๊ฑฐ๋ ํธ์ด ๋ซ๋ค.
RestTemplate์์ WebClient๋ก ๋ฐ๊พธ๊ณ ๋์ ์ธ๋ถ API๊ฐ ๋๋ ค์ ธ๋ ์ค๋ ๋๊ฐ ๋ฌถ์ด์ง ์์๋ค. ์ฌ์๋์ ํ์์์๋ ๋ฉ์๋ ์ฒด์ด๋์ผ๋ก ํ๊ณณ์ ๋ชจ์ฌ์ ์ฅ์ ๊ฐ ๋ฌ์ ๋ ๋น ๋ฅด๊ฒ ์คํจํ๊ณ ๋ค์ ์๋ํ๋ ํ๋ฆ์ด ๋จ์ํด์ก๋ค.