什么是Ribbon
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients.
上述是官方文档对Ribbon的解释,意思是Ribbon是一个客户端负载均衡器,它可以有效的控制HTTP和TCP客户端的访问。上述一段话有个很关键的词“客户端负载均衡器”,我们常用的负载均衡模式就是nginx反向代理模式,这种模式我们是不知道服务究竟请求的是那台机器的。这种负载均衡模式是服务端负载均衡。
而客户端负载均衡就是在发起请求是有明确的目标,请求精确指向集群中的某台机器。
Ribbon的负载均衡默认是依靠Eureka来实现的,Ribbon通过Eureka客户端定期拉取注册服务,通过客户端的轮训、随机等算法来实现负载均衡。
快速开始
这个案例依赖Eureka-Server,请准备好server服务。Eureka相关查看Spring-Cloud系列(一) Eureka
添加依赖
org.springframework.boot
spring-boot-starter-parent
1.5.13.RELEASE
UTF-8
UTF-8
1.8
Dalston.SR5
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
看上面的依赖有些同学可能会奇怪,为什么没有引入Ribbon依赖,只引入了Eureka客户端。下面我们来看一下Eureka客户端的依赖关系图就明白了。
从上图中我们明显可以看出来,Eureka客户端的依赖是包含Ribbon依赖的。所以我们不需要添加依赖。
代码
初始化bean
/**
* @LoadBalanced Ribbon整合注解,Rebbon本身依赖在EurekClient中存在。所以不需要继续引入
* Ribbon是客户端负载均衡实现之一,具有轮训,随机等等多种算法。
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
使用方式
@RestController
public class RibbonControlller {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/url")
public String postUser() {
return this.restTemplate.getForObject("http://eureka-client-one/url", String.class);
}
}
application配置
server:
port: 8082
spring:
application:
name: ribbon-one
eureka:
instance:
prefer-ip-address: true
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
client:
serviceUrl:
defaultZone: http://user:Pass123456@localhost:8761/eureka
启动效果
自定义RibbonClient
代码自定义
定义配置
@Configuration
public class RibbonConfiguration {
@Autowired
private IClientConfig config;
@Bean
public IRule ribbonRule(IClientConfig config){
return new RandomRule();
}
}
The FooConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication) you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan).
注意Configuration类不能和主类平级,也不能在主类的子包下,否则该配置会覆盖本项目下所有的Ribben配置
配置注入
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "eureka-client-one",configuration = RibbonConfiguration.class)
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
使用方式
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
return instance.toString();
}
可被重写的其他配置
上面的代码配置我们改变了IRule配置,更多例子参考官方文档。以下配置可以通过代码修改:
- IClientConfig ribbonClientConfig: DefaultClientConfigImpl
- IRule ribbonRule: ZoneAvoidanceRule
- IPing ribbonPing: NoOpPing
- ServerList
ribbonServerList: ConfigurationBasedServerList - ServerListFilter
ribbonServerListFilter: ZonePreferenceServerListFilter - ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
- ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater
配置文件自定义
配置文件
users:#即需要调用的应用名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
使用方式
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
return instance.toString();
}
注意!
Ribbon的配置优先级顺序是 yml>javaConfig>default
其他配置选项
上面的配置文件我们改变IRule配置,更多例子参考官方文档。以下配置可以通过配置文件修改
- NFLoadBalancerClassName: should implement ILoadBalancer
- NFLoadBalancerRuleClassName: should implement IRule
- NFLoadBalancerPingClassName: should implement IPing
- NIWSServerListClassName: should implement ServerList
- NIWSServerListFilterClassName should implement ServerListFilter
Ribbon的其他配置
Ribbon禁用Eureka
ribbon:
eureka:
enabled: false
users:#即需要调用的应用名
ribbon:
listOfServers: localhost:8080,localhost:8081 #服务IP List
延迟加载Ribbon
ribbon:
eager-load:
enabled: true
clients: client1, client2, client3
本文参考Spring-Cloud-Ribbon,Ribbon延迟加载