Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from ralf-ueberfuhr-ars/feature/api-consumer-co…
Browse files Browse the repository at this point in the history
…nfig

Make API client configurable
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 26, 2024
2 parents 0054cf6 + 57a02a7 commit 958e975
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
8 changes: 7 additions & 1 deletion account-service-consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
server:
port: 8081
port: 8081
application:
api-clients:
customers:
base-url: ${CUSTOMERS_API_PROVIDER_URL:http://localhost:8080/api/v1}
# connection-timeout: 100
# response-timeout: 1000

0 comments on commit 958e975

Please sign in to comment.