Ribbon 框架简介及搭建

news/2024/7/3 10:02:26

Ribbon简介

1. 负载均衡框架,支持可插拔式的负载均衡规则

2. 支持多种协议,如HTTP、UDP等

3. 提供负载均衡客户端

Ribbon子模块

1. ribbon-core(ribbon的核心,主要包含负载均衡器、负载均衡接口、客户端接口、内置负载均衡实现API)

2. ribbon-eureka(为eureka客户端提供的客户端实现类)

3. ribbon-httpclient(为负载均衡提供了REST客户端)

负载均衡器组件

1. 一个负载均衡器,至少提供以下功能

1.1 要维护各个服务器的IP等信息

1.2 根据特定逻辑选取服务器

2. 为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块

2.1 Rule

2.2 Ping

2.3 ServerList

由于本次的教程是没有与SpringCloud整合的,是用来单独使用的,下面就教大家怎么搭建Ribbon程序 并 调用服务。

1:创建Ribbon服务器(一个单纯的SpringBoot程序)

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.7.RELEASE</version>
    </dependency>
</dependencies>
复制代码

为了方便Ribbon客户端测试,在这里建一个实体类:Person.java

public class Person {
    private String url;// 处理请求的服务器url
    private String message;// 提示信息
    
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
复制代码

PersonController.java

@RestController
public class PersonController {

    @RequestMapping(value="/getPerson", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(HttpServletRequest request){
        Person p = new Person();
        p.setMessage("请求成功");
        p.setUrl(request.getRequestURL().toString());
        return p;
    }
}
复制代码

启动类:Application.java(因为要测试负载均衡,所有这里需要启动多个服务,以下配置以手动输入端口号方式启动)

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String port = scan.nextLine();
        new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args);
    }
}
复制代码

本次启动以端口:8080、8081分别启动,稍后我们配置完客户端 统一测试(配置后,将服务启动)

2:创建Ribbon客户端

pom.xml 中只需要引入核心及客户端的依赖即可

<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon-core</artifactId>
    <version>2.2.5</version>
</dependency>
<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon-httpclient</artifactId>
    <version>2.2.5</version>
</dependency>
复制代码

编写main方法测试,调用服务(服务列表也可以直接在配置文件中配置,本次用setProperty进行配置)其中的my-client,这个名称可以任意起,因为这个名称是用来命名配置创建客户端的。

public static void main(String[] args) {
    try {
        // 写入服务列表
        ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
        // 输出服务列表
        System.out.println("服务列表:" + ConfigurationManager.getConfigInstance().getProperty("my-client.ribbon.listOfServers"));
        // 获取客户端(如果获取不到,可通过getNamedClient方法自动创建)
        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        // 创建request对象
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/getPerson")).build();// 写入将要访问的接口
        // 多次访问测试
        for (int i = 0; i < 10; i++) {
            // 创建response对象
            HttpResponse response = client.executeWithLoadBalancer(request);
            // 接收请求结果
            String json = response.getEntity(String.class);
            // 打印结果
            System.out.println(json);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
复制代码

以上就是Ribbon单独使用的全部过程,下面大家看一下Ribbon负载均衡默认的轮询规则

服务列表:[localhost:8080, localhost:8081]
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}
{"url":"http://localhost:8081/getPerson","message":"请求成功"}
{"url":"http://localhost:8080/getPerson","message":"请求成功"}复制代码



http://www.niftyadmin.cn/n/4821462.html

相关文章

硬盘分区表详解

硬盘主引导扇区 硬盘主引导记录&#xff08;MBR&#xff09; 硬盘分区表&#xff08;DPT&#xff09; -------------------------------------------------------------- 物理位置&#xff1a;0面0道1扇区&#xff08;clindyer 0, side 0, sector 1) 大小&#xff1a; 512字节…

(原創) 數學就是loose coupling的極致表現 (OO)

Abstract我們已經在STL中看到loose coupling的威力了&#xff0c;但我今天發現&#xff0c;數學更是loose coupling最佳的詮釋。Introduction從小學到大學&#xff0c;哪些科目用處最大?英文和數學&#xff0c;英文不難理解&#xff0c;因為他是語言&#xff0c;任何知識都以英…

Visual Studio 2010 and the .NET Framework 4.0!

下一站:Visual Studio 2010 和 .NET Framework 4.0 REDMOND, Wash. — Sept. 29, 2008 — Microsoft Corp. today provided the first look at the next version of its developer tools and platform, which will be named Visual Studio 2010 and the .NET Framework 4.0. M…

CentOS基本的命令与快捷建

由于我的计算机在安装linux系统时&#xff0c;计算机出现了问题&#xff0c;并没有安装ubuntu而是安装的centos。虽然两者属于linux的不同版本&#xff0c;但是在具体的操作上大同小异。在学习linux的各种指令和快捷键的时候&#xff0c;难免会遇到各种各样的问题&#xff0c;以…

Delphi使用zlib来压缩文件

使用时&#xff0c;需要Zlib.pas和 Zlibconst.pas两个单元文件&#xff0c;这两个文件保存在 Delphi 5.0安装光盘上 InfoExtrasZlib目录下&#xff0c;此外&#xff0c;在 InfoExtrasZlibObj目录中还保存了 Zlib.pas单元引用的 Obj文件&#xff0c;把这个目录拷贝到delphi的lib…

Linq 动态查询库

【原文地址】Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library) 【原文发表日期】 Monday, January 07, 2008 11:02 PM LINQ &#xff08;语言级集成查询&#xff09;是VS 2008 和 .NET 3.5中提供的一个新特性。LINQ使得数据查询的概念成为.NET中的一等编程概念&a…

CentOS下screen 命令详解

一、背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器&#xff0c;经常运行一些需要很长时间才能完成的任务&#xff0c;比如系统备份、ftp 传输等等。通常情况下我们都是为每一个这样的任务开一个远程终端窗口&#xff0c;因为它们执行的时间太长了。必须等待它…

javascript的fn方法(转)

jQuery为开发插件提拱了两个方法&#xff0c;分别是&#xff1a; jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。 jQuery.fn.extend(object);给jQuery对象添加方法。 fn 是什么东西呢。查看jQuery代码&#…