Ribbon 框架简介及搭建(没有与SpringCloud整合,独立使用)

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 根据特定逻辑选取服务器

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

2.1 Rule

2.2 Ping

2.3 ServerList

至于负载均衡的机制和规则,笔者会在下一章的 “Ribbon负载均衡机制” 中进行讲解,那么在这里就不多说了。

在这里插入图片描述

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

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

pom.xml

org.springframework.boot spring-boot-starter-web 1.5.7.RELEASE

为了方便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 中只需要引入核心及客户端的依赖即可

com.netflix.ribbon ribbon-core 2.2.5 com.netflix.ribbon ribbon-httpclient 2.2.5 编写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负载均衡默认的轮询规则
给大家推荐一个Java交流群:874811168 进群即可免费获取资料

服务列表:[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://mp.toutiao.com/preview_article/?pgc_id=6613679746268004867

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值