SpringCloud定制Ribbon客户端负载均衡策略

悠悠 2022-05-23 00:40 350阅读 0赞
  • springColud目录

前言


  • 在项目中,我们部署一个微服务的时候往往是集群的形式部署的,这样既能提高并发量,又能保证系统的健壮性。相对的对于这个集群我们需要采取一定的策略保证负载均衡。再说一句,ribbon的工作原理是从注册中心获取集群的地址列表,再按一定策略选取一个微服务的地址进行链接。这就是客户端发现模式。

Ribbon在Eureka中的使用


添加依赖

  • 在Eureka下使用不需要添加Ribbon的依赖,因为spring-cloud-starter-eureka中会自动引入Ribbon的依赖包。

ribbon的使用有两种方式,使用注解方式与配置文件方式,下面依次介绍。

注解方式


  1. 主入口程序

    @SpringBootApplication
    @EnableEurekaClient
    @RibbonClient(name = “microservice-provider”, configuration = TestConfiguration.class)
    @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) })
    public class MicroserviceConsumerApplication {

  1. @Bean
  2. @LoadBalanced
  3. public RestTemplate restTemplate(){
  4. return new RestTemplate();
  5. }
  6. public static void main(String[] args) {
  7. SpringApplication.run(MicroserviceConsumerApplication.class, args);
  8. }
  9. }
  1. TestConfiguration文件

    @Configuration
    @ExcludeFromComponentScan
    public class TestConfiguration {
    // @Autowired
    // IClientConfig config;

    @Bean
    public IRule ribbonRule() {

    1. return new RandomRule();

    }
    }

  2. ExcludeFromComponentScan 文件

    public @interface ExcludeFromComponentScan {

    }

说明:

  1. @LoadBalanced 指定了restTemplate使用Ribbon进行负载均衡。
  2. @RibbonClient 说明对名为microservice-provider的微服务集群采用的负载均衡策略由TestConfiguration.class文件设置。
  3. TestConfiguration 中函数返回的即是负载均衡的策略,RandomRule代表随机。
  4. 如果TestConfiguration 放在主程序的文件目录及子目录下时,spring扫描后会将该策略设置为所有的负载均衡策略而不是单单指对microservice-provider的负载均衡策略。所以要么将该文件放置在入口程序的文件目录之外或者禁止spring扫描该文件。禁止扫描的做法如上,在主程序上添加@ComponentScan注解,则spring不会扫描带@ExcludeFromComponentScan注解的文件。
  5. ribbon默认的负载均衡策略为轮询。
  6. 如果需要配置多个,则使用@RibbonClients注解,如下

基于配置文件的方式


  1. # 微服务名.ribbon.NFLoadBalancerRuleClassName:负载均衡策略
  2. microservice-provider:
  3. ribbon:
  4. NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
  5. @RibbonClients(value = {
  6. @RibbonClient(name = "xxx",configuration = xxxConfiguration.class),
  7. @RibbonClient(name = "microservice-provider",configuration = TestConfiguration.class)
  8. })

使用实例:

  1. @RestController
  2. public class MovieController {
  3. @Autowired
  4. private LoadBalancerClient loadBalancerClient;
  5. //用于访问其他微服务的实体。
  6. @Autowired
  7. private RestTemplate restTemplate;
  8. @GetMapping("/movie/{id}")
  9. public User findById(@PathVariable Long id) {
  10. //通过ribbon负载均衡策略,发送一个HTTP请求给一个微服务,并将返回的数据封装到user.class实体中
  11. return this.restTemplate.getForObject("http://microservice-provider/simple/" + id, User.class);
  12. }
  13. @GetMapping("/test")
  14. public String test() {
  15. //根据负载均衡策略获取到一个微服务的信息。
  16. ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider");
  17. System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
  18. return "1";
  19. }
  20. }

独立使用ribbon


1.有引入eureka的时候

  • 关闭eureka
  • 指定listOfServers

    ribbon:
    eureka:
    enabled: false
    microservice-provider:
    ribbon:

    1. listOfServers: localhost:7900

2.没有引入eureka的时候

  • 添加ribbon依赖

    1. <groupId>org.springframework.cloud</groupId>
    2. <artifactId>spring-cloud-starter-ribbon</artifactId>
    3. /dependency>
  • 指定listOfServers

    microservice-provider:
    ribbon:

    1. listOfServers: localhost:7900

发表评论

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

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

相关阅读