Skip to content

Commit 02fb33a

Browse files
committed
Document using configurers and customisers with @LoadBalanced HTTP client builders. Fixes gh-1407.
1 parent d1d552f commit 02fb33a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/modules/ROOT/pages/spring-cloud-commons/common-abstractions.adoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ public class MyClass {
250250
The URI needs to use a virtual host name (that is, a service name, not a host name).
251251
The `BlockingLoadBalancerClient` is used to create a full physical address.
252252

253+
In order to leverage additional capabilities that Spring Boot provides for `RestClient.Builder` (for example, observability support) you may want to use the autoconfigured
254+
`RestClientBuilderConfigurer` while creating the `@LoadBalanced RestClient.Builder` beans:
255+
256+
[source,java,indent=0]
257+
----
258+
@Configuration
259+
public class MyConfiguration {
260+
261+
@LoadBalanced
262+
@Bean
263+
RestClient.Builder restClientBuilder(RestClientBuilderConfigurer configurer) {
264+
return configurer.configure(RestClient.builder());
265+
}
266+
}
267+
----
268+
253269
IMPORTANT: To use it, add xref:spring-cloud-commons/loadbalancer.adoc#spring-cloud-loadbalancer-starter[Spring Cloud LoadBalancer starter] to your project.
254270

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

349+
In order to leverage additional capabilities that Spring Boot provides for `WebClient.Builder` (for example, observability support) you may want to use the autoconfigured
350+
`WebClientCustomizer` beans while creating the `@LoadBalanced WebClient.Builder` beans:
351+
352+
[source,java,indent=0]
353+
----
354+
@Configuration
355+
public class MyConfiguration {
356+
357+
@Bean
358+
@LoadBalanced
359+
public WebClient.Builder loadBalancedWebClientBuilder(ObjectProvider<WebClientCustomizer> customizerProvider) {
360+
WebClient.Builder builder = WebClient.builder();
361+
customizerProvider.orderedStream().forEach((customizer) -> customizer.customize(builder));
362+
return builder;
363+
}
364+
}
365+
----
366+
333367
IMPORTANT: If you want to use a `@LoadBalanced WebClient.Builder`, you need to have a Spring Cloud LoadBalancer
334368
implementation in the classpath. We recommend that you add the
335369
xref:spring-cloud-commons/loadbalancer.adoc#spring-cloud-loadbalancer-starter[Spring Cloud LoadBalancer starter] to your project.

0 commit comments

Comments
 (0)