From 57a02a73470a46f41577c902933356281cd52f93 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr Date: Wed, 26 Jun 2024 13:06:21 +0200 Subject: [PATCH] Make API client configurable --- .../client/CustomersApiConfiguration.java | 36 +++++++++++++------ .../domain/client/WebClientConfig.java | 12 +++++++ .../src/main/resources/application.yml | 8 ++++- 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/WebClientConfig.java diff --git a/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/CustomersApiConfiguration.java b/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/CustomersApiConfiguration.java index ebd6d56..7d766f7 100644 --- a/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/CustomersApiConfiguration.java +++ b/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/CustomersApiConfiguration.java @@ -1,6 +1,8 @@ package de.sample.schulung.accounts.consumer.domain.client; import io.netty.channel.ChannelOption; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.reactive.ReactorClientHttpConnector; @@ -15,22 +17,36 @@ public class CustomersApiConfiguration { @Bean - CustomersApi customersApi() { + @ConfigurationProperties(prefix = "application.api-clients.customers") + WebClientConfig customersWebClientConfig() { + return new WebClientConfig(); + } + + @Bean + WebClient customersWebClient( + @Qualifier("customersWebClientConfig") + WebClientConfig config + ) { var httpClient = HttpClient.create() - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) - .responseTimeout(Duration.ofMillis(1000)); - var webClient = WebClient + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeout()) + .responseTimeout(Duration.ofMillis(config.getResponseTimeout())); + return WebClient .builder() - .baseUrl("http://localhost:8080/api/v1") + .baseUrl(config.getBaseUrl()) .clientConnector(new ReactorClientHttpConnector(httpClient)) .build(); + } + + @Bean + CustomersApi customersApi( + @Qualifier("customersWebClient") + WebClient customersWebClient + ) { var adapter = WebClientAdapter - .create(webClient); - HttpServiceProxyFactory factory = HttpServiceProxyFactory + .create(customersWebClient); + return HttpServiceProxyFactory .builderFor(adapter) - .build(); - - return factory + .build() .createClient(CustomersApi.class); } diff --git a/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/WebClientConfig.java b/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/WebClientConfig.java new file mode 100644 index 0000000..1160e77 --- /dev/null +++ b/account-service-consumer/src/main/java/de/sample/schulung/accounts/consumer/domain/client/WebClientConfig.java @@ -0,0 +1,12 @@ +package de.sample.schulung.accounts.consumer.domain.client; + +import lombok.Data; + +@Data +public class WebClientConfig { + + private String baseUrl = "http://localhost:8080"; + private int connectionTimeout = 100; + private long responseTimeout = 1000; + +} diff --git a/account-service-consumer/src/main/resources/application.yml b/account-service-consumer/src/main/resources/application.yml index 54b155f..23fc228 100644 --- a/account-service-consumer/src/main/resources/application.yml +++ b/account-service-consumer/src/main/resources/application.yml @@ -1,2 +1,8 @@ server: - port: 8081 \ No newline at end of file + port: 8081 +application: + api-clients: + customers: + base-url: ${CUSTOMERS_API_PROVIDER_URL:http://localhost:8080/api/v1} + # connection-timeout: 100 + # response-timeout: 1000 \ No newline at end of file