首页 > 代码库 > Spring Cloud Ribbon实现客户端负载均衡

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=http://www.mamicode.com/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实现客户端负载均衡