经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Ribbon 框架简介及搭建(没有与SpringCloud整合,独立使用)
来源:cnblogs  作者:沛昕的博客  时间:2018/10/19 9:24:11  对本文有异议

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

 

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

 

 

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

 

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

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <version>1.5.7.RELEASE</version>
  6. </dependency>
  7. </dependencies>

 

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

  1. public class Person {
  2. private String url;// 处理请求的服务器url
  3. private String message;// 提示信息
  4. public String getUrl() {
  5. return url;
  6. }
  7. public void setUrl(String url) {
  8. this.url = url;
  9. }
  10. public String getMessage() {
  11. return message;
  12. }
  13. public void setMessage(String message) {
  14. this.message = message;
  15. }
  16. }

 

PersonController.java

  1. @RestController
  2. public class PersonController {
  3. @RequestMapping(value="/getPerson", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  4. public Person getPerson(HttpServletRequest request){
  5. Person p = new Person();
  6. p.setMessage("请求成功");
  7. p.setUrl(request.getRequestURL().toString());
  8. return p;
  9. }
  10. }

 

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

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. String port = scan.nextLine();
  6. new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args);
  7. }
  8. }

 

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

 

2:创建Ribbon客户端

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

  1. <dependency>
  2. <groupId>com.netflix.ribbon</groupId>
  3. <artifactId>ribbon-core</artifactId>
  4. <version>2.2.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.netflix.ribbon</groupId>
  8. <artifactId>ribbon-httpclient</artifactId>
  9. <version>2.2.5</version>
  10. </dependency>

 

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

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

 

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

  1. 服务列表:[localhost:8080, localhost:8081]
  2. {"url":"http://localhost:8081/getPerson","message":"请求成功"}
  3. {"url":"http://localhost:8080/getPerson","message":"请求成功"}
  4. {"url":"http://localhost:8081/getPerson","message":"请求成功"}
  5. {"url":"http://localhost:8080/getPerson","message":"请求成功"}
  6. {"url":"http://localhost:8081/getPerson","message":"请求成功"}
  7. {"url":"http://localhost:8080/getPerson","message":"请求成功"}
  8. {"url":"http://localhost:8081/getPerson","message":"请求成功"}
  9. {"url":"http://localhost:8080/getPerson","message":"请求成功"}
  10. {"url":"http://localhost:8081/getPerson","message":"请求成功"}
  11. {"url":"http://localhost:8080/getPerson","message":"请求成功"}

 

 

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号