学而时习之,不亦说乎!

                             --《论语》

Ribbon使用版本2.2.2

Ribbon是什么?

  开始接触Ribbon的时候,网上以及很多书上都说Ribbon是一个负载均衡的工具,提供各种负载均衡算法。

  但是分析完源码后,在我的理解里,Ribbon是一个http客户端,它具备了负载均衡,失败重试,ping等功能。

  比如httpclient就是一个http客户端,它就是用来发送http请求的,但是Ribbon在httpclient上做了更多的封装,满足更好的使用,当然,也可以使用其他的http客户端。

  所以,Ribbon并不是很多人说的负载均衡工具,而是一个具有负载均衡等功能的http客户端。

  即:Ribbon是http客户端。

示例

  1. package org.crazyit.cloud;
  2.  
  3. import com.netflix.client.ClientFactory;
  4. import com.netflix.client.IClient;
  5. import com.netflix.client.config.DefaultClientConfigImpl;
  6. import com.netflix.client.config.IClientConfig;
  7. import com.netflix.client.config.IClientConfigKey;
  8. import com.netflix.client.http.HttpRequest;
  9. import com.netflix.client.http.HttpRequest.Verb;
  10. import com.netflix.client.http.HttpResponse;
  11. import com.netflix.config.ConfigurationManager;
  12. import com.netflix.loadbalancer.ILoadBalancer;
  13. import com.netflix.loadbalancer.Server;
  14. import com.netflix.niws.client.http.RestClient;
  15.  
  16. /**
  17. *
  18. * @author zby
  19. * @date 2019年1月23日
  20. * @Description ↓↑←→↘↙↖↗↔
  21. */
  22. @SuppressWarnings("all")
  23. public class RibbonMain {
  24.  
  25. public static final String DELIMITER = ".";
  26. public static final String DEFAULT = "default";
  27. public static final String CUSTOM = "custom";
  28.  
  29. public static void main(String[] args) throws Exception {
  30. // 创建指定名称的客户端配置,会使用默认配置进行初始化,然后使用archaius读取自定义的配置
  31. // IClientConfig iClientConfig = ClientFactory.getNamedConfig("default");↓
  32. IClientConfig defaltClientConfig = ClientFactory.getNamedConfig(DEFAULT, DefaultClientConfigImpl.class);
  33.  
  34. // 使用archaius设置自定义配置,配置结构为:name+namespace+key。name由我们指定,namespace是固定的,Ribbon的namespace就是ribbon,key是配置的名称
  35. ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.ListOfServers),
  36. "http://www.spring.io,http://www.apache.org");
  37. ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.MaxAutoRetriesNextServer), 0);
  38. // ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.ClientClassName),
  39. // "com.netflix.client.IClient子类全路径");
  40. // ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NIWSServerListClassName),
  41. // "com.netflix.loadbalancer.ServerList子类全路径");
  42. // ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerClassName),
  43. // "com.netflix.loadbalancer.ILoadBalancer子类全路径");
  44. // ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerPingClassName),
  45. // "com.netflix.loadbalancer.IPing子类全路径");
  46. // ConfigurationManager.getConfigInstance().setProperty(getPropertyKey(CUSTOM, IClientConfigKey.Keys.NFLoadBalancerRuleClassName),
  47. // "com.netflix.loadbalancer.IRule子类全路径");
  48. IClientConfig customClientConfig = ClientFactory.getNamedConfig(CUSTOM, DefaultClientConfigImpl.class);
  49.  
  50. printAllRibbonConfig(defaltClientConfig, customClientConfig);
  51.  
  52. // 默认配置
  53. // ClientClassName=com.netflix.niws.client.http.RestClient
  54. // NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList
  55. // NFLoadBalancerClassName=com.netflix.loadbalancer.ZoneAwareLoadBalancer
  56. // NFLoadBalancerPingClassName=com.netflix.loadbalancer.DummyPing
  57. // NFLoadBalancerRuleClassName=com.netflix.loadbalancer.AvailabilityFilteringRule
  58.  
  59. // IClient iClient = ClientFactory.getNamedClient("zby");↓
  60. IClient iClient = ClientFactory.getNamedClient(CUSTOM, DefaultClientConfigImpl.class);
  61. RestClient restClient = (RestClient) iClient;
  62. HttpRequest httpRequest = HttpRequest.newBuilder().uri("/").verb(Verb.GET).build();
  63. System.out.println(httpRequest.getUri());
  64. HttpResponse httpResponse = restClient.executeWithLoadBalancer(httpRequest, null);
  65. System.out.println(httpResponse.getStatus());
  66.  
  67. // ILoadBalancer iLoadBalancer = ClientFactory.getNamedLoadBalancer(CUSTOM);↓
  68. ILoadBalancer iLoadBalancer = ClientFactory.getNamedLoadBalancer(CUSTOM, DefaultClientConfigImpl.class);
  69. for (int i = 0; i < 6; i++) {
  70. Server server = iLoadBalancer.chooseServer(null);
  71. System.out.println(server);
  72. }
  73.  
  74. }
  75.  
  76. /**
  77. *
  78. * @author zby
  79. * @date 2019年1月23日
  80. * @param name
  81. * @param iClientConfigKey
  82. * @return
  83. * @Description 获取Ribbon配置的key
  84. */
  85. public static String getPropertyKey(String name, IClientConfigKey<?> iClientConfigKey) {
  86. return name + DELIMITER + DefaultClientConfigImpl.DEFAULT_PROPERTY_NAME_SPACE + DELIMITER + iClientConfigKey.key();
  87. }
  88.  
  89. public static void printAllRibbonConfig(IClientConfig iClientConfig1, IClientConfig iClientConfig2) {
  90. for (IClientConfigKey iClientConfigKey : IClientConfigKey.Keys.values()) {
  91. println(iClientConfig1, iClientConfig2, iClientConfigKey);
  92. }
  93. }
  94.  
  95. private static void println(IClientConfig iClientConfig1, IClientConfig iClientConfig2, IClientConfigKey<?> iClientConfigKey) {
  96. System.out.println(iClientConfigKey.key());
  97. System.out.println(
  98. "\t\t\t\t\t\t【" + iClientConfig1.get(iClientConfigKey) + "】" + "←--------→【" + iClientConfig2.get(iClientConfigKey) + "】");
  99. }
  100. }

 输出结果:

  1. RequestIdHeaderName
  2. null】←--------→【null
  3. EnableGZIPContentEncodingFilter
  4. false】←--------→【false
  5. ServerListRefreshInterval
  6. null】←--------→【null
  7. UseIPAddrForServer
  8. false】←--------→【false
  9. MaxTotalHttpConnections
  10. 200】←--------→【200
  11. PoolKeepAliveTimeUnits
  12. SECONDS】←--------→【SECONDS
  13. VipAddressResolverClassName
  14. com.netflix.client.SimpleVipAddressResolver】←--------→【com.netflix.client.SimpleVipAddressResolver
  15. VipAddress
  16. null】←--------→【null
  17. EnablePrimeConnections
  18. false】←--------→【false
  19. ForceClientPortConfiguration
  20. null】←--------→【null
  21. NFLoadBalancerPingClassName
  22. com.netflix.loadbalancer.DummyPing】←--------→【com.netflix.loadbalancer.DummyPing
  23. MinPrimeConnectionsRatio
  24. 1.0】←--------→【1.0
  25. MaxHttpConnectionsPerHost
  26. 50】←--------→【50
  27. PoolKeepAliveTime
  28. 900】←--------→【900
  29. ConnectionManagerTimeout
  30. 2000】←--------→【2000
  31. ProxyPort
  32. null】←--------→【null
  33. PrioritizeVipAddressBasedServers
  34. true】←--------→【true
  35. EnableMarkingServerDownOnReachingFailureLimit
  36. null】←--------→【null
  37. ReadTimeout
  38. 5000】←--------→【5000
  39. ServerDownFailureLimit
  40. null】←--------→【null
  41. IsHostnameValidationRequired
  42. null】←--------→【null
  43. listOfServers
  44. 【】←--------→【http://spring.io,http://www.apache.org】
  45. FollowRedirects
  46. false】←--------→【false
  47. KeyStorePassword
  48. null】←--------→【null
  49. ProxyHost
  50. null】←--------→【null
  51. MaxAutoRetries
  52. 0】←--------→【0
  53. StaleCheckingEnabled
  54. null】←--------→【null
  55. EnableZoneExclusivity
  56. false】←--------→【false
  57. MaxRetriesPerServerPrimeConnection
  58. 9】←--------→【9
  59. RequestSpecificRetryOn
  60. null】←--------→【null
  61. ConnIdleEvictTimeMilliSeconds
  62. 30000】←--------→【30000
  63. TargetRegion
  64. null】←--------→【null
  65. InitializeNFLoadBalancer
  66. null】←--------→【null
  67. EnableConnectionPool
  68. true】←--------→【true
  69. ClientClassName
  70. com.netflix.niws.client.http.RestClient】←--------→【com.netflix.niws.client.http.RestClient
  71. MaxTotalTimeToPrimeConnections
  72. 30000】←--------→【30000
  73. ConnectionCleanerRepeatInterval
  74. 30000】←--------→【30000
  75. Version
  76. null】←--------→【null
  77. ReceiveBufferSize
  78. null】←--------→【null
  79. PoolMaxThreads
  80. 200】←--------→【200
  81. PoolMinThreads
  82. 1】←--------→【1
  83. CustomSSLSocketFactoryClassName
  84. null】←--------→【null
  85. PrimeConnectionsClassName
  86. com.netflix.niws.client.http.HttpPrimeConnection】←--------→【com.netflix.niws.client.http.HttpPrimeConnection
  87. EnableZoneAffinity
  88. false】←--------→【false
  89. Linger
  90. null】←--------→【null
  91. MaxConnectionsPerHost
  92. 50】←--------→【50
  93. OkToRetryOnAllOperations
  94. false】←--------→【false
  95. KeyStore
  96. null】←--------→【null
  97. TrustStorePassword
  98. null】←--------→【null
  99. BackoffTimeout
  100. null】←--------→【null
  101. NFLoadBalancerClassName
  102. com.netflix.loadbalancer.ZoneAwareLoadBalancer】←--------→【com.netflix.loadbalancer.ZoneAwareLoadBalancer
  103. ServerListUpdaterClassName
  104. null】←--------→【null
  105. NIWSServerListClassName
  106. com.netflix.loadbalancer.ConfigurationBasedServerList】←--------→【com.netflix.loadbalancer.ConfigurationBasedServerList
  107. MaxTotalConnections
  108. 200】←--------→【200
  109. ServerDownStatWindowInMillis
  110. null】←--------→【null
  111. DeploymentContextBasedVipAddresses
  112. null】←--------→【null
  113. ConnectionPoolCleanerTaskEnabled
  114. true】←--------→【true
  115. IgnoreUserTokenInConnectionPoolForSecureClient
  116. null】←--------→【null
  117. TrustStore
  118. null】←--------→【null
  119. GZipPayload
  120. null】←--------→【null
  121. NFLoadBalancerMaxTotalPingTime
  122. null】←--------→【null
  123. IsSecure
  124. null】←--------→【null
  125. MaxAutoRetriesNextServer
  126. 1】←--------→【0
  127. SendBufferSize
  128. null】←--------→【null
  129. NFLoadBalancerRuleClassName
  130. com.netflix.loadbalancer.AvailabilityFilteringRule】←--------→【com.netflix.loadbalancer.AvailabilityFilteringRule
  131. NIWSServerListFilterClassName
  132. null】←--------→【null
  133. AppName
  134. null】←--------→【null
  135. ConnectTimeout
  136. 2000】←--------→【2000
  137. IsClientAuthRequired
  138. false】←--------→【false
  139. PrimeConnectionsURI
  140. 【/】←--------→【/】
  141. NFLoadBalancerPingInterval
  142. null】←--------→【null
  143. SecurePort
  144. null】←--------→【null
  145. Port
  146. 7001】←--------→【7001
  147. RulePredicateClasses
  148. null】←--------→【null
  149. /
  150. 200
  151. spring.io:80
  152. www.apache.org:80
  153. spring.io:80
  154. www.apache.org:80
  155. spring.io:80
  156. www.apache.org:80

  

 留下的疑问?

  1. 1.ClientFactory242行和248行是否重复???
  1. 2.个人觉得ClientFactory70行的loadBalancer = registerNamedLoadBalancerFromclientConfig(restClientName, clientConfig);改为loadBalancer = getNamedLoadBalancer(restClientName, clientConfig);是否更好???

