spring cloud 自定义ribbon客户端
一、自定义Ribbon客户端-【方式一】配置类
1.1、自定义负载规则
增加RibbonConfiguration.java配置类
public class RibbonConfiguration {
// @Autowired
// IClientConfig config;
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
}
其中:RibbonClient中name 微服务名称,configuration配置类
注意:configuration等于的TestConfiguration必须是@Configuration,但要注意它不在主应用程序上下文的@ComponentScan中,否则它将被所有@RibbonClients共享。如果使用@ComponentScan(或@SpringBootApplication),则需要采取措施以避免包含它(例如,将其放在单独的,不重叠的包中,或者指定要在@ComponentScan中显式扫描的包)。
方式1、TestConfiguration不放在spring boot启动类的当前包或子包中即可
方式2、如果TestConfiguration确实需要放在当前包,需要设置如下
增加注解
public @interface ExcludeFromComponentScan {
}
将注解增加至RibbonConfiguration
package com.pupeiyuan.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule; @Configuration
@ExcludeFromComponentScan
public class RibbonConfiguration { // @Autowired
// IClientConfig config;
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
}
将排除注解增加至启动类,将自定义客户端增加至启动类
package com.pupeiyuan.config; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate; @Configuration
//扫描bean
@ComponentScan(basePackages = "com.pupeiyuan.*")
//不用自动配置数据源
@EnableDiscoveryClient
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@RibbonClient(name = "multiple", configuration = RibbonConfiguration.class)
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) })
public class MainApplication extends SpringBootServletInitializer { //相当于xml中的bean标签 用于调用当前方法获取到指定的对象
@Bean(name="remoteRestTemplate")
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MainApplication.class);
}
}
多个自定义客户端可以依次添加@RibbonClient(name = "multiple", configuration = RibbonConfiguration.class)
1.2、源码查看
查看:@RibbonClient注解发现主要属性为 RibbonClientConfiguration,查看其实现
Spring Cloud Netflix默认为Ribbon提供以下Bean(BeanType beanName:ClassName):
IClientConfigribbonClientConfig:DefaultClientConfigImplIRuleribbonRule:ZoneAvoidanceRuleIPingribbonPing:DummyPingServerList<Server>ribbonServerList:ConfigurationBasedServerListServerListFilter<Server>ribbonServerListFilter:ZonePreferenceServerListFilterILoadBalancerribbonLoadBalancer:ZoneAwareLoadBalancerServerListUpdaterribbonServerListUpdater:PollingServerListUpdater
二、自定义Ribbon客户端-【方式二】配置文件
从1.2.0版开始,Spring Cloud Netflix现在支持使用属性自定义Ribbon客户端以与Ribbon文档兼容。
配置属性

<clientName>.ribbon.:
NFLoadBalancerClassName: should implement ILoadBalancer
NFLoadBalancerRuleClassName: should implement IRule
NFLoadBalancerPingClassName: should implement IPing
NIWSServerListClassName: should implement ServerList
NIWSServerListFilterClassName should implement ServerListFilter

