2.3.1 Ribbon

Ribbon介绍

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

Ribbon工作时分为两步:第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略,例如轮询、随机、根据响应时间加权等。

Ribbon代码示例

创建一个Maven项目,并在pom.xml中加入如下内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <artifactId>microservice-consumer-movie-ribbon</artifactId>
  6. <packaging>jar</packaging>
  7. <parent>
  8. <groupId>com.itmuch.cloud</groupId>
  9. <artifactId>spring-cloud-microservice-study</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-eureka</artifactId>
  16. </dependency>
  17. <!-- 整合ribbon -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-ribbon</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-actuator</artifactId>
  25. </dependency>
  26. </dependencies>
  27. </project>

启动类:MovieRibbonApplication.java。使用@LoadBalanced注解,为RestTemplate开启负载均衡的能力。

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class MovieRibbonApplication {
  4. /**
  5. * 实例化RestTemplate,通过@LoadBalanced注解开启均衡负载能力.
  6. * @return restTemplate
  7. */
  8. @Bean
  9. @LoadBalanced
  10. public RestTemplate restTemplate() {
  11. return new RestTemplate();
  12. }
  13. public static void main(String[] args) {
  14. SpringApplication.run(MovieRibbonApplication.class, args);
  15. }
  16. }

实体类:User.java

  1. public class User {
  2. private Long id;
  3. private String username;
  4. private Integer age;
  5. ...
  6. // getters and setters
  7. }

Ribbon的测试类:RibbonService.java

  1. @Service
  2. public class RibbonService {
  3. @Autowired
  4. private RestTemplate restTemplate;
  5. public User findById(Long id) {
  6. // http://服务提供者的serviceId/url
  7. return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
  8. }
  9. }

controller:RibbonController.java

  1. @RestController
  2. public class RibbonController {
  3. @Autowired
  4. private RibbonService ribbonService;
  5. @GetMapping("/ribbon/{id}")
  6. public User findById(@PathVariable Long id) {
  7. return this.ribbonService.findById(id);
  8. }
  9. }

application.yml

  1. server:
  2. port: 8010
  3. spring:
  4. application:
  5. name: microservice-consumer-movie-ribbon
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://discovery:8761/eureka/
  10. instance:
  11. preferIpAddress: true

启动后,访问多次http://localhost:8010/ribbon/1,返回结果:

  1. {
  2. "id": 1,
  3. "username": "Tom",
  4. "age": 12
  5. }

然后打开两个microservice-provider-user实例的控制台,发现两个实例都输出了类似如下的日志内容:

  1. Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.username as username3_0_0_ from user user0_ where user0_.id=?
  2. 2016-09-13 21:38:56.719 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
  3. 2016-09-13 21:38:56.720 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([age2_0_0_] : [INTEGER]) - [12]
  4. 2016-09-13 21:38:56.720 TRACE 17404 --- [nio-8000-exec-1] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username3_0_0_] : [VARCHAR]) - [Tom]
  5. 2016-09-13 21:39:10.588 INFO 17404 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration

至此,我们已经通过Ribbon在客户端侧实现了均衡负载。

代码地址(任选其一):

Ribbon代码地址:

http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-consumer-movie-ribbon
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-consumer-movie-ribbon