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 ...
随机推荐
- PHP(五)session和文件上传初步
- .NET基础 (18)特性
特性1 什么是特性,如何自定义一个特性2 .NET中特性可以在哪些元素上使用3 有哪几种方法可以获知一个元素是否申明某个特性4 一个元素是否可以重复申明同一个特性 特性1 什么是特性,如何自定义一个特 ...
- (4)-optXXX方法的使用
在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...
- EF查询记录
public void TestMethod1() { , Ids = , Ids = "4,5,6" } }; , , , , , , , }; var query = quer ...
- async异步方法
在C# 中,可以使用asyc+await来完成一个异步方法. async用来标志一个使用了await的方法是非阻塞API,是一个异步方法,就当成一个普通关键字就行了.关键是await,await是配合 ...
- API Test Postman接口测试之高级篇2
API Test Postman接口测试之高级篇2 一.继承父类的设置: 二.导出及导入: 三.分享文档: 四.发布接口文档: 五.常用脚本: 右边框选的是一些常用的脚本,postman提供的,可以 ...
- JAVA异常的最佳工程学实践探索
此文已由作者占金武授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 先说明一下背景: 项目日志中的Exception会被哨兵统一监控并报警 比较多的项目基于dubbo在做服务化 ...
- ClamAV学习【3】——scanmanager函数浏览
吃饱饭继续浏览Manager.c的scanmanager函数,这个函数的功能吧,暂时理解如下. 接收一个命令行参数(经过处理的optstruct结构指针). 然后根据选项判断文件类型种类,还有一些扫描 ...
- Bootstrap框架常用总结
Bootstrap框架常用标签: 标题标签:<h1>-<h6> bootstrap中也设置的相同的样式 - 若要使用 必须使用空标签来定义 比如<s ...
- CentOS中vsftpd的主动和被动方式
网址http://blog.csdn.net/nyunyuzhao/article/details/5734978,学习了. FTP是File Transfer Protocol(文件传输协议)的缩写 ...