Adds support for @LoadBalanced WebClient.Builder.

fixes gh-295
This commit is contained in:
Spencer Gibb
2017-12-20 11:20:37 -05:00
parent cb8bfd08f9
commit a2ffa9a9e0
4 changed files with 270 additions and 34 deletions

View File

@@ -393,6 +393,36 @@ The Ribbon client is used to create a full physical address. See
{githubroot}/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java[RibbonAutoConfiguration]
for details of how the `RestTemplate` is set up.
=== Spring WebClient as a Load Balancer Client
`WebClient` can be automatically configured to use the `LoadBalancerClient`. To create a load balanced `WebClient` create a `WebClient.Builder` `@Bean` and use the `@LoadBalanced` qualifier.
[source,java,indent=0]
----
@Configuration
public class MyConfiguration {
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
public class MyClass {
@Autowired
private WebClient.Builder webClientBuilder;
public Mono<String> doOtherStuff() {
return webClientBuilder.build().get().uri("http://stores/stores")
.retrieve().bodyToMono(String.class);
}
}
----
The URI needs to use a virtual host name (ie. service name, not a host name).
The Ribbon client is used to create a full physical address.
==== Retrying Failed Requests
A load balanced `RestTemplate` can be configured to retry failed requests.