简介
Ribbon
是
Netflix
发布的负载均衡器, 它有助于控制
HTTP
和
TCP
客户端的行为。 为
Ribbon
配置服务提供者地址列表后,
Ribbon
就可基于某种负载均衡算法, 自动地帮助
服务消费者去请求。
Ribbon
默认为我们提供了很多的负载均衡算法, 例如轮询、 随机
等
如何使用
//1. Pom.xml引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
//2. SpringBoot项目加入配置
@Configuration
public class RibbonCfg {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
//3. Controller使用
@Autowired
RestTemplate restTemplate;
@RequestMapping("/sayhello")
public String sayhello() {
String url="http://exam/exam/hello"; //直接使用服务名调用
String result = restTemplate.getForEntity(url, String.class).getBody();
System.out.println("return info : "+result);
return result;
}
很多场景下, 可能根据需要自定义Ribbon的配置, 例如修改Ribbon的负载均衡规则等。 Spring Cloud Camden允许使用Java代码或属性自定义Ribbon的配置, 两种方式是等价的
使用
Java
代码自定义
Ribbon
配置
Ribbon中定义的每一个接口都有多种不同的策略实现,同时这些接口之间又有一 定的依赖关系,这使得第一次使用Ribbon的开发者很难上手,不知道如何选择具体的实现 策略以及如何组织它们的关系。Spring Cloud Ribbon中的自动化配置恰恰能够解决这样的 痛点,在引入Spring Cloud Ribbon的依赖之后,就能够自动化构建下面这些接口的实现。
• IClientConfig: Ribbon 的客户端配置,默认采用 com. netflix. client. config. DefaultClientConfiglmpl 实现。
• IRule: Ribbon 的负载均衡策略,默认釆用 com.netf lix. loadbalancer. ZoneAvoidanceRule实现,该策略能够在多区域环境下选出最佳区域的实例进行 访问。
• I Ping: Ribbon 的实例检查策略,默认釆用 com.netflix. loadbalancer. NoOpPing 实现,该检查策略是一个特殊的实现,实际上它并不会检查实例是否可用,而是始 终返回true,默认认为所有服务实例都是可用的。
• ServerList<Server>:服务实例清单的维护机制,默认采用com.netflix . loadbalencer. Conf igurationBasedServerList 实现。
• ServerListFilter<Server> :服务实例清单过滤机制,默认采用org. springframework.cloud.netflix.ribbon.ZonePreferenceServerLis tFilter实现,该策略能够优先过滤出与请求调用方处于同区域的服务实例。
• ILoadBalancer :负载均衡器,默认采用 com.netflix. loadbalancer.ZoneAwareLoadBalancer实现,它具备了区域感知的能力。
// Ribbon 的默认配置如下(格式是BeanType beanName: ClassName):
// • IClientConfig ribbonClientConfig: DefaultClientConfiglmpl
// • IRule ribbonRule: ZoneAvoidanceRule
// • IPing ribbonPing: NoOpPing
// • ServerList ribbonServerList: ConfigurationBasedServerList
// • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
// • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer58 5使用Ribbon实现客户端侧负载均衡
// 简单说明一下, 例如以下代码:
@Bean
@ConditionalOnMissingBean
public IRule ribbonRule(IClientConfig config) {
ZoneAvoidanceRule rule = new ZoneAvoidanceRuleO;
rule.initWithNiwsConfig(config);
return rule;
}
// 来自org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
// BeanType 是 IRule, beanName 是 ribbonRule, ClassName 是 ZoneAvoidanceRule, 这是一
// 种根据服务提供者所在Zone的性能以及服务提供者可用性综合计算, 选择提供者节点
// 的负载均衡规
// 例子2
// 创建了 PingUrl 实例,所以默认的NoOpPing就不会被创建。
@Configuration
public class MyRibbonConfiguration {
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
}
方式二 属性自定义Ribbon配置
下面采用属性来修改名为service-provider-user的Ribbon客户端的负载均衡规则。
在项目的 application.yml 中添加以下内容即可:
service-provider-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
这样, 就可以将负载均衡规则修改为随机。 由代码不难看出, 使用属性自定义的方式比
用Java代码配置方便很多