开发环境
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微服务架构开发实战
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。