Spring Cloud Ribbon实现客户端负载均衡

Posted Miss_wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Ribbon实现客户端负载均衡相关的知识,希望对你有一定的参考价值。

1、构建microservice-consumer-movie-ribbon项目,在pom.xml中引入ribbon依赖

  在引入Eureka依赖的时候,默认里面含有ribbon依赖

2、添加@LoadBalanced注解,实现负载均衡

  ribbon负载均衡策略默认为轮循方式

@SpringBootApplication
@EnableEurekaClientpublic class ConsumeMovieRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumeMovieRibbonApplication.class, args);
    }
    @Bean
    @LoadBalanced  //客户端负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

3、自定义负载均衡策略

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
/**
 * @Configuration注解不能放在@SpringBootApplication所在的包下
* 如果放在此包下,默认全部负载均衡使用此策略 *
*/ @Configuration @ExcludeFromComponentScan public class TestConfiguration { /** * 设置负载均衡的规则为随机 * */ @Bean public IRule ribbonRule() { return new RandomRule(); } }

4、指定对那个客户端使用自定义负载均衡

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "microservice-provider-user", configuration = TestConfiguration.class)
public class ConsumeMovieRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumeMovieRibbonApplication.class, args);
    }
    @Bean
    @LoadBalanced  //客户端负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

5、如果@Configuration注解放在@SpringBootApplication所在的包下

  a、在@Configuration包下创建ExcludeFromComponentScan注解

package com.wyl.microservicesimpleconsumemovie;
public @interface ExcludeFromComponentScan {
}

  b、在入口类中排除此注解不扫描

package com.wyl.microservicesimpleconsumemovie;

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

  c、在TestConfiguration中使用此注解

/**
 * @Configuration注解不能放在@SpringBootApplication所在的包及其子包下
 * */
@Configuration
@ExcludeFromComponentScan
public class TestConfiguration {
    /**
     * 设置负载均衡的规则为随机
     * */
    @Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }
}

6、开启多个microservice-provider-user微服务,测试结果

    

负载均衡的结果:

111:microservice-provider-user:192.168.1.39:7900
222:microservice-provider-user2:192.168.1.39:7903
111:microservice-provider-user:192.168.1.39:7900
222:microservice-provider-user2:192.168.1.39:7902
111:microservice-provider-user:192.168.1.39:7901
222:microservice-provider-user2:192.168.1.39:7903
111:microservice-provider-user:192.168.1.39:7900
222:microservice-provider-user2:192.168.1.39:7902
111:microservice-provider-user:192.168.1.39:7901
222:microservice-provider-user2:192.168.1.39:7903
111:microservice-provider-user:192.168.1.39:7901
222:microservice-provider-user2:192.168.1.39:7902
111:microservice-provider-user:192.168.1.39:7900
222:microservice-provider-user2:192.168.1.39:7903
111:microservice-provider-user:192.168.1.39:7900
222:microservice-provider-user2:192.168.1.39:7902
microservice-provider-user采用自定义的随机负载均衡,而microservice-provider-user2采用ribbon默认的轮循方式
6、完整代码参见https://i.cnblogs.com/Files.aspx中microservice-spring-cloud-ribbon文件

以上是关于Spring Cloud Ribbon实现客户端负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

笔记:Spring Cloud Ribbon 客户端配置详解

笔记:Spring Cloud Feign Ribbon 配置

Spring Cloud Ribbon

Spring Cloud(十四):Ribbon实现客户端负载均衡及其实现原理介绍

spring cloud ribbon 负载均衡类1

Spring Cloud Ribbon原理解析