什么是Ribbon,ribbon有什么用,个人先总结一下(不正确请提出讨论):Ribbon是基于客户端的负载均衡器,为我们提供了多样的负载均衡的方案,比如轮询,最小的并发请求的server,随机server等;其默认的策略是ZoneAvoidanceRule,也就是复合判断server所在区域的性能和server的可用性选择server,使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。

在上面两篇博文中我们介绍了Eureka的使用和相关的博文教程。在上一篇博文中(https://blog.csdn.net/asd529735325/article/details/85044158),在我们服务调用方,我们就用到了Ribbon,我们使用Ribbon的RestTemplate,其整合了EurekaClient,Ribbon基于客户端的负载均衡器为我们提供了多样的负载均衡的功能,我们所需要做的就是在Spring中注册一个RestTemplate,并且添加@LoadBalanced 注解。(上一篇我们添加的是LoadBalanced,默认ZoneAvoidanceRule策略)。

这里我们详细实验一下Ribbon如何实现的负载均衡。

https://github.com/wades2/EurekaDemo2这个代码的基础上我们添加了一个Client端,也是作为server实例的。

为了区分是最终究竟访问的哪个EurekaClient,我们的Cotroller返回的和第一个有差异(但是实际开发中不可以!我们这里只是为了实验!!强调!!)。

我们按照之前注册到服务端一样把新的Client注册到Eurekaserver,以下是我个人的配置文件:

  1. spring.application.name=Eureka-client
  2. #eureka.client.allow-redirects=false
  3. #修改启动端口
  4. server.port=8085
  5. eureka.client.serviceUrl.defaultZone=http://user:123456@localhost:8083/eureka,http://user:123456@localhost:8082/eureka
  6. #eureka.client.register-with-eureka=true
  7. eureka.instance.ip-address=true

注意:spring.application.name必须和EurekaClient的一致,因为restTemplate是通过spring.application.name+请求接口地址来实现请求的类似于httpClient的框架(可以理解成域名或IP+端口号就能实现请求,只是在Ribbon中换了个形式而已)。

  1. package com.example.demo.controller;
  2. import com.netflix.appinfo.InstanceInfo;
  3. import com.netflix.discovery.EurekaClient;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("user")
  10. public class UserController {
  11. @Autowired
  12. private EurekaClient eurekaClients;
  13. // @Autowired
  14. // private DiscoveryClient discoveryClient;
  15. @GetMapping("getUser")
  16. public String getUser() {
  17. //todo 得到eureka server的服务实例
  18. // InstanceInfo info=eurekaClients.getNextServerFromEureka("Eureka-client",false);
  19. // return info.getHomePageUrl();
  20. return "hello client2";
  21. }
  22. }

Controller除了返回的信息不一样,其他都一样。

然后启动访问http://localhost:8086/getUser/routine,我们可以看到,请求同样的url,返回的一次是hello client2,一次是hello one,我们可以知道,客户端的负载均衡已经完成了。

接下来我们来说说如何根据自己的需求配置负载均衡策略,我们刚在前文提到Ribbon默认的策略是ZoneAvoidanceRule,我们通过实验来试试其他策略:

在Eurakacaller中Controller下new一新的Class作为config,来配置我们的策略。

然后重构下Controller(因为之前是把Config直接写在Controller里面的):

  1. package com.example.demo.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import org.springframework.web.client.RestTemplate;
  6. @RestController
  7. //@Configuration
  8. public class Controller {
  9. // @Bean
  10. // @LoadBalanced
  11. // public RestTemplate getRestTemplate() {
  12. // return new RestTemplate();
  13. // }
  14. //
  15. // @GetMapping("getUser/routine")
  16. // public String routine() {
  17. // RestTemplate restTemplate = getRestTemplate();
  18. //
  19. // String json = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
  20. // return json;
  21. // }
  22. @Autowired
  23. private RestTemplate restTemplate;
  24. @GetMapping("getUser/routine")
  25. public String routine() {
  26. // RestTemplate restTemplate = configBean.getRestTemplate();
  27. String json = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
  28. return json;
  29. }
  30. // @GetMapping("getUser/routine2")
  31. // public String routine2() {
  32. // RestTemplate restTemplate = getRestTemplate();
  33. // String json = restTemplate.getForObject("http://Eureka-client/user/getUser2", String.class);
  34. // return json;
  35. // }
  36. }

然后配置下我们的Ribbon策略:

  1. package com.example.demo.controller;
  2. import com.netflix.loadbalancer.IRule;
  3. import com.netflix.loadbalancer.RandomRule;
  4. import com.netflix.loadbalancer.RoundRobinRule;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.web.client.RestTemplate;
  9. @Configuration
  10. public class ConfigBean {
  11. @Bean
  12. @LoadBalanced // ribbon负载均衡
  13. public RestTemplate getRestTemplate() {
  14. return new RestTemplate();
  15. }
  16. @Bean
  17. public IRule muRule() {
  18. return new RoundRobinRule();//轮询
  19. // return new RandomRule();// 随机
  20. }
  21. }

选择轮询还是随机看你,如果注销了muRule,则变为默认策略。

OK,有关Ribbon的配置就结束了,欢迎大家一起讨论评论。

这里推荐一个对Ribbon有深入研究大佬的博客地址,看了后会明白很多,Ribbon是如何通过restTemplate来发送http请求调用的:https://blog.csdn.net/puhaiyang/article/details/79682177

Spring-cloud之Ribbon负载均衡的使用及负载均衡策略配置(与Eurka配合使用)的更多相关文章

  1. spring cloud 使用ribbon简单处理客户端负载均衡

    假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...

  2. spring cloud: 关闭ribbon负载均衡

    spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...

  3. spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略

    spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...

  4. Spring Cloud微服务笔记(四)客户端负载均衡:Spring Cloud Ribbon

    客户端负载均衡:Spring Cloud Ribbon 一.负载均衡概念 负载均衡在系统架构中是一个非常重要,并且是不得不去实施的内容.因为负载均衡对系统的高可用性. 网络压力的缓解和处理能力的扩容的 ...

  5. Spring Cloud Gateway Ribbon 自定义负载均衡

    在微服务开发中,使用Spring Cloud Gateway做为服务的网关,网关后面启动N个业务服务.但是有这样一个需求,同一个用户的操作,有时候需要保证顺序性,如果使用默认负载均衡策略,同一个用户的 ...

  6. Spring Cloud - 切换Ribbon的负载均衡模式

    Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模 ...

  7. spring cloud 通过 ribbon 实现客户端请求的负载均衡(入门级)

    项目结构 环境: idea:2020.1 版 jdk:8 maven:3.6.2 1. 搭建项目 ( 1 )父工程:spring_cloud_demo_parent pom 文件 <?xml v ...

  8. Spring cloud 之Ribbon(二)负载均衡原理

    ribbon实现负载均衡的原理 我们从Ribbon实现负载均衡的代码可以看到,Ribbon是通过RestTemPlate实现客户端负载均衡的,准确的说是RestTemPlate上的@LoadBalan ...

  9. Spring Cloud 之 Ribbon

    新建Spring Boot工程,命名为ribbon 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"? ...

随机推荐

  1. Codeforces Round #538 (Div. 2)D(区间DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){    int n ...

  2. js中的DOM对象 和 jQuery对象 比较

    一,二者的区别 通过 jQuery 获取的元素是一个数组,数组中包含着原生JS中的DOM对象. 总结:jQuery 就是把 DOM 对象重新包装了一下,让其具有了 jQuery 方法. 二,二者的相互 ...

  3. 关于javascript数据存储机制的一个案例。

    之前在学习js的结合性的时候,我有点不太明白,在网上找到一个比较经典的C语言优先级结合性的案例,就是下边这一个.本想在js之中测试一番,结果竟然发现得出的结果和网上的不一样,这令我百思不得其解,后经高 ...

  4. 资深专家深度剖析Kubernetes API Server第1章(共3章)

    欢迎来到深入学习Kubernetes API Server的系列文章,在本系列文章中我们将深入的探究Kubernetes API Server的相关实现.如果你对Kubernetes的内部实现机制比较 ...

  5. Laplace(拉普拉斯)算子

    [摘要] Laplace算子作为边缘检测之一,和Sobel算子一样也是工程数学中常用的一种积分变换,属于空间锐化滤波操作.拉普拉斯算子(Laplace Operator)是n维欧几里德空间中的一个二阶 ...

  6. 新建maven 父子模块项目

    第一步: 第二步: 先创建个简单的空架结构作为父项目 第三步: 创建子项目 第四步: 切换显示不同的maven子项目显示方式 关于maven中的parent聚合一直都有没好好总结,固有这篇. ---- ...

  7. freemarker macro 使用

    转载... macro, nested, return语法 <#macro name param1 param2 ... paramN>...<#nested loopvar1, l ...

  8. CPU和微架构的概念

    CPU是什么: 中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Unit). 它的功能主要 ...

  9. LINUX下用PHPIZE安装PHP GD扩展

    环境:LNMP in centOS 6.4. linux下PHP的扩展可以用phpize的方法,比较简单地进行启用. 以下以PHP-GD2 库安装为例子. sudo yum install php-g ...

  10. getElementsByTagName 、 getElementsByName 、getElementById区别

    WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问DOCUMENT中的任一个标签: getEle ...