Skip to content

Commit c1796a6

Browse files
committed
Fix tests. Add documentation.
1 parent 941747d commit c1796a6

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,89 @@ IMPORTANT: Notice the use of the `@Primary` annotation on the plain `RestTemplat
218218

219219
TIP: If you see errors such as `java.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89`, try injecting `RestOperations` or setting `spring.aop.proxyTargetClass=true`.
220220

221+
[[rest-template-builder-loadbalancer-client]]
222+
== Using `@LoadBalanced RestTemplateBuilder` to create a LoadBalancer Client
223+
224+
You can also configure a `RestTemplate` to use a Load-Balancer client by annotating a
225+
`RestTemplateBuilder` bean with `@LoadBalanced`:
226+
227+
[source,java,indent=0]
228+
----
229+
import org.springframework.boot.web.client.RestTemplateBuilder;@Configuration
230+
public class MyConfiguration {
231+
232+
@Bean
233+
@LoadBalanced
234+
RestTemplateBuilder loadBalancedRestTemplateBuilder() {
235+
return new RestTemplateBuilder();
236+
}
237+
}
238+
239+
public class MyClass {
240+
241+
private final RestTemplate restTemplate;
242+
243+
MyClass(@LoadBalanced RestTemplateBuilder restTemplateBuilder) {
244+
this.restTemplate = restTemplateBuilder.build();
245+
}
246+
247+
public String getStores() {
248+
return restTemplate.getForObject("http://stores/stores", String.class);
249+
}
250+
}
251+
----
252+
253+
The URI needs to use a virtual host name (that is, a service name, not a host name).
254+
The `BlockingLoadBalancerClient` is used to create a full physical address.
255+
256+
IMPORTANT: To use it, add xref:spring-cloud-commons/loadbalancer.adoc#spring-cloud-loadbalancer-starter[Spring Cloud LoadBalancer starter] to your project.
257+
258+
[[multiple-resttemplate-builder-beans]]
259+
=== Multiple `RestTemplateBuilder` beans
260+
261+
If you want a `RestTemplateBuilder` that is not load-balanced, create a `RestTemplateBuilder` bean and inject it.
262+
To access the load-balanced `RestTemplateBuilder`, use the `@LoadBalanced` qualifier when you create your `@Bean`, as the following example shows:
263+
264+
[source,java,indent=0]
265+
----
266+
@Configuration
267+
public class MyConfiguration {
268+
269+
@LoadBalanced
270+
@Bean
271+
RestTemplateBuilder loadBalancedRestTemplateBuilder() {
272+
return new RestTemplateBuilder();
273+
}
274+
275+
@Primary
276+
@Bean
277+
RestTemplateBuilder restTemplateBuilder() {
278+
return new RestTemplateBuilder();
279+
}
280+
}
281+
282+
public class MyClass {
283+
284+
@Autowired
285+
private RestTemplateBuilder restTemplateBuilder;
286+
287+
@Autowired
288+
@LoadBalanced
289+
private RestTemplateBuilder loadBalanced;
290+
291+
public String doOtherStuff() {
292+
return loadBalanced.getForObject("http://stores/stores", String.class);
293+
}
294+
295+
public String doStuff() {
296+
return restTemplateBuilder.build().getForObject("http://example.com", String.class);
297+
}
298+
}
299+
----
300+
301+
IMPORTANT: Notice the use of the `@Primary` annotation on the plain `RestTemplateBuilder` declaration in the preceding example to disambiguate the unqualified `@Autowired` injection.
302+
303+
221304
[[rest-client-loadbalancer-client]]
222305
== Spring RestClient as a LoadBalancer Client
223306

@@ -256,7 +339,7 @@ IMPORTANT: To use it, add xref:spring-cloud-commons/loadbalancer.adoc#spring-clo
256339
=== Multiple `RestClient.Builder` Objects
257340

258341
If you want a `RestClient.Builder` that is not load-balanced, create a `RestClient.Builder` bean and inject it.
259-
To access the load-balanced `RestClient`, use the `@LoadBalanced` qualifier when you create your `@Bean`, as the following example shows:
342+
To access the load-balanced `RestClient.Builder`, use the `@LoadBalanced` qualifier when you create your `@Bean`, as the following example shows:
260343

261344
[source,java,indent=0]
262345
----
@@ -296,7 +379,7 @@ public class MyClass {
296379
}
297380
----
298381

299-
IMPORTANT: Notice the use of the `@Primary` annotation on the plain `RestTemplate` declaration in the preceding example to disambiguate the unqualified `@Autowired` injection.
382+
IMPORTANT: Notice the use of the `@Primary` annotation on the plain `RestClient.Builder` declaration in the preceding example to disambiguate the unqualified `@Autowired` injection.
300383

301384
[[webclinet-loadbalancer-client]]
302385
== Spring WebClient as a LoadBalancer Client

spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRestClientIntegrationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class LoadBalancedRestClientIntegrationTests {
4141

4242
private final RestClient.Builder restClientBuilder;
4343

44-
4544
public LoadBalancedRestClientIntegrationTests(@Autowired RestClient.Builder restClientBuilder) {
4645
this.restClientBuilder = restClientBuilder;
4746
}

spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRestTemplateBuilderIntegrationTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ void shouldBuildLoadBalancedRestTemplate() {
5050
RestTemplate restTemplate = restTemplateBuilder.build();
5151

5252
assertThat(restTemplate.getInterceptors()).hasSize(1);
53-
assertThat(restTemplate.getInterceptors()
54-
.get(0)).isInstanceOf(DeferringLoadBalancerInterceptor.class);
55-
assertThat(((DeferringLoadBalancerInterceptor) restTemplate.getInterceptors()
56-
.get(0))
57-
.getLoadBalancerInterceptorProvider()
58-
.getObject()).isInstanceOf(BlockingLoadBalancerInterceptor.class);
53+
assertThat(restTemplate.getInterceptors().get(0)).isInstanceOf(DeferringLoadBalancerInterceptor.class);
54+
assertThat(((DeferringLoadBalancerInterceptor) restTemplate.getInterceptors().get(0))
55+
.getLoadBalancerInterceptorProvider()
56+
.getObject()).isInstanceOf(BlockingLoadBalancerInterceptor.class);
5957
}
6058

6159
@SpringBootConfiguration
@@ -100,5 +98,4 @@ public <T> ServiceInstance choose(String serviceId, Request<T> request) {
10098

10199
}
102100

103-
}
104-
101+
}

0 commit comments

Comments
 (0)