当前位置:网站首页>@loadbalance annotation of resttemplate

@loadbalance annotation of resttemplate

2022-06-12 21:21:00 GodSure0914

In the use of springcloud ribbon When the client load is balanced , You can give RestTemplate bean Add one more @LoadBalanced annotation , You can make this RestTemplate The ability to load balance the client on request :

   /** *  Injection is used to call the third-party interface RestTemplate * @return RestTemplate */
    @Bean
    @LoadBalanced
    public RestTemplate loadBalancedRestTemplate() {
    
        // Pass... For the time being build obtain  new Coming out sMessageConverters Lack of parameter 
        RestTemplate buildTemplate = restTemplateBuilder.build();
        buildTemplate.setRequestFactory(httpRequestFactory());
        return buildTemplate;
    }

1 If a service is clustered, it may not be able to find a service for me to run without this annotation. Therefore, this annotation must be added to make my service request have the ability of client load balancing , So that we don't make a mistake
2. Here I want to state that the default load balancing mechanism is that polling is a round robin service
3. First : This @LoadBalanced Annotations are from cloud A comment under the package
4. This annotation is to make something have the ability of load balancing

Through the source code of annotations, it is found that there are only more than ordinary annotations @Qualifier:
 Insert picture description here
This annotation is used for precise injection by name .
When spring There are multiple of the same type in the container bean When , Can pass @Qualifier To distinguish , So that when you inject, you can clearly indicate which specific... You want to inject bean, disambiguate .

原网站

版权声明
本文为[GodSure0914]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122119053522.html