Skip to content

Commit

Permalink
Document using configurers and customisers with @LoadBalanced HTTP cl…
Browse files Browse the repository at this point in the history
…ient builders. Fixes gh-1407.
  • Loading branch information
OlgaMaciaszek committed Oct 17, 2024
1 parent d1d552f commit 02fb33a
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ public class MyClass {
The URI needs to use a virtual host name (that is, a service name, not a host name).
The `BlockingLoadBalancerClient` is used to create a full physical address.

In order to leverage additional capabilities that Spring Boot provides for `RestClient.Builder` (for example, observability support) you may want to use the autoconfigured
`RestClientBuilderConfigurer` while creating the `@LoadBalanced RestClient.Builder` beans:

[source,java,indent=0]
----
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestClient.Builder restClientBuilder(RestClientBuilderConfigurer configurer) {
return configurer.configure(RestClient.builder());
}
}
----

IMPORTANT: To use it, add xref:spring-cloud-commons/loadbalancer.adoc#spring-cloud-loadbalancer-starter[Spring Cloud LoadBalancer starter] to your project.

[[multiple-restclient-objects]]
Expand Down Expand Up @@ -330,6 +346,24 @@ public class MyClass {
The URI needs to use a virtual host name (that is, a service name, not a host name).
The Spring Cloud LoadBalancer is used to create a full physical address.

In order to leverage additional capabilities that Spring Boot provides for `WebClient.Builder` (for example, observability support) you may want to use the autoconfigured
`WebClientCustomizer` beans while creating the `@LoadBalanced WebClient.Builder` beans:

[source,java,indent=0]
----
@Configuration
public class MyConfiguration {
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder(ObjectProvider<WebClientCustomizer> customizerProvider) {
WebClient.Builder builder = WebClient.builder();
customizerProvider.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder;
}
}
----

IMPORTANT: If you want to use a `@LoadBalanced WebClient.Builder`, you need to have a Spring Cloud LoadBalancer
implementation in the classpath. We recommend that you add the
xref:spring-cloud-commons/loadbalancer.adoc#spring-cloud-loadbalancer-starter[Spring Cloud LoadBalancer starter] to your project.
Expand Down

0 comments on commit 02fb33a

Please sign in to comment.