diff --git a/catalog-service/src/main/java/com/example/catalogservice/config/WebClientConfiguration.java b/catalog-service/src/main/java/com/example/catalogservice/config/WebClientConfiguration.java index 89c8adbe..a70e2dfd 100644 --- a/catalog-service/src/main/java/com/example/catalogservice/config/WebClientConfiguration.java +++ b/catalog-service/src/main/java/com/example/catalogservice/config/WebClientConfiguration.java @@ -1,6 +1,6 @@ /***
- Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli. + Licensed under MIT License Copyright (c) 2022-2024 Raja Kolli.
***/ @@ -11,9 +11,11 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli. import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; +import java.time.Duration; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -23,6 +25,8 @@ Licensed under MIT License Copyright (c) 2022-2023 Raja Kolli. import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import reactor.netty.http.client.HttpClient; +import reactor.netty.resources.ConnectionProvider; +import reactor.util.retry.Retry; @Configuration(proxyBeanMethods = false) public class WebClientConfiguration { @@ -37,14 +41,30 @@ public WebClientConfiguration(ApplicationProperties applicationProperties) { @Bean WebClient webClient(WebClient.Builder webClientBuilder) { - return webClientBuilder - .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .filter(logRequestDetails()) - .filter(logResponseDetails()) - .baseUrl(applicationProperties.getInventoryServiceUrl()) - .clientConnector(new ReactorClientHttpConnector(clientConnectorConfig())) - .build(); + return webClientBuilder.baseUrl(applicationProperties.getInventoryServiceUrl()).build(); + } + + @Bean + WebClientCustomizer webClientCustomizer() { + return webClientBuilder -> + webClientBuilder + .defaultHeaders( + httpHeaders -> { + httpHeaders.add( + HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + httpHeaders.add( + HttpHeaders.CONTENT_TYPE, + MediaType.APPLICATION_JSON_VALUE); + httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip"); + }) + .filter(logRequestDetails()) + .filter(logResponseDetails()) + .filter( + (request, next) -> + next.exchange(request) + .retryWhen( + Retry.backoff(3, Duration.ofMillis(100)))) + .clientConnector(new ReactorClientHttpConnector(clientConnectorConfig())); } private ExchangeFilterFunction logRequestDetails() { @@ -85,8 +105,17 @@ private ExchangeFilterFunction logResponseDetails() { } private HttpClient clientConnectorConfig() { - return HttpClient.create() + ConnectionProvider connectionProvider = + ConnectionProvider.builder("custom") + .maxConnections(10) + .pendingAcquireMaxCount(50) + .maxIdleTime(Duration.ofSeconds(20)) + .maxLifeTime(Duration.ofSeconds(60)) + .build(); + + return HttpClient.create(connectionProvider) .option(CONNECT_TIMEOUT_MILLIS, 10_000) + .responseTimeout(Duration.ofSeconds(10)) .doOnConnected( conn -> { conn.addHandlerLast(new ReadTimeoutHandler(15, TimeUnit.SECONDS)); diff --git a/catalog-service/src/test/java/com/example/catalogservice/TestCatalogServiceApplication.java b/catalog-service/src/test/java/com/example/catalogservice/TestCatalogServiceApplication.java index e60f9beb..edd5477a 100644 --- a/catalog-service/src/test/java/com/example/catalogservice/TestCatalogServiceApplication.java +++ b/catalog-service/src/test/java/com/example/catalogservice/TestCatalogServiceApplication.java @@ -37,7 +37,7 @@ PostgreSQLContainer> postgreSqlContainer() { @ServiceConnection KafkaContainer kafkaContainer(DynamicPropertyRegistry dynamicPropertyRegistry) { KafkaContainer kafkaContainer = - new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.6.0")) + new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.6.2")) .withKraft() .withReuse(true); dynamicPropertyRegistry.add(