学而时习之,不亦说乎!

                             --《论语》

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. Spring3.x错误----java.lang.ClassNotFoundException:org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

    Spring3.x错误: 解决方法: 缺少aspectjweaver.jar包 下载地址: https://cn.jarfire.org/aspectjweaver.html

  2. input中的disabled、readonly和hidden

    最近开发项目的时候,遇到一个问题,就是我希望某个input中的值不能被修改,刚开始的时候,我想到的是disabled属性!但是,发现表单提交后,值无法传递过来! 解决方法: 可以设置其readonly ...

  3. CYUSB3014芯片使用EEPROM无法下载固件说明

    当使用128KB的EEPROM存储CYUSB3014芯片的固件时,需要注意,不同厂家的EEPROM存储器,其A0.A1.A2功能不一样,在设计时电路也不一样.Microchip对应的128KB的EEP ...

  4. Scala程序编译运行

    1.编译 Scala演示代码如下: <pre name="code" class="plain">/** * @author Administrat ...

  5. DELPHI如何获取某目录下的所有文件名?

    //=====================================================================// 函数名称: FindPathFiles// 功能描述 ...

  6. pro1

    #include<iostream> using namespace std; int main(void) { int i,a[],sum; cin>>i; for(i=0; ...

  7. 关于EF的一点小记录

    今日新闻:朝鲜要改革开放了!!!比你牛逼的人都在努力,你还有理由懒惰吗? 宇宙强大IDE配套的EF问题记录 今天做数据添加时,Id我设置为int类型了,结果在做Add操作时报的错让我摸不着头脑,后来问 ...

  8. SQL查询速度慢的原因分析和解决方案

    SQL查询速度慢的原因分析和解决方案 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建 ...

  9. Mycat SqlServer Do not have slave connection to use, use master connection instead

    Do not have slave connection to use, use master connection instead 很奇怪啊 都是按照配置配置的 怎么就是不通呢 有点怀疑人生了吧 其 ...

  10. sharepoint 2013 补丁升级步骤

    1. 安装过程合理: A. 可以同时在管理中心.两台前端.搜索服务器上安装重新发布的SP1补丁包(所提供的链接) B. 等待所有SP1补丁包安装完成,依次在管理中心.两台前端.搜索服务器上运行配置向导 ...