SpringCloud_Ribbon负载均衡调用

爱被打了一巴掌 2023-07-24 05:56 29阅读 0赞

文章目录

  • 概述
    • 负载均衡(Load Balance)是什么
    • Ribbon客户端本地负载均衡 VS Nginx服务端负载均衡
  • Eureka+Ribbon+RestTemplate使用
  • 核心组件IRule
    • 规则替换
  • 轮询算法原理
  • 参考Demo

概述

SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端复杂均衡的工具。

简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时、重试等。简单地说,就是在配置文件中列出Load Balancer所有的机器,Ribbon会自动的帮助你基于某种规则(如轮序、随机等)去连接这些机器。我们同样很容易使用Ribbon实现自定义的负载均衡算法。

Ribbon目前已经进入维护模式
在这里插入图片描述
SpringCloud提供的替换方案是LoadBalancer
在这里插入图片描述

负载均衡(Load Balance)是什么

简单地说,就是将用户的请求平摊到多个服务上,从而到达系统的HA(高可用)。常见的负载均衡软件有Nginx,LVS,硬件F5等。

Ribbon客户端本地负载均衡 VS Nginx服务端负载均衡

Ribbon本地负载均衡,在调用微服务接口时,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。

Nginx服务器负载均衡,客户端所有请求都会交给Nginx,然后由Nginx实现转发请求。

Eureka+Ribbon+RestTemplate使用

  1. pom


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

Eureka依赖中已经包含了Ribbon的依赖。

  1. yaml

    server:
    port: 80

    spring:

    1. application:
    2. name: cloud-order-service

    eureka:
    client:

    1. #表示是否将自己注册进EurekaServer默认为true。
    2. register-with-eureka: false
    3. #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    4. fetchRegistry: true
    5. service-url:
    6. #单机
    7. #defaultZone: http://localhost:7001/eureka
    8. # 集群
    9. defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
  2. 主启动类

    @SpringBootApplication
    @EnableEurekaClient
    public class OrderMain80
    {

    1. public static void main(String[] args) {
    2. SpringApplication.run(OrderMain80.class, args);
    3. }

    }

  3. @LoadBalancer

    @Configuration
    public class ApplicationContextConfig
    {

    1. @Bean
    2. @LoadBalanced
    3. public RestTemplate getRestTemplate()
    4. {
    5. return new RestTemplate();
    6. }

    }

  4. RestTemplate

    //返回的对象为响应体中数据转化成的对象,基本上可以理解为JSON
    @GetMapping(“/consumer/payment/create”)
    public CommonResult create(Payment payment)
    {

    1. return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);

    }

    //返回的对象为响应体中数据转化成的对象,基本上可以理解为JSON
    @GetMapping(“/consumer/payment/get/{id}”)
    public CommonResult getPayment(@PathVariable(“id”) Long id)
    {

    1. return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);

    }

    //返回对象为ResponseEntity,包含了响应中的一些重要信息,响应头,响应状态码,响应体等
    @GetMapping(“/consumer/payment/getForEntity/{id}”)
    public CommonResult getPayment2(@PathVariable(“id”) Long id)
    {

    1. ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    2. if(entity.getStatusCode().is2xxSuccessful()){
    3. return entity.getBody();
    4. }else{
    5. return new CommonResult<>(444,"操作失败");
    6. }

    }

核心组件IRule

根据特定算法从服务列表中选择一个要访问的服务
在这里插入图片描述

  • com.netflix.loadbalancer.RoundRobinRule 轮询
  • com.netflix.loadbalancer.RandomRule 随机
  • com.netflix.loadbalancer.RetryRule 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内进行重试,获取可用的服务
  • WeightedResponseTimeRule 对RoundRobinRule的扩展,响应速度越快的实例选择权重越多大,越容易被选择
  • BestAvailableRule 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
  • AvailabilityFilteringRule 先过滤掉故障实例,再选择并发较小的实例
  • ZoneAvoidanceRule 复合判断server所在区域的性能和server的可用性选择服务器

规则替换

注意!自定义配置类不能放在@ComponentScan所扫描的当前包以及子包下,否则自定义的配置类就会被所有Ribbon客户端共享,不能达到定值的目的。

与SpringCloud包并行创建一个包,新建MySelfRule规则类

  1. @Configuration
  2. public class MySelfRule
  3. {
  4. @Bean
  5. public IRule myRule()
  6. {
  7. return new RandomRule();//定义为随机
  8. }
  9. }

主启动类添加@RibbonClient

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration=MySelfRule.class)
  4. public class OrderMain80
  5. {
  6. public static void main(String[] args) {
  7. SpringApplication.run(OrderMain80.class, args);
  8. }
  9. }

轮询算法原理

在这里插入图片描述

参考Demo

https://github.com/zzyybs/atguigu\_spirngcloud2020

发表评论

表情:
评论列表 (有 0 条评论,29人围观)

还没有评论,来说两句吧...

相关阅读

    相关 负载均衡

    定义 > 负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外供应效力而无需其他服务器的辅助。经过某种负载分管技术,将外部发送

    相关 负载均衡

    一、什么是负载均衡(Load balancing) 在网站创立初期,我们一般都使用单台机器对台提供集中式服务,但是随着业务量越来越大,无论是性能上还是稳定性上都有了更大的