Ribbon是什么?的更多相关文章

  1. Devexpress Ribbon Add Logo

    一直在网上找类似的效果.在Devpexress控件里面的这个是一个Demo的.没法查看源代码.也不知道怎么写的.所以就在网上搜索了半天的. 终于找到类似的解决办法. 可以使用重绘制的办法的来解决. [ ...

  2. 问题解决——MFC Ribbon 响应函数 错乱 执行其他函数

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  3. 问题解决——MFC Ribbon 添加图标

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  4. Office2013插件开发Outlook篇(2)-- Ribbon

    一.获取当前实例 在Ribbon1的任何方法中调用如下代码,可获取当前实例. 如: Application application = new Application(); var list = ap ...

  5. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

  6. sharepont 2013 隐藏Ribbon 菜单

    引用:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Web.Comma ...

  7. 使用vs2010创建MFC C++ Ribbon程序

    Your First MFC C++ Ribbon Application with Visual Studio 2010 Earlier this month, I put together my ...

  8. E-Form++图形可视化源码库新增同BCGSoft的Ribbon结合示例

    2015年11月20日,来自UCanCode E-Form++源码库的开发团队消息,E-Form++正式提供了同BCGSoft的Ribbon界面风格相结合的示例,如下图: 下载此示例请访问: http ...

  9. DotNetBar 第2课,窗口设置 Ribbon Form 样式

    1. 新增 windows 窗体时,选 Ribbon Form 2. 窗体继承 Office2007RibbonForm 3. 设计窗口下面,删除 删除styleManager1  组件 窗口效果如下 ...

  10. MSCRM 2013/2015 Ribbon Editor

    由于新版本2015的解决方案与之前有变化,因此许多老的Tools已经不能使用,推荐给大家新的Ribbon Editor Tool. 下载地址: http://www.develop1.net/publ ...

随机推荐

  1. PHP(五)session和文件上传初步

  2. .NET基础 (18)特性

    特性1 什么是特性,如何自定义一个特性2 .NET中特性可以在哪些元素上使用3 有哪几种方法可以获知一个元素是否申明某个特性4 一个元素是否可以重复申明同一个特性 特性1 什么是特性,如何自定义一个特 ...

  3. (4)-optXXX方法的使用

    在JSONObject获取value有多种方法,如果key不存在的话,这些方法无一例外的都会抛出异常.如果在线环境抛出异常,就会使出现error页面,影响用户体验,针对这种情况最好是使用optXXX方 ...

  4. EF查询记录

    public void TestMethod1() { , Ids = , Ids = "4,5,6" } }; , , , , , , , }; var query = quer ...

  5. async异步方法

    在C# 中,可以使用asyc+await来完成一个异步方法. async用来标志一个使用了await的方法是非阻塞API,是一个异步方法,就当成一个普通关键字就行了.关键是await,await是配合 ...

  6. API Test Postman接口测试之高级篇2

    API Test  Postman接口测试之高级篇2 一.继承父类的设置: 二.导出及导入: 三.分享文档: 四.发布接口文档: 五.常用脚本: 右边框选的是一些常用的脚本,postman提供的,可以 ...

  7. JAVA异常的最佳工程学实践探索

    此文已由作者占金武授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 先说明一下背景: 项目日志中的Exception会被哨兵统一监控并报警 比较多的项目基于dubbo在做服务化 ...

  8. ClamAV学习【3】——scanmanager函数浏览

    吃饱饭继续浏览Manager.c的scanmanager函数,这个函数的功能吧,暂时理解如下. 接收一个命令行参数(经过处理的optstruct结构指针). 然后根据选项判断文件类型种类,还有一些扫描 ...

  9. Bootstrap框架常用总结

    Bootstrap框架常用标签:    标题标签:<h1>-<h6>        bootstrap中也设置的相同的样式  - 若要使用 必须使用空标签来定义 比如<s ...

  10. CentOS中vsftpd的主动和被动方式

    网址http://blog.csdn.net/nyunyuzhao/article/details/5734978,学习了. FTP是File Transfer Protocol(文件传输协议)的缩写 ...