Ribbon是一个有容错能力的客户端负载均衡器,支持HTTP\TCP\UDP协议,从前面Feign的源码可以看出来,默认Feign集成了Ribbon。下面是官方对Ribbon特性的一个简介。
Ribbon is a client side IPC library that is battle-tested in cloud. It provides the following features
Load balancing
Fault tolerance
Multiple protocol (HTTP, TCP, UDP) support in an asynchronous and reactive model
Caching and batching
值得注意的是Ribbon项目目前的状态为维护,也就是说官方不会有大规模程度的开发,提供新特性,理由是一方面Ribbon运行十分稳定,另一方面Netflex在研发基于gRPC的RPC框架,精力有限。
笔者也搜索了下有关RPC的项目,star数目在3000以上,用Java语言写的,发现有几个项目可以关注以下,如微博的motan,阿里巴巴的dubbo和谷歌的grpc,这些可以重点关注。
rpc in:readme stars:>3000 language:java
weibocom/motan
apache/dubbo
grpc/grpc-java
sofastack/sofa-boot
关于Spring与RPC的集成,Spring官方在2015年出了一篇文章描述spring boot如何集成谷歌的protocol buffers,不过反馈并不好,更多人希望这个功能集成在Spring的框架里,而不是在简单的CRUD操作里写一堆的controller。
Using Google Protocol Buffers with Spring MVC-based REST Services
有鉴于此,学习下dubbo在RPC方面的成果是很有益处的,有机会,笔者也会在未来的文章中加入在这一方面的探讨。
在讲Ribbon负载均衡实例之前还要说明一些参数,这样能更好地理解之后的演示代码。Ribbon的行为是由下面的七个Bean的行为所决定的,因此可以搭配不同的实现来满足负载均衡的要求,或者实现提供的接口来做自定义实现。
Bean Type |
Bean Name |
Class Name |
Description |
IClientConfig | ribbonClientConfig |
DefaultClientConfigImpl | defines the client configuration used by various APIs to initialize clients or load balancers and for method execution |
IRule |
ribbonRule |
ZoneAvoidanceRule |
defines a "Rule" for a LoadBalancer |
IPing | ribbonPing | DummyPing | defines how we "ping" a server to check if its alive |
ServerList<Server> | ribbonServerList | ConfigurationBasedServerList | defines the methods sed to obtain the list of servers |
ServerListFilter<Server> |
ribbonServerListFilter | ZonePreferenceServerListFilter |