Ribbon 使用入门
Ribbon 是 Netflix 下的负载均衡项目,在集群中为各个客户端的通信提供支持,主要实现中间层应用程序的负载均衡,提供以下特性:
- 负载均衡器,可支持插拔式的负载均衡规则
- 对多种协议提供支持,例如HTTP、TCP
- 集成了负载均衡功能的客户端
Ribbon 可以与 Eureka 服务注册中心整合使用,并且被整合到 Spring Cloud Netflix 子项目中。Ribbon 主要有三大模块,模块说明如下:
- ribbon-core:该模块为Ribbon项目的核心,主要包括负载均衡器接口定义、客户端接口定义、内置的负载均衡实现等API
- ribbon-eureka:为 Eureka 客户端提供的负载君均衡实现类
- ribbon-httpclient:对 Apache 的 HttpClient 进行封装,该模块提供了含有负载均衡功能的 REST 客户端
Ribbon 的负载均衡器主要是与集群中的各个服务器进行通信,因此负载均衡器需要提供维护服务器IP、DNS名称等信息、根据特定逻辑在服务器列表中循环的功能,为了实现这些功能 Ribbon 的负载均衡器提供了以下三大模块:
- Rule:该组件用于处理从服务器列表中选择那个服务器实例
- Ping:该组件主要用定时器来确保服务器网络可以连接
- ServerList:服务器列表,可以通过静态的配置确定负载的服务器,也可以动态指定服务器列表,如果动态指定服务器列表,则会有后台线程来刷新该列表
在使用 Ribbon 时,可以使用配置也可以使用代码设置配置项,需要使用代码来设置配置项,示例代码如下:
ConfigurationManager.getConfigInstance()
.setProperty("MyRibbonClient.ribbon.listOfServers","localhost:8080,localhost:8002");
如果希望使用配置文件来设置项,在 src/main/resources 目录创建 properties 文件,并在文件中增加配置项,格式如下:
<client>.<namespace>.<property>=<value>
其中,<client> 表示为客户的名称,声明配置属于那个客户端,在使用 ClientFactory 时可传入客户端的名称来获取对应的实例;<namespace>为该配置的命名空间,默认为 ribbon;<property>为属性名;<value>为属性值;如果希望对全部客户端生效,则配置格式如下:
<namespace>.<property>=<value>
Ribbon使用示例
- 创建项目
创建 maven 项目,命名为 ribbon-client,并增加 ribbon 依赖,POM.xml 内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.lixue</groupId>
<artifactId>ribbon-client</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
- 增加配置文件
在 src/main/resources 目录创建 ribbon-client.properties 文件,并增加配置项,配置如下:
MyRibbonClient.ribbon.listOfServers=localhost:8080,localhost:8002
- 增加测试类
package org.lixue.ribbon.client;
import com.netflix.client.ClientFactory;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;
public class RibbonClient{
public static void main(String[]args)throwsException{
ConfigurationManager.loadCascadedPropertiesFromResources("ribbon-client");
ILoadBalancerloadBalancer=ClientFactory.getNamedLoadBalancer("MyRibbonClient");
Irule chooseRule=new RoundRobinRule();
chooseRule.setLoadBalancer(loadBalancer);
for(inti=0;i<10;i++){
Server server=chooseRule.choose(null);
System.out.println("request"+server.getHostPort());
}
}
}
- 测试验证
启动项目,我们代码获取了10次的服务器,地址分别为 localhost:8080 和 localhost:8002 ,通过输出结果可以发现,使用了轮询调度算法来选择服务器,输出结果如下:
request localhost:8002
request localhost:8080
request localhost:8002
request localhost:8080
request localhost:8002
request localhost:8080
request localhost:8002
request localhost:8080
request localhost:8002
request localhost:8080
Ribbon 使用入门的更多相关文章
- Ribbon【入门】
公共依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...
- SpringCloud-客户端的负载均衡Ribbon(三)
前言:微服务架构,不可避免的存在单个微服务有多个实例,那么客户端如何将请求分摊到多个微服务的实例上呢?这里我们就需要使用负载均衡了 一.Ribbon简介 Ribbon是Netflix发布的负载均衡器, ...
- springcloud高级
第一章 负载均衡 Ribbon (Spring Cloud 高级) 一. Ribbon 在微服务中的作用 1 什么是 Ribbon 1.Ribbon 是一个基于 Http 和 TCP ...
- Spring Cloud Ribbon入门
一.简介 Spring Cloud Ribbon是一个基于Http和TCP的客户端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署,但是它 ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- VS2010/MFC编程入门之五十二(Ribbon界面开发:创建Ribbon样式的应用程序框架)
上一节中鸡啄米讲了GDI对象之画刷CBrush,至此图形图像的入门知识就讲完了.从本节开始鸡啄米将为大家带来Ribbon界面开发的有关内容.本文先来说说如何创建Ribbon样式的应用程序框架. Rib ...
- Spring Cloud 入门 之 Ribbon 篇(二)
原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...
- 从实例一步一步入门学习SpringCloud的Eureka、Ribbon、Feign、熔断器、Zuul的简单使用(附代码下载)
场景 SpringCloud -创建统一的依赖管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 Sprin ...
随机推荐
- JAVA拼合数组方法
方法一: package org.ken.array; import java.lang.reflect.Array; import java.util.Arrays; public class Jo ...
- Symbol -- JavaScript 语言的第七种数据类型
ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种机制,保证 ...
- input默认显示当前时间
方法一: // 获取当天的年月日 new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().get ...
- HDU1548- A strange lift (BFS入门)
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A Strrange lift Time Limit: 2000/1000 MS (Java/ ...
- 微软Power BI 每月功能更新系列——9月Power BI 新功能学习
Power BI Desktop 9月新功能摘要 Power BI 9月更新如期而至,这一次Power BI 又推出了新功能——聚合预览,它可在内存中无缝地存储汇总值,大大提高报告的性能.另外本月还包 ...
- nodejs的express框架
介绍: Express是由路由和中间件构成一个的nodejs的一种web应用框架; 功能: 可以设置中间件来响应 HTTP 请求. 定义了路由表用于执行不同的 HTTP 请求动作. 可以通过向模板传递 ...
- jQuery.Deferred exception: $.get is not a function TypeError: $.get is not a function
/********************************************************************** * jQuery.Deferred exception: ...
- [LeetCode&Python] Problem 242. Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- Js中的判空
1.JS 中判断 undefined JavaScript 中有两个特殊数据类型:undefined 和 null var test= undefined; if (typeof(test) == u ...
- linux lamp编译环境安装
apache 安装:http://blog.csdn.net/wplblog/article/details/52172128 编译安装 mysql安装:http://www.centoscn.com ...