@ -36,7 +36,26 @@ on the classpath to cause the Spring Boot application to register with the servi
@@ -36,7 +36,26 @@ on the classpath to cause the Spring Boot application to register with the servi
}</pre><p>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. See
for details of how the <codeclass="literal">RestTemplate</code> is set up.</p><divclass="section"><divclass="titlepage"><div><div><h3class="title"><aname="_retrying_failed_requests"href="#_retrying_failed_requests"></a>2.3.1 Retrying Failed Requests</h3></div></div></div><p>A load balanced <codeclass="literal">RestTemplate</code> can be configured to retry failed requests.
for details of how the <codeclass="literal">RestTemplate</code> is set up.</p></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="_spring_webclient_as_a_load_balancer_client"href="#_spring_webclient_as_a_load_balancer_client"></a>2.4 Spring WebClient as a Load Balancer Client</h2></div></div></div><p><codeclass="literal">WebClient</code> can be automatically configured to use the <codeclass="literal">LoadBalancerClient</code>. To create a load balanced <codeclass="literal">WebClient</code> create a <codeclass="literal">WebClient.Builder</code><codeclass="literal">@Bean</code> and use the <codeclass="literal">@LoadBalanced</code> qualifier.</p><preclass="programlisting"><em><spanclass="hl-annotation"style="color: gray">@Configuration</span></em>
}</pre><p>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.</p><divclass="section"><divclass="titlepage"><div><div><h3class="title"><aname="_retrying_failed_requests"href="#_retrying_failed_requests"></a>2.4.1 Retrying Failed Requests</h3></div></div></div><p>A load balanced <codeclass="literal">RestTemplate</code> can be configured to retry failed requests.
By default this logic is disabled, you can enable it by adding <aclass="link"href="https://github.com/spring-projects/spring-retry"target="_top">Spring Retry</a> to your application’s classpath. The load balanced <codeclass="literal">RestTemplate</code> will
honor some of the Ribbon configuration values related to retrying failed requests. If
you would like to disable the retry logic with Spring Retry on the classpath
@ -58,7 +77,7 @@ you would like to use for a given service.</p><pre class="programlisting"><em><s
@@ -58,7 +77,7 @@ you would like to use for a given service.</p><pre class="programlisting"><em><s
};
}
}</pre><divclass="note"style="margin-left: 0.5in; margin-right: 0.5in;"><tableborder="0"summary="Note"><tr><tdrowspan="2"align="center"valign="top"width="25"><imgalt="[Note]"src="images/note.png"></td><thalign="left">Note</th></tr><tr><tdalign="left"valign="top"><p><codeclass="literal">client</code> in the above examples should be replaced with your Ribbon client’s
name.</p></td></tr></table></div></div><divclass="section"><divclass="titlepage"><div><div><h3class="title"><aname="_multiple_resttemplate_objects"href="#_multiple_resttemplate_objects"></a>2.3.2 Multiple RestTemplate objects</h3></div></div></div><p>If you want a <codeclass="literal">RestTemplate</code> that is not load balanced, create a <codeclass="literal">RestTemplate</code>
name.</p></td></tr></table></div></div><divclass="section"><divclass="titlepage"><div><div><h3class="title"><aname="_multiple_resttemplate_objects"href="#_multiple_resttemplate_objects"></a>2.4.2 Multiple RestTemplate objects</h3></div></div></div><p>If you want a <codeclass="literal">RestTemplate</code> that is not load balanced, create a <codeclass="literal">RestTemplate</code>
bean and inject it as normal. To access the load balanced <codeclass="literal">RestTemplate</code> use
the <codeclass="literal">@LoadBalanced</code> qualifier when you create your <codeclass="literal">@Bean</code>.</p><divclass="important"style="margin-left: 0.5in; margin-right: 0.5in;"><tableborder="0"summary="Important"><tr><tdrowspan="2"align="center"valign="top"width="25"><imgalt="[Important]"src="images/important.png"></td><thalign="left">Important</th></tr><tr><tdalign="left"valign="top"><p>Notice the <codeclass="literal">@Primary</code> annotation on the plain <codeclass="literal">RestTemplate</code> declaration in the example below, to disambiguate the unqualified <codeclass="literal">@Autowired</code> injection.</p></td></tr></table></div><preclass="programlisting"><em><spanclass="hl-annotation"style="color: gray">@Configuration</span></em>
@ -91,7 +110,7 @@ the <code class="literal">@LoadBalanced</code> qualifier when you create your <c
@@ -91,7 +110,7 @@ the <code class="literal">@LoadBalanced</code> qualifier when you create your <c
}</pre><divclass="tip"style="margin-left: 0.5in; margin-right: 0.5in;"><tableborder="0"summary="Tip"><tr><tdrowspan="2"align="center"valign="top"width="25"><imgalt="[Tip]"src="images/tip.png"></td><thalign="left">Tip</th></tr><tr><tdalign="left"valign="top"><p>If you see errors like <codeclass="literal">java.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89</code> try injecting <codeclass="literal">RestOperations</code> instead or setting <codeclass="literal">spring.aop.proxyTargetClass=true</code>.</p></td></tr></table></div></div></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="loadbalanced-webclient"href="#loadbalanced-webclient"></a>2.4 Spring WebFlux WebClient as a Load Balancer Client</h2></div></div></div><p><codeclass="literal">WebClient</code> can be configured to use the <codeclass="literal">LoadBalancerClient. A `LoadBalancerExchangeFilterFunction</code> is auto-configured if spring-webflux is on the classpath.</p><preclass="programlisting"><spanxmlns:d="http://docbook.org/ns/docbook"class="hl-keyword">public</span><spanxmlns:d="http://docbook.org/ns/docbook"class="hl-keyword">class</span> MyClass {
}</pre><divclass="tip"style="margin-left: 0.5in; margin-right: 0.5in;"><tableborder="0"summary="Tip"><tr><tdrowspan="2"align="center"valign="top"width="25"><imgalt="[Tip]"src="images/tip.png"></td><thalign="left">Tip</th></tr><tr><tdalign="left"valign="top"><p>If you see errors like <codeclass="literal">java.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89</code> try injecting <codeclass="literal">RestOperations</code> instead or setting <codeclass="literal">spring.aop.proxyTargetClass=true</code>.</p></td></tr></table></div></div></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="loadbalanced-webclient"href="#loadbalanced-webclient"></a>2.5 Spring WebFlux WebClient as a Load Balancer Client</h2></div></div></div><p><codeclass="literal">WebClient</code> can be configured to use the <codeclass="literal">LoadBalancerClient. A `LoadBalancerExchangeFilterFunction</code> is auto-configured if spring-webflux is on the classpath.</p><preclass="programlisting"><spanxmlns:d="http://docbook.org/ns/docbook"class="hl-keyword">public</span><spanxmlns:d="http://docbook.org/ns/docbook"class="hl-keyword">class</span> MyClass {
@ -105,7 +124,7 @@ the <code class="literal">@LoadBalanced</code> qualifier when you create your <c
@@ -105,7 +124,7 @@ the <code class="literal">@LoadBalanced</code> qualifier when you create your <c
}</pre><p>The URI needs to use a virtual host name (ie. service name, not a host name).
The <codeclass="literal">LoadBalancerClient</code> is used to create a full physical address.</p></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="ignore-network-interfaces"href="#ignore-network-interfaces"></a>2.5 Ignore Network Interfaces</h2></div></div></div><p>Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".</p><p><b>application.yml. </b>
The <codeclass="literal">LoadBalancerClient</code> is used to create a full physical address.</p></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="ignore-network-interfaces"href="#ignore-network-interfaces"></a>2.6 Ignore Network Interfaces</h2></div></div></div><p>Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".</p><p><b>application.yml. </b>
</p><preclass="screen">spring:
cloud:
inetutils:
@ -124,7 +143,7 @@ The <code class="literal">LoadBalancerClient</code> is used to create a full phy
@@ -124,7 +143,7 @@ The <code class="literal">LoadBalancerClient</code> is used to create a full phy
cloud:
inetutils:
useOnlySiteLocalInterfaces: true</pre><p>
</p></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="http-clients"href="#http-clients"></a>2.6 HTTP Client Factories</h2></div></div></div><p>Spring Cloud Commons provides beans for creating both Apache HTTP clients (<codeclass="literal">ApacheHttpClientFactory</code>)
</p></div><divclass="section"><divclass="titlepage"><div><div><h2class="title"style="clear: both"><aname="http-clients"href="#http-clients"></a>2.7 HTTP Client Factories</h2></div></div></div><p>Spring Cloud Commons provides beans for creating both Apache HTTP clients (<codeclass="literal">ApacheHttpClientFactory</code>)
as well as OK HTTP clients (<codeclass="literal">OkHttpClientFactory</code>). The <codeclass="literal">OkHttpClientFactory</code> bean will only be created
if the OK HTTP jar is on the classpath. In addition, Spring Cloud Commons provides beans for creating
the connection managers used by both clients, <codeclass="literal">ApacheHttpClientConnectionManagerFactory</code> for the Apache
<title>Spring WebClient as a Load Balancer Client</title>
<simpara><literal>WebClient</literal> can be automatically configured to use the <literal>LoadBalancerClient</literal>. To create a load balanced <literal>WebClient</literal> create a <literal>WebClient.Builder</literal><literal>@Bean</literal> and use the <literal>@LoadBalanced</literal> qualifier.</simpara>