Ribbon为RestTemplate增加负载均衡功能
Posted 叮咚_打地鼠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ribbon为RestTemplate增加负载均衡功能相关的知识,希望对你有一定的参考价值。
开发环境
eureka、ribbon
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
// 让restTemplate 具有负载均衡功能
@LoadBalanced
@Bean public RestTemplate restTemplate() {
return new RestTemplate();
}
}
添加@LoadBalanced注解,让restTemplate具有负载均衡的能力
ribbon实现原理可以分为如下4个步骤:
- ribbon首先根据其所在Zone优先选择一个负载均衡比较少的Eureka
- 定期从eureka服务器更新,并过滤服务实例列表
- 根据指定的负载均衡策略,从可用的服务实例列表中选择一个
- 然后使用该地址,通过rest客户端进行服务调用
缺省情况下,使用轮询策略
参考书籍-----------------------spring cloud微服务架构开发实战
以上是关于Ribbon为RestTemplate增加负载均衡功能的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得无熔断器
spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得无熔断器
2.Spring Cloud Alibaba实现负载均衡-Ribbon
spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得