Ribbon详解与实例

偏执的太偏执、 2022-01-22 23:39 293阅读 0赞

Ribbon是一个为客户端提供负载均衡功能的服务,它内部提供了一个叫做ILoadBalance的接口代表负载均衡器的操作,比如有添加服务器操作、选择服务器操作、获取所有的服务器列表、获取可用的服务器列表等等。

需要解决的问题:

  • ① 如何在配置Eureka Client注册中心时不去硬编码Eureka Server的地址?
  • ② 在微服务不同模块间进行通信时,如何不去硬编码服务提供者的地址?
  • ③ 当部署多个相同微服务时,如何实现请求时的负载均衡?

Ribbon是什么?

  1. RibbonNetflix发布的云中间层服务开源项目,其主要功能是提供客户端实现负载均衡算法。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,Ribbon是一个客户端负载均衡器,我们可以在配置文件中Load Balancer后面的所有机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。

下图展示了Eureka使用Ribbon时的大致架构:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ppblhZYW4_size_16_color_FFFFFF_t_70

SpringCloud之Ribbon入门案例

① 首先引入Ribbon依赖,Ribbon的使用依赖Eureka:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-ribbon</artifactId>
  8. </dependency>

② 如何使用Ribbon

  1. 使用RestTemplate进行Eureka Client(包括服务提供者以及服务消费者,在这里其实是服务消费者使用RestTemplate)之间的通信,为RestTemplate配置类添加@LoadBalanced注解即可,如下所示:
  2. @Bean
  3. @LoadBalanced
  4. public RestTemplate restTemplate() {
  5. return new RestTemplate();
  6. }

主程序:

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. //在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
  4. @RibbonClient(name="MICROSERVICECLOUD-DEPT")
  5. public class DeptConsumer80_App{
  6. public static void main(String[] args){
  7. SpringApplication.run(DeptConsumer80_App.class, args);
  8. }
  9. }

③ 如何解决硬编码

  1. 使用添加@LoadBalanced注解后的RestTemplate调用服务提供者的接口时,可以使用虚拟IP替代真实IP地址。所谓的虚拟IP就是服务提供者在application.propertiesyml文件中配置的spring.application.name属性的值。示例如下:

以前:

  1. @RestController
  2. public class DeptController_Consumer
  3. {
  4. private static final String REST_URL_PREFIX = "http://localhost:8001"; //需要ip+端口
  5. @Autowired
  6. private RestTemplate restTemplate;
  7. @RequestMapping(value = "/consumer/dept/add")
  8. public boolean add(Dept dept)
  9. {
  10. return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
  11. }
  12. }

使用Ribbon后:

  1. @RestController
  2. public class DeptController_Consumer
  3. {
  4. private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT"; //微服务的虚拟id
  5. @Autowired
  6. private RestTemplate restTemplate;
  7. @RequestMapping(value = "/consumer/dept/add")
  8. public boolean add(Dept dept)
  9. {
  10. return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
  11. }
  12. }

小总结:Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心ip地址和端口号

微服务(服务提供者)集群搭建:

  1. 机器1
  2. server:
  3. port: 8001
  4. spring:
  5. application:
  6. name: microservicecloud-dept
  7. datasource:
  8. type: com.alibaba.druid.pool.DruidDataSource
  9. driver-class-name: org.gjt.mm.mysql.Driver
  10. url: jdbc:mysql://localhost:3306/cloudDB01
  11. username: root
  12. password: 123456
  13. 机器2
  14. server:
  15. port: 8002
  16. spring:
  17. application:
  18. name: microservicecloud-dept
  19. datasource:
  20. type: com.alibaba.druid.pool.DruidDataSource
  21. driver-class-name: org.gjt.mm.mysql.Driver
  22. url: jdbc:mysql://localhost:3306/cloudDB02
  23. username: root
  24. password: 123456
  25. 机器3
  26. server:
  27. port: 8003
  28. spring:
  29. application:
  30. name: microservicecloud-dept
  31. datasource:
  32. type: com.alibaba.druid.pool.DruidDataSource
  33. driver-class-name: org.gjt.mm.mysql.Driver
  34. url: jdbc:mysql://localhost:3306/cloudDB03
  35. username: root
  36. password: 123456

其中{Spring.application.name}都是一样的,不可以变。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ppblhZYW4_size_16_color_FFFFFF_t_70 1

Ribbon组件IRule

默认的是RoundBobinRule(轮询)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ppblhZYW4_size_16_color_FFFFFF_t_70 2

