Home Java javaTutorial Detailed explanation of the Ribbon integrated by RestTemplate of springcloud components

Detailed explanation of the Ribbon integrated by RestTemplate of springcloud components

Aug 02, 2018 pm 02:28 PM
java spring

This article talks about how springcloud integrates ribbon. Different springcloud components (feign, zuul, RestTemplate) integrate ribbon differently. This article first takes a look at RestTemplate.

The class diagram of RestTemplate is as follows

Detailed explanation of the Ribbon integrated by RestTemplate of springcloud components

  • ##HttpAccessor is mainly created based on ClientHttpRequestFactory ##ClientHttpRequest

  • ##InterceptingHttpAccessor
  • extends

    HttpAccessor to create an intercepted InterceptingClientHttpRequest, where the interception will be set Device ClientHttpRequestInterceptor, which is the core of the integrated ribbon. When RestTemplate initiates an http request call, it will first go through the interceptor and then actually initiate the http request.

    Interceptor
  • ClientHttpRequestInterceptor
How is it set up? In the

LoadBalancerAutoConfiguration class, there is the following code:

@LoadBalanced
@Autowired(required = false)
private List<RestTemplate> restTemplates = Collections.emptyList();
Copy after login
As long as the annotation @LoadBalanced

is added, the

RestTemplate will be injected. If spring retry is not introduced, When loading the component, load the following configuration:

@Configuration
@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
static class LoadBalancerInterceptorConfig {
    @Bean
    public LoadBalancerInterceptor ribbonInterceptor(
        LoadBalancerClient loadBalancerClient,
        LoadBalancerRequestFactory requestFactory) {
        return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public RestTemplateCustomizer restTemplateCustomizer(
        final LoadBalancerInterceptor loadBalancerInterceptor) {
        return new RestTemplateCustomizer() {
            @Override
            public void customize(RestTemplate restTemplate) {
                List<ClientHttpRequestInterceptor> list = new ArrayList<>(
                    restTemplate.getInterceptors());
                list.add(loadBalancerInterceptor);
                restTemplate.setInterceptors(list);
            }
        };
    }
}
Copy after login
In this way, RestTemplate

is set to LoadBalancerInterceptor. Let’s take a look at the entire calling process

The whole process is a bit complicated. The core is to initiate a load balancing call through the interceptor LoadBalancerInterceptor and RibbonLoadBalancerClient. RibbonLoadBalancerClientI combines LoadBalancer, so it has the ability to load balance, which is the ribbon principle we explained in the previous article. Detailed explanation of the Ribbon integrated by RestTemplate of springcloud components

We did not draw the actual process of initiating an http request in the figure. The default is created by

SimpleClientHttpRequestFactory

. The class diagram of

ClientHttpRequestFactory is as follows:

##We can see from the calling sequence diagram that at first we called Detailed explanation of the Ribbon integrated by RestTemplate of springcloud componentsInterceptingClientHttpRequestFactory to obtain

InterceptingClientHttpRequest

, which are combined The method integrates ClientHttpRequestFactory and the interceptor. When InterceptingClientHttpRequest initiates the call, it delegates its internal class InterceptingRequestExecution to handle it. The core logic is: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>@Override public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException { if (this.iterator.hasNext()) { ClientHttpRequestInterceptor nextInterceptor = this.iterator.next(); return nextInterceptor.intercept(request, body, this); }else { ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), request.getMethod()); for (Map.Entry&lt;String, List&lt;String&gt;&gt; entry : request.getHeaders().entrySet()) { List&lt;String&gt; values = entry.getValue(); for (String value : values) { delegate.getHeaders().add(entry.getKey(), value); } } if (body.length &gt; 0) { StreamUtils.copy(body, delegate.getBody()); } return delegate.execute(); } }</pre><div class="contentsignin">Copy after login</div></div> First, the first execution of the interceptor collection will be taken out. When the execution of the interceptor is completed, it will be called back, execute the else code, and actually initiate the http request. There are two main ways to implement the ClientHttpRequestFactory interface:

One is

