学而时习之,不亦说乎!

                             --《论语》

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是什么?的更多相关文章

  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. Python中的排序方法sort(),sorted(),argsort()等

    python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...

  2. java如何集成支付宝移动快捷支付功能

    项目需要,需要在客户端集成支付宝接口.第一次集成,过程还是挺简单的,不过由于支付宝官方文档写的不够清晰,也是走了一些弯路,下面把过程写出来分享给大家.就研究了一下:因为使用支付宝接口,就需要到支付宝官 ...

  3. iconv用法解读

    iconv是一个字符集转换函数,原型为: size_t iconv(iconv_t cd,              char **inbuf, size_t *inbytesleft,       ...

  4. 用Swift实现一款天气预报APP(三)

    这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 通过前面的学习,一个天气预报的APP已经基本可用了.至少 ...

  5. Codeforces788A Functions again 2017-04-12 18:22 56人阅读 评论(0) 收藏

    C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. 试题 F: 特别数的和 第十届蓝桥杯

    试题 F: 特别数的和时间限制: 1.0s 内存限制: 512.0MB 本题总分: 15 分[问题描述]小明对数位中含有 2. 0. 1. 9 的数字很感兴趣(不包括前导 0),在 1 到40 中这样 ...

  7. leancloud 云引擎

    可以部署网站的云端,云代码的升级版.

  8. URAL 1996 Cipher Message 3 (FFT + KMP)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :给出两个串A , B,每个串是若干个byt ...

  9. iOS9 Https技术预研

    一.服务器需要做的事情: 1.要注意 App Transport Security 要求 TLS 1.2, 2.而且它要求站点使用支持forward secrecy协议的密码. 3.证书也要求是符合A ...

  10. [leetcode] 10. Symmetric Tree

    这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...