RetryRule

  • 1、先按照RoundRobinRule(轮询)的策略获取服务,如果获取的服务失败侧在指定的时间会进行重试,进行获取可用的服务
  • 2、如多次获取某个服务失败,这不会再再次获取该服务如(高德地图上某条道路堵车,司机不会走那条道路)=

使用:

  1. @Configuration
  2. public class ConfigBean //boot -->spring applicationContext.xml --- @Configuration配置 ConfigBean = applicationContext.xml
  3. {
  4. @Bean
  5. @LoadBalanced
  6. public RestTemplate getRestTemplate(){
  7. return new RestTemplate();
  8. }
  9. @Bean
  10. public IRule myRule(){
  11. //return new RoundRobinRule();
  12. //return new RandomRule();//达到的目的,用我们重新选择的随机算法替代默认的轮询。
  13. return new RetryRule(); //在这里选择负载均衡算法
  14. }
  15. }

自定义负载均衡算法:

  1. 所谓的自定义Ribbon Client的主要作用就是使用自定义配置替代Ribbon默认的负载均衡策略,注意:自定义的Ribbon Client是有针对性的,一般一个自定义的Ribbon Client是对一个服务提供者(包括服务名相同的一系列副本)而言的。自定义了一个Ribbon Client 它所设定的负载均衡策略只对某一特定服务名的服务提供者有效,但不能影响服务消费者与别的服务提供者通信所使用的策略。根据官方文档的意思,推荐在 springboot 主程序扫描的**包范围之外**进行自定义配置类。其实纯代码自定义RibbonClient的话有两种方式:

方式一:在springboot主程序扫描的包外定义配置类,然后为springboot主程序添加 @RibbonClient 注解引入配置类

配置类不应该在SpringBoot的包路径下通过@RibbonClient 注解加载:

  1. @Configuration
  2. public class MySelfRule
  3. {
  4. @Bean
  5. public IRule myRule()
  6. {
  7. return new RandomRule_ZY(); // 我自定义为每台机器5次,5次之后在轮询到下一个
  8. }
  9. }

springboot主程序:

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. //在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
  4. @RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
  5. public class DeptConsumer80_App
  6. {
  7. public static void main(String[] args)
  8. {
  9. SpringApplication.run(DeptConsumer80_App.class, args);
  10. }
  11. }

自定义LoadBalance:

  1. public class RandomRule_ZY extends AbstractLoadBalancerRule{
  2. // total = 0 // 当total==5以后,我们指针才能往下走,
  3. // index = 0 // 当前对外提供服务的服务器地址,
  4. // total需要重新置为零,但是已经达到过一个5次,我们的index = 1
  5. // 分析:我们5次,但是微服务只有8001 8002 8003 三台,OK?
  6. private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
  7. private int currentIndex = 0; // 当前提供服务的机器号
  8. public Server choose(ILoadBalancer lb, Object key){
  9. if (lb == null) {
  10. return null;
  11. }
  12. Server server = null;
  13. while (server == null) {
  14. if (Thread.interrupted()) {
  15. return null;
  16. }
  17. List<Server> upList = lb.getReachableServers(); //激活可用的服务
  18. List<Server> allList = lb.getAllServers(); //所有的服务
  19. int serverCount = allList.size();
  20. if (serverCount == 0) {
  21. return null;
  22. }
  23. if(total < 5){
  24. server = upList.get(currentIndex);
  25. total++;
  26. }else {
  27. total = 0;
  28. currentIndex++;
  29. if(currentIndex >= upList.size()){
  30. currentIndex = 0;
  31. }
  32. }
  33. if (server == null) {
  34. Thread.yield();
  35. continue;
  36. }
  37. if (server.isAlive()) {
  38. return (server);
  39. }
  40. // Shouldn't actually happen.. but must be transient or a bug.
  41. server = null;
  42. Thread.yield();
  43. }
  44. return server;
  45. }
  46. @Override
  47. public Server choose(Object key){
  48. return choose(getLoadBalancer(), key);
  49. }
  50. @Override
  51. public void initWithNiwsConfig(IClientConfig clientConfig){
  52. }
  53. }

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Zuul详解实例

    前言        介绍完分布式配置中心,结合前面的文章。我们已经有了一个微服务的框架了,可以对外提供api接口服务了。但现在试想一下,在微服务框架中,每个对外服务都是独

    相关 Ribbon详解实例

            Ribbon是一个为客户端提供负载均衡功能的服务,它内部提供了一个叫做ILoadBalance的接口代表负载均衡器的操作,比如有添加服务器操作、选择服务器操作、