Ribbon 简介
- 分布式系统中,各个微服务会部署多个实例,如何将服务消费者均匀分摊到多个服务提供者实例上,就要使用到负载均衡器。Ribbon 是负载均衡器,它提供了很多负载均衡算法,例如轮询、随即等,在配置服务提供者地址后,可以将服务消费者请求均匀的分发
Ribbon
快速入门-服务消费者整合 Ribbon
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ribbobn</artifactId>
</dependency>
- 给 RestTemplate 添加 @LoadBalaced 注解,就可整合 RestTemplate 和 Ribbon
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
- 修改Controller,请求地址修改为 http://film-user/user/ ,当 Ribbon 和 Eureka 配合使用时,会自动将虚拟主机名映射成微服务的网络地址,并注入了 LoadBalancerClient 输出当前选择的微服务节点
@RestController
public class MovieController {
private final Logger log = LoggerFactory.getLogger(MovieController.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/user/{id}")
public UserEntity findById(@PathVariable int id){
return this.restTemplate.getForObject("http://film-user/"+id,UserEntity.class);
}
@GetMapping("/log-instance")
public void logInstance(){
ServiceInstance serviceInstance = this.loadBalancerClient.choose("film-user");
log.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
}
}
- 测试:
{"id":1,"username":"account1","name":"张三","age":20,"balance":100.00}
2017-12-17 20:47:53.975 INFO 12313 --- [nio-8010-exec-2] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 20:47:54.215 INFO 12313 --- [nio-8010-exec-1] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 20:47:54.445 INFO 12313 --- [nio-8010-exec-3] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 20:47:54.690 INFO 12313 --- [nio-8010-exec-4] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 20:47:54.935 INFO 12313 --- [nio-8010-exec-5] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
- 注意:不能将 restTemplate.getForObject(...) 与 loadBalancerClient.choose(...) 写在同一方法中,因为 rest-Template 实际上就是一个 Ribbon 客户端,本身已经包含“choose”行为
配置 Ribbon
- Ribbon 的配置是细粒度的,不同的客户端可以使用不同的配置
代码方式
/**
* 该类为配置类
* 不应该被ComponentScan扫描
*/
@Configuration
public class RibbonConfiguration {
@Bean
public IRule ribbonRule(){
//配置负载均衡的规则,更改为随机
return new RandomRule();
}
}
- 使用 @RibbonClient 或 @RibbonClients 注解为服务提供者指定配置类
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "flim-user",configuration = RibbonConfiguration.class)
public class FlimConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(FlimConsumerApplication.class, args);
}
}
2017-12-17 21:08:52.769 INFO 12524 --- [nio-8010-exec-7] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:52.946 INFO 12524 --- [nio-8010-exec-8] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:53.138 INFO 12524 --- [nio-8010-exec-9] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
2017-12-17 21:08:53.319 INFO 12524 --- [io-8010-exec-10] com.linyuan.controller.MovieController : flim-user:linyuandembp:8764
2017-12-17 21:08:53.511 INFO 12524 --- [nio-8010-exec-1] com.linyuan.controller.MovieController : flim-user:linyuandembp:8763
- 注意:RibbonConfiguration 类不能被 @ComponentScan 扫描到,否则配置信息就会被所有 @RibbonClient 共享,因此如果只想自定义某个 Ribbon 客户端的配置,必须防止被 @ComponentScan 扫描
配置文件方式
- 通过配置文件方式配置 Ribbon 更加方便,配置的前缀是 <clientName>.ribbon.
-
NFLoadBalancerClassName:配置 ILoadBalancer 的实现类
-
NFLoadBalancerRuleClassName:配置 IRule 的实现类
-
NFLoadBalancerPingClassName:配置 IPing 实现类
-
NIWSServerListClassName:配置 ServerList 的实现类
-
NIWSServerListFilterClassName:配置 ServerListFilter 的实现类
flim-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
ribbon:
ConnectionTimeout: #连接超时时间
ReadTimeout: #读取超时时间
OkToRetryOnAllOperatotions: #对所有操作请求都进行重试
MaxAutoRetriesNextServer: #切换服务器实例的重试次数
MaxAutoRetries: #对当前实例的重试次数
ServerListRefreshInterval: #刷新服务列表源的间隔时间
网友评论