SimpleClientHttpRequestFactory
    , which uses the method provided by J2SE (the method provided by the java.net package) to create the underlying Http request connection
  • One way is to use the

    HttpComponentsClientHttpRequestFactory
  • method. The bottom layer uses HttpClient to access the remote Http service. You can use HttpClient to configure the connection pool, certificate and other information.
  • RestTemplate defaults to using SimpleClientHttpRequestFactory, which internally calls HttpConnection of jdk. The default timeout is -1. You can set the timeout like this:

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory  = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(1000 * 2);//连接超时时间
        factory.setReadTimeout(1000 * 1);//读超时时间
        return new RestTemplate(factory);
    }
    Copy after login
  • Use
HttpComponentsClientHttpRequestFactory

You can use a connection pool (recommended), and you can also set a retry strategy (not studied specifically)

If you want to enable the retry mechanism, we can introduce spring's retry component<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&lt;dependency&gt; &lt;groupId&gt;org.springframework.retry&lt;/groupId&gt; &lt;artifactId&gt;spring-retry&lt;/artifactId&gt; &lt;version&gt;版本号&lt;/version&gt; &lt;/dependency&gt;</pre><div class="contentsignin">Copy after login</div></div> In this way, springcloud-ribbon will add the following configuration:

@Configuration
@ConditionalOnClass(RetryTemplate.class)
public static class RetryAutoConfiguration {
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate template =  new RetryTemplate();
        template.setThrowLastExceptionOnExhausted(true);
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public LoadBalancedRetryPolicyFactory loadBalancedRetryPolicyFactory() {
        return new LoadBalancedRetryPolicyFactory.NeverRetryFactory();
    }
}

@Configuration
@ConditionalOnClass(RetryTemplate.class)
public static class RetryInterceptorAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public RetryLoadBalancerInterceptor ribbonInterceptor(
        LoadBalancerClient loadBalancerClient, LoadBalancerRetryProperties properties,
        LoadBalancedRetryPolicyFactory lbRetryPolicyFactory,
        LoadBalancerRequestFactory requestFactory) {
        return new RetryLoadBalancerInterceptor(loadBalancerClient, properties,
                                                lbRetryPolicyFactory, requestFactory);
    }

    @Bean
    @ConditionalOnMissingBean
    public RestTemplateCustomizer restTemplateCustomizer(
        final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
        return new RestTemplateCustomizer() {
            @Override
            public void customize(RestTemplate restTemplate) {
                List<ClientHttpRequestInterceptor> list = new ArrayList<>(
                    restTemplate.getInterceptors());
                list.add(loadBalancerInterceptor);
                restTemplate.setInterceptors(list);
            }
        };
    }
}
Copy after login
@Bean
@ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate")
@ConditionalOnMissingBean
    public LoadBalancedRetryPolicyFactory loadBalancedRetryPolicyFactory(SpringClientFactory clientFactory) {
    return new RibbonLoadBalancedRetryPolicyFactory(clientFactory);
}
Copy after login

The interceptor is replaced with

RetryLoadBalancerInterceptor

, where the retry component retryTemplate is integrated. The retry strategy is configured by the

RetryHandler

interface. The default implementation class is DefaultLoadBalancerRetryHandler. The following are the default configuration parameters <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>#最大的重试次数 ribbon.MaxAutoRetries=0 #最大重试server的个数 ribbon.MaxAutoRetriesNextServer=1 #是否开启任何异常都重试(默认在get请求下会重试,其他情况不会重试,除非设置为true) ribbon.OkToRetryOnAllOperations=false #指定重试的http状态码 ribbon.retryableStatusCodes=500,501</pre><div class="contentsignin">Copy after login</div></div>. The above is globally effective. If ## is added #xxx.ribbon.MaxAutoRetries=1<code>This will only take effect on a certain ribbon client. MaxAutoRetries and MaxAutoRetriesNextServer are used together. The maximum number of retries is for each server. If MaxAutoRetries=1 and MaxAutoRetriesNextServer=1 are set, the maximum number of retries triggered is 4 times.

Related articles:

Java Example - Array to Collection


The above is the detailed content of Detailed explanation of the Ribbon integrated by RestTemplate of springcloud components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles