Ribbon:提供客户端侧负载均衡算法,默认轮询(Feign中自带ribbon,不需导入依赖)
工作流程:
1、选择Eureka Server(优先选择在同一个Zone且负载较少的Server)
2、根据用户指定的策略(Ribbon提供),从Server取到的服务注册列表中选择一个地址
region:可以简单理解为地理上的分区
zone:可以简单理解为region内的具体机房
@LoadBalanced注解:在主程序中的RestTemplate添加注解即可实现负载均衡
eg:
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
控制器controller:
return this.restTemplate.getForObject("http://生产者yml的applicationname+接口名", 接受的类型.class);
配置config类:(config类不能在@ComponentScan和@SpringBootApplication扫描目录中)
开始类中:
@RibbonClient(value = "指定微服务名",configuration = 指定配置文件.class)
config类:
@Configuration
public class RibbonConfi