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中加入如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-consumer-movie-ribbon</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 整合ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
启动类:MovieRibbonApplication.java。使用@LoadBalanced注解,为RestTemplate开启负载均衡的能力。
@SpringBootApplication
@EnableDiscoveryClient
public class MovieRibbonApplication {
/**
* 实例化RestTemplate,通过@LoadBalanced注解开启均衡负载能力.
* @return restTemplate
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MovieRibbonApplication.class, args);
}
}
实体类:User.java
public class User {
private Long id;
private String username;
private Integer age;
...
// getters and setters
}
Ribbon的测试类:RibbonService.java
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public User findById(Long id) {
// http://服务提供者的serviceId/url
return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
}
}
controller:RibbonController.java
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@GetMapping("/ribbon/{id}")
public User findById(@PathVariable Long id) {
return this.ribbonService.findById(id);
}
}
application.yml
server:
port: 8010
spring:
application:
name: microservice-consumer-movie-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://discovery:8761/eureka/
instance:
preferIpAddress: true
启动后,访问多次http://localhost:8010/ribbon/1,返回结果:
{
"id": 1,
"username": "Tom",
"age": 12
}
然后打开两个microservice-provider-user实例的控制台,发现两个实例都输出了类似如下的日志内容:
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=?
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]
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]
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]
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