Ribbon是什么?
学而时习之,不亦说乎!
--《论语》
Ribbon使用版本2.2.2
Ribbon是什么?
开始接触Ribbon的时候,网上以及很多书上都说Ribbon是一个负载均衡的工具,提供各种负载均衡算法。
但是分析完源码后,在我的理解里,Ribbon是一个http客户端,它具备了负载均衡,失败重试,ping等功能。
比如httpclient就是一个http客户端,它就是用来发送http请求的,但是Ribbon在httpclient上做了更多的封装,满足更好的使用,当然,也可以使用其他的http客户端。
所以,Ribbon并不是很多人说的负载均衡工具,而是一个具有负载均衡等功能的http客户端。
即:Ribbon是http客户端。
示例
package org.crazyit.cloud; import com.netflix.client.ClientFactory;
import com.netflix.client.IClient;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.client.config.IClientConfigKey;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpRequest.Verb;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.niws.client.http.RestClient; /**
*
* @author zby
* @date 2019年1月23日
* @Description ↓↑←→↘↙↖↗↔
*/
@SuppressWarnings("all")
public class RibbonMain { public static final String DELIMITER = ".";
public static final String DEFAULT = "default";
public static final String CUSTOM = "custom"; public static void main(String[] args) throws Exception {
// 创建指定名称的客户端配置,会使用默认配置进行初始化,然后使用archaius读取自定义的配置
// IClientConfig iClientConfig = ClientFactory.getNamedConfig("default");↓
IClientConfig defaltClientConfig = ClientFactory.getNamedConfig(DEFAULT, DefaultClientConfigImpl.class); // 使用archaius设置自定义配置,配置结构为:name+namespace+key。name由我们指定,namespace是固定的,Ribbon的namespace就是ribbon,key是配置的名称
ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.ListOfServers),
"http://www.spring.io,http://www.apache.org");
ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.MaxAutoRetriesNextServer), 0);
// ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.ClientClassName),
// "com.netflix.client.IClient子类全路径");
// ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NIWSServerListClassName),
// "com.netflix.loadbalancer.ServerList子类全路径");
// ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerClassName),
// "com.netflix.loadbalancer.ILoadBalancer子类全路径");
// ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerPingClassName),
// "com.netflix.loadbalancer.IPing子类全路径");
// ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerRuleClassName),
// "com.netflix.loadbalancer.IRule子类全路径");
IClientConfig customClientConfig = ClientFactory.getNamedConfig(CUSTOM, DefaultClientConfigImpl.class); printAllRibbonConfig(defaltClientConfig, customClientConfig); // 默认配置
// ClientClassName=com.netflix.niws.client.http.RestClient
// NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList
// NFLoadBalancerClassName=com.netflix.loadbalancer.ZoneAwareLoadBalancer
// NFLoadBalancerPingClassName=com.netflix.loadbalancer.DummyPing
// NFLoadBalancerRuleClassName=com.netflix.loadbalancer.AvailabilityFilteringRule // IClient iClient = ClientFactory.getNamedClient("zby");↓
IClient iClient = ClientFactory.getNamedClient(CUSTOM, DefaultClientConfigImpl.class);
RestClient restClient = (RestClient) iClient;
HttpRequest httpRequest = HttpRequest.newBuilder().uri("/").verb(Verb.GET).build();
System.out.println(httpRequest.getUri());
HttpResponse httpResponse = restClient.executeWithLoadBalancer(httpRequest, null);
System.out.println(httpResponse.getStatus()); // ILoadBalancer iLoadBalancer = ClientFactory.getNamedLoadBalancer(CUSTOM);↓
ILoadBalancer iLoadBalancer = ClientFactory.getNamedLoadBalancer(CUSTOM, DefaultClientConfigImpl.class);
for (int i = 0; i < 6; i++) {
Server server = iLoadBalancer.chooseServer(null);
System.out.println(server);
} } /**
*
* @author zby
* @date 2019年1月23日
* @param name
* @param iClientConfigKey
* @return
* @Description 获取Ribbon配置的key
*/
public static String getPropertyKey(String name, IClientConfigKey<?> iClientConfigKey) {
return name + DELIMITER + DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + DELIMITER + iClientConfigKey.key();
} public static void printAllRibbonConfig(IClientConfig iClientConfig1, IClientConfig iClientConfig2) {
for (IClientConfigKey iClientConfigKey : IClientConfigKey.Keys.values()) {
println(iClientConfig1, iClientConfig2, iClientConfigKey);
}
} private static void println(IClientConfig iClientConfig1, IClientConfig iClientConfig2, IClientConfigKey<?> iClientConfigKey) {
System.out.println(iClientConfigKey.key());
System.out.println(
"\t\t\t\t\t\t【" + iClientConfig1.get(iClientConfigKey) + "】" + "←--------→【" + iClientConfig2.get(iClientConfigKey) + "】");
}
}
输出结果:
RequestIdHeaderName
【null】←--------→【null】
EnableGZIPContentEncodingFilter
【false】←--------→【false】
ServerListRefreshInterval
【null】←--------→【null】
UseIPAddrForServer
【false】←--------→【false】
MaxTotalHttpConnections
【200】←--------→【200】
PoolKeepAliveTimeUnits
【SECONDS】←--------→【SECONDS】
VipAddressResolverClassName
【com.netflix.client.SimpleVipAddressResolver】←--------→【com.netflix.client.SimpleVipAddressResolver】
VipAddress
【null】←--------→【null】
EnablePrimeConnections
【false】←--------→【false】
ForceClientPortConfiguration
【null】←--------→【null】
NFLoadBalancerPingClassName
【com.netflix.loadbalancer.DummyPing】←--------→【com.netflix.loadbalancer.DummyPing】
MinPrimeConnectionsRatio
【1.0】←--------→【1.0】
MaxHttpConnectionsPerHost
【50】←--------→【50】
PoolKeepAliveTime
【900】←--------→【900】
ConnectionManagerTimeout
【2000】←--------→【2000】
ProxyPort
【null】←--------→【null】
PrioritizeVipAddressBasedServers
【true】←--------→【true】
EnableMarkingServerDownOnReachingFailureLimit
【null】←--------→【null】
ReadTimeout
【5000】←--------→【5000】
ServerDownFailureLimit
【null】←--------→【null】
IsHostnameValidationRequired
【null】←--------→【null】
listOfServers
【】←--------→【http://spring.io,http://www.apache.org】
FollowRedirects
【false】←--------→【false】
KeyStorePassword
【null】←--------→【null】
ProxyHost
【null】←--------→【null】
MaxAutoRetries
【0】←--------→【0】
StaleCheckingEnabled
【null】←--------→【null】
EnableZoneExclusivity
【false】←--------→【false】
MaxRetriesPerServerPrimeConnection
【9】←--------→【9】
RequestSpecificRetryOn
【null】←--------→【null】
ConnIdleEvictTimeMilliSeconds
【30000】←--------→【30000】
TargetRegion
【null】←--------→【null】
InitializeNFLoadBalancer
【null】←--------→【null】
EnableConnectionPool
【true】←--------→【true】
ClientClassName
【com.netflix.niws.client.http.RestClient】←--------→【com.netflix.niws.client.http.RestClient】
MaxTotalTimeToPrimeConnections
【30000】←--------→【30000】
ConnectionCleanerRepeatInterval
【30000】←--------→【30000】
Version
【null】←--------→【null】
ReceiveBufferSize
【null】←--------→【null】
PoolMaxThreads
【200】←--------→【200】
PoolMinThreads
【1】←--------→【1】
CustomSSLSocketFactoryClassName
【null】←--------→【null】
PrimeConnectionsClassName
【com.netflix.niws.client.http.HttpPrimeConnection】←--------→【com.netflix.niws.client.http.HttpPrimeConnection】
EnableZoneAffinity
【false】←--------→【false】
Linger
【null】←--------→【null】
MaxConnectionsPerHost
【50】←--------→【50】
OkToRetryOnAllOperations
【false】←--------→【false】
KeyStore
【null】←--------→【null】
TrustStorePassword
【null】←--------→【null】
BackoffTimeout
【null】←--------→【null】
NFLoadBalancerClassName
【com.netflix.loadbalancer.ZoneAwareLoadBalancer】←--------→【com.netflix.loadbalancer.ZoneAwareLoadBalancer】
ServerListUpdaterClassName
【null】←--------→【null】
NIWSServerListClassName
【com.netflix.loadbalancer.ConfigurationBasedServerList】←--------→【com.netflix.loadbalancer.ConfigurationBasedServerList】
MaxTotalConnections
【200】←--------→【200】
ServerDownStatWindowInMillis
【null】←--------→【null】
DeploymentContextBasedVipAddresses
【null】←--------→【null】
ConnectionPoolCleanerTaskEnabled
【true】←--------→【true】
IgnoreUserTokenInConnectionPoolForSecureClient
【null】←--------→【null】
TrustStore
【null】←--------→【null】
GZipPayload
【null】←--------→【null】
NFLoadBalancerMaxTotalPingTime
【null】←--------→【null】
IsSecure
【null】←--------→【null】
MaxAutoRetriesNextServer
【1】←--------→【0】
SendBufferSize
【null】←--------→【null】
NFLoadBalancerRuleClassName
【com.netflix.loadbalancer.AvailabilityFilteringRule】←--------→【com.netflix.loadbalancer.AvailabilityFilteringRule】
NIWSServerListFilterClassName
【null】←--------→【null】
AppName
【null】←--------→【null】
ConnectTimeout
【2000】←--------→【2000】
IsClientAuthRequired
【false】←--------→【false】
PrimeConnectionsURI
【/】←--------→【/】
NFLoadBalancerPingInterval
【null】←--------→【null】
SecurePort
【null】←--------→【null】
Port
【7001】←--------→【7001】
RulePredicateClasses
【null】←--------→【null】
/
200
spring.io:80
www.apache.org:80
spring.io:80
www.apache.org:80
spring.io:80
www.apache.org:80
留下的疑问?
1.ClientFactory242行和248行是否重复???
2.个人觉得ClientFactory70行的loadBalancer = registerNamedLoadBalancerFromclientConfig(restClientName, clientConfig);改为loadBalancer = getNamedLoadBalancer(restClientName, clientConfig);是否更好???
Ribbon是什么?的更多相关文章
- Devexpress Ribbon Add Logo
一直在网上找类似的效果.在Devpexress控件里面的这个是一个Demo的.没法查看源代码.也不知道怎么写的.所以就在网上搜索了半天的. 终于找到类似的解决办法. 可以使用重绘制的办法的来解决. [ ...
- 问题解决——MFC Ribbon 响应函数 错乱 执行其他函数
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...
- 问题解决——MFC Ribbon 添加图标
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- Office2013插件开发Outlook篇(2)-- Ribbon
一.获取当前实例 在Ribbon1的任何方法中调用如下代码,可获取当前实例. 如: Application application = new Application(); var list = ap ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
- sharepont 2013 隐藏Ribbon 菜单
引用:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Web.Comma ...
- 使用vs2010创建MFC C++ Ribbon程序
Your First MFC C++ Ribbon Application with Visual Studio 2010 Earlier this month, I put together my ...
- E-Form++图形可视化源码库新增同BCGSoft的Ribbon结合示例
2015年11月20日,来自UCanCode E-Form++源码库的开发团队消息,E-Form++正式提供了同BCGSoft的Ribbon界面风格相结合的示例,如下图: 下载此示例请访问: http ...
- DotNetBar 第2课,窗口设置 Ribbon Form 样式
1. 新增 windows 窗体时,选 Ribbon Form 2. 窗体继承 Office2007RibbonForm 3. 设计窗口下面,删除 删除styleManager1 组件 窗口效果如下 ...
- MSCRM 2013/2015 Ribbon Editor
由于新版本2015的解决方案与之前有变化,因此许多老的Tools已经不能使用,推荐给大家新的Ribbon Editor Tool. 下载地址: http://www.develop1.net/publ ...
随机推荐
- idea如何设置注释作者信息
什么情况下使用? 在建一个新的类的时候 有注释信息 如下图所示 实现步骤 1 打开idea后 点击File后 选择Settings..如下图 2 打开后打开 file and code t ...
- 创建圆角 抛出一个错误:二元运算符“|”不能用于两个UIRectCorner操作数
// let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRect ...
- Opencascade、OpenGL和OpenSceneGraph的区别与联系
OpenGL只是三维显示 Openscenegraph基于场景图的概念,它提供一个在OpenGL之上的面向对象的框架,从而能把开发者从实现和优化底层图形的调用中解脱出来 Opencascade更适合算 ...
- (字符串 KMP)Blue Jeans -- POJ -- 3080:
链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- Java反射API研究(2)——java.lang.reflect详细内容与关系
对于最新的java1.8而言,reflect中接口的结构是这样的: java.lang.reflect.AnnotatedElement java.lang.reflect.AnnotatedType ...
- ajax 与 form 提交的区别
有如下几种区别: 1. Ajax在提交.请求.接收时,都是异步进行的,网页不需要刷新:Form提交则是新建一个页面,哪怕是提交给自己本身的页面,也是需要刷新的: 2. A在提交时,是在后台新建一个请求 ...
- 日志框架 NLog
这里按老规矩先进行和其它产品进行比较: 目前在.net平台存在两个比较老牌的日志框架分别为Log4net和NLog. 我们进行对这两种框架进行比较下 Log4net Log4net是一个老牌的日志框架 ...
- Ubuntu 12.04 Openssh 安装过程
1.输入 apt-get install openssh-server 注意 出现依赖 需要使用 查看依赖是否需要,如果不需要可以直接删除,sudo apt-get remove XXXX 或者sud ...
- WPF 降低.net framework到4.0
1. 问题背景 由于xp系统上面最高只能安装.net framework 4.0,所以公司项目需要将原来项目的.net framework版本降低到4.0,具体的降版本很简单,只要把项目属性中的目标框 ...
- UWP开发入门(八)——聊天窗口和ItemTemplateSelector
我们平常用的最多的APP可能就是企鹅和微信了.有没有想过聊天窗口如何实现的?本篇我们将简单模拟一个聊天窗口. 聊天窗口大致上就是消息的一个集合列表.集合列表最常见的展现形式无非就是ListView.可 ...