注意:配置优先级高于代码自定义和默认配置,并且不会有代码方式的干扰
示例
users: #微服务名称
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
NFLoadBalancerRuleClassName,可以通过这个配置项定制需要的负载均衡规则,可以是ribbon提供的原生的几种规则类,也可以是自己实现的规则类,这些类都实现了IRule接口。指定负载均衡器的实现类。当然,可以设置自己实现的负载均衡器。
NFLoadBalancerPingClassName用于配置查看服务器是否存活。
NIWSServerListClassName是服务器列表的处理类,用来维护服务器列表的。Ribbon已经实现了动态服务器列表。
NIWSServerListFilterClassName是服务器的拦截类。
四、Ribbon内置负载均衡规则
Ribbon框架按照不同需求,已经为我们实现了许多实现了IRule接口的实现类,适用于常用的负载均衡规则。以下规则能够实现大部分负载均衡需求的应用场景,如果有更复杂的需求,可以自己实现IRule。
| 内置负载均衡规则类 | 规则描述 |
| RoundRobinRule | 简单轮询服务列表来选择服务器。它是Ribbon默认的负载均衡规则。 |
| AvailabilityFilteringRule |
对以下两种服务器进行忽略: (1)在默认情况下,这台服务器如果3次连接失败,这台服务器就会被设置为“短路”状态。短路状态将持续30秒,如果再次连接失败,短路的持续时间就会几何级地增加。 注意:可以通过修改配置loadbalancer.<clientName>.connectionFailureCountThreshold来修改连接失败多少次之后被设置为短路状态。默认是3次。 (2)并发数过高的服务器。如果一个服务器的并发连接数过高,配置了AvailabilityFilteringRule规则的客户端也会将其忽略。并发连接数的上线,可以由客户端的<clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit属性进行配置。 |
| WeightedResponseTimeRule |
为每一个服务器赋予一个权重值。服务器响应时间越长,这个服务器的权重就越小。这个规则会随机选择服务器,这个权重值会影响服务器的选择。 |
| ZoneAvoidanceRule | 以区域可用的服务器为基础进行服务器的选择。使用Zone对服务器进行分类,这个Zone可以理解为一个机房、一个机架等。 |
| BestAvailableRule | 忽略哪些短路的服务器,并选择并发数较低的服务器。 |
| RandomRule | 随机选择一个可用的服务器。 |
| Retry | 重试机制的选择逻辑 |
附录:AvailabilityFilteringRule的三个默认配置
# successive connection failures threshold to put the server in circuit tripped state, default 3
niws.loadbalancer.<clientName>.connectionFailureCountThreshold
# Maximal period that an instance can remain in "unusable" state regardless of the exponential increase, default 30
niws.loadbalancer.<clientName>.circuitTripMaxTimeoutSeconds
# threshold of concurrent connections count to skip the server, default is Integer.MAX_INT
<clientName>.<clientConfigNameSpace>.ActiveConnectionsLimit
spring cloud 自定义ribbon客户端的更多相关文章
- spring cloud 使用ribbon简单处理客户端负载均衡
假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...
- 自定义Ribbon客户端策略
说明 为了实现Ribbon细粒度的划分,让调用不同的微服务时采用不同的客户端负载均衡策略, 通常情况下我们会自定义配置策略. 本文以内容中心(content-center)调用户中心微服务(u ...
- 笔记:Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- spring cloud: 关闭ribbon负载均衡
spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...
- spring cloud 通过 ribbon 实现客户端请求的负载均衡(入门级)
项目结构 环境: idea:2020.1 版 jdk:8 maven:3.6.2 1. 搭建项目 ( 1 )父工程:spring_cloud_demo_parent pom 文件 <?xml v ...
- Spring Cloud Gateway Ribbon 自定义负载均衡
在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...
- 从零开始学spring cloud(六) -------- Ribbon
一.Ribbon介绍 Ribbon就是客户端侧负责均衡实现的一种方式,那么Ribbon是什么呢? Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端侧负载均衡算法.Ribb ...
- Spring Cloud Aliaba - Ribbon
Ribbon(有关介绍见RestTemplate末尾) Ribbon负载均衡实现策略 Ribbon负载均衡实现策略通过接口IRule进行实现,默认使用ZoneAvoidanceRule规则进行负载均衡 ...
随机推荐
- 【编程拾遗】C++的static成员函数与单例模式
static小结 static的引入 static 是C++中非经常常使用的修饰符,它被用来控制变量的存储方式和可见性. 函数内部定义的变量,在程序运行到它的定义处时,编译器为它在栈上分配空间,函数在 ...
- SpringBoot的Web配置
重写全局配置 如果springboot提供的springmvc配置不符合要求,则可以通过一个配置类(标有@Configuration注解的类)加上@EnableWebMvc注解来实现完全自己控制的mv ...
- Sql server—— for xml path简单用法(可以按照分组把相同组的列中的不同的值,像字符串一样拼接在一起显示在分组之后的列中。)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI8AAACWCAIAAABo2EyXAAAKeklEQVR4nO2dy27rNh7G+U7CFIrfZX
- 2018 Multi-University Training Contest 1 杭电多校第一场
抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001 Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...
- Faster_RCNN 4.训练模型
总结自论文:Faster_RCNN,与Pytorch代码: 本文主要介绍代码最后部分:trainer.py .train.py , 首先分析一些主要理论操作,然后在代码分析里详细介绍其具体实现.首先 ...
- faster-rcnn自己的理解总结(包括它的前世今身R-CNN和fast R-CNN)
1.grandfather: R-CNN网络 结构如下: 工作流程: Input(an image) Proposals(~2K个,在使用CNN提取特征之前还要先resize) feature ...
- C#编写COM组件
1.新建一个类库项目 2.将Class1.cs改为我们想要的名字 问是否同时给类改名,确定 3.修改Properties目录下面的AssemblyInfo.cs ComVisible属性设置为True ...
- win7 vs2012/2013 编译boost 1.55
bjam install stage --toolset=msvc-11.0 --stagedir="C:\Boost\boost_vc_110" link=shared runt ...
- eclipse 迁移项目 乱码
eclipse 迁移项目总是乱码问题,网上解决都无非是把workspace.项目.文件等改成utf-8,但总是不好使,因为原来有的文件类型还是要改成原来的编码格式,可以使用文本工具如notepad打开 ...
- ffmpeg-201701[10,16,21,23,25]-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...