Ribbon简介
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.
—–摘自官网
Ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
目前主流的LB方案分类
目前主流的LB方案可分成两类:
- 一种是集中式LB, 即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责把访问请求通过某种策略转发至服务的提供方;
- 另一种是进程内LB,将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器。Ribbon就属于后者,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址。
Ribbon用以实现负载均衡;实现软负载均衡,核心有三点:
- 服务发现,发现依赖服务的列表
- 服务选择规则,在多个服务中如何选择一个有效服务
- 服务监听,检测失效的服务,高效剔除失效服务
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。
项目准备
在上一篇的介绍图中我们知道,无论是提供服务端还是调用服务端都需要在注册中心注册,Ribbon作为客户端当然也 会在注册中心注册。
上一篇中的两个项目依旧使用
- eureka-server :注册服务
- service-hello:服务端
- service-ribbon:客户端(新建项目)
在打开的service-hello项目窗口,创建项目service-ribbon(IDEA中打开已有项目新建-选择Module而不是Project)
pom
<?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">
<parent>
<artifactId>service-hello</artifactId>
<groupId>comm.bamboo</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../service-hello/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-ribbon</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
yml
server:
port: 8764
spring:
application:
name: service-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
java代码
application
package com.bamboo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
*
* 客户端
*
* service-hello Application
*/
@SpringBootApplication
@EnableDiscoveryClient//向服务中心注册
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
@LoadBalanced//开启负载均衡的功能
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HelloService.java
package com.bamboo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* Created by xialeme on 2017/10/20.
*/
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
/* @Autowired
public HelloService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}*/
public String helloService(String name) {
// return restTemplate.getForObject("http://SERVICE-HELLO/hello?name="+name,String.class);//带参方式调用
//service-hello中没有参数,因此如下调用
return restTemplate.getForObject("http://SERVICE-HELLO/hello",String.class);
}
}
这里调用的服务URL,IP使用的是服务名称而不是IP地址
HelloControler.java
package com.bamboo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by xialeme on 2017/10/20.
*/
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hello")
public String hello(@RequestParam String name){
return helloService.helloService(name);
}
}
运行服务组件
- 运行eureka-server
- 运行service-hello
- 修改service-hello的端口8763再次运行(如何在idea中多次运行main方法,请参考)
- 运行service-ribbon
运行客户端接口
http://desktop-180i6sh:8764/hello?name=111
刷新可以看到,两个服务轮换调用
hello from port:8762
hello from port:8763
Ribbon原理
[借用方志明博客中的图,service-hi在我这里是service-hello]
- 一个服务注册中心,eureka server,端口为8761
- service-hello工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
- sercvice-ribbon端口为8764,向服务注册中心注册
- 当sercvice-ribbon通过restTemplate调用service-hello的hello接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;
Ribbon的主要组件与工作流程
Ribbon的核心组件(均为接口类型)有以下几个:
- ServerList
用于获取地址列表。它既可以是静态的(提供一组固定的地址),也可以是动态的(从注册中心中定期查询地址列表)。 - ServerListFilter
仅当使用动态ServerList时使用,用于在原始的服务列表中使用一定策略过虑掉一部分地址。 - IRule
选择一个最终的服务地址作为LB结果。选择策略有轮询、根据响应时间加权、断路器(当Hystrix可用时)等。
Ribbon在工作时首选会通过ServerList来获取所有可用的服务列表,然后通过ServerListFilter过虑掉一部分地址,最后在剩下的地址中通过IRule选择出一台服务器作为最终结果。
Ribbon提供的主要负载均衡策略介绍
1:简单轮询负载均衡(RoundRobin)
以轮询的方式依次将请求调度不同的服务器,即每次调度执行i = (i + 1) mod n,并选出第i台服务器。
2:随机负载均衡 (Random)
随机选择状态为UP的Server
3:加权响应时间负载均衡 (WeightedResponseTime)
根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。
4:区域感知轮询负载均衡(ZoneAvoidanceRule)
复合判断server所在区域的性能和server的可用性选择server
参考资料
【微服务架构】SpringCloud之Ribbon(四)
史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)