Ribbon 介绍

Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负载均衡工具,且已集成在 Eureka 依赖中。

1)客户端负载均衡:

  • 负载均衡算法在客户端

  • 客户端维护服务地址列表

2)服务端负载均衡:

  • 负载均衡算法在服务端
  • 由负载均衡器维护服务地址列表

开启客户端负载均衡,简化 RestTemplate 调用

1)在服务调用者的 RestTemplate 配置类上添加注解:

@Configuration
public class RestTemplateConfig { @Bean
@LoadBalanced // 开启客户端负载均衡(默认轮询策略)
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

2)在调用时指定服务名:

package com.controller;

import com.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* 服务调用方
*/
@RestController
@RequestMapping("/order")
public class OrderController { @Autowired
private RestTemplate restTemplate; @GetMapping("/goods/{id}")
public Goods findOrderByGoodsId(@PathVariable("id") int id) { String url = String.format("http://eureka-provider/goods/findOne/%d", id);
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}

负载均衡策略

负载均衡策略:

  • 轮询(默认)
  • 随机
  • 最小并发
  • 过滤
  • 响应时间
  • 轮询重试
  • 性能可用性

使用负载均衡:

方式一:使用 bean 的方式

  • 在消费者端配置负载均衡策略 Bean:
package com.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean; public class MyRule { @Bean
public IRule rule() {
return new RandomRule(); // 随机策略
} }
  • 在启动类添加注解:
package com;

import com.config.MyRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient; @EnableDiscoveryClient // 激活DiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name="eureka-provider", configuration= MyRule.class) // 指定服务提供方并配置负载均衡策略
public class ConsumerApp { public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}

方式二:使用配置文件

server:
port: 9000 eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka spring:
application:
name: eureka-consumer # 设置 Ribbon 的负载均衡策略:随机策略
EUREKA-PROVIDER:
ribbon:
NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule.RandomRule

SpringCloud 客户端负载均衡:Ribbon的更多相关文章

  1. 客户端负载均衡Ribbon

    客户端负载均衡Ribbon 一.Ribbon是什么 二.Ribbon实现客户端负载均衡 三.Ribbon负载均衡策略 四.Rest请求模板类解读 4.1 RestTemplate的GET请求 第一种: ...

  2. SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon

    通过上一章<SpringCloud开发学习总结(三)—— 服务治理Eureka>,我们已经搭建起微服务架构中的核心组件——服务注册中心(包括单点模式和高可用模式).同时还注册了一个服务,命 ...

  3. 【springcloud】客户端负载均衡(Ribbon)

    转自:https://blog.csdn.net/pengjunlee/article/details/86594934 服务器端负载均衡负载均衡是我们处理高并发.缓解网络压力和进行服务器扩容的重要手 ...

  4. 五、springcloud之客户端负载均衡Ribbon

    一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...

  5. 客户端负载均衡Ribbon之二:Loadbalance的源码

    Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 像nginx可以使用负载均衡分配流量,ribbon为客户端提供负载均衡,dubbo服务调用里的负载均衡 ...

  6. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

  7. 0403-服务注册与发现-客户端负载均衡-Ribbon的基本使用

    一.概述 问题1.上一篇文章已说明如何注册微服务,但是调用方如何调用,以及如何防止硬编码.即电影微服务调用用户微服务 问题2.用户微服务多个节点,调用服务方如何负载均衡 二.实现负载均衡方式 2.1. ...

  8. 客户端负载均衡Ribbon之源码解析

    什么是负载均衡器? 假设有一个分布式系统,该系统由在不同计算机上运行的许多服务组成.但是,当用户数量很大时,通常会为服务创建多个副本.每个副本都在另一台计算机上运行.此时,出现 "Load ...

  9. Spring Cloud第四篇 | 客户端负载均衡Ribbon

    ​ 本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: ​Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

随机推荐

  1. table表格做出圆角效果

    采用border-radius 这个属性的时候在chrome里面没有圆角,倒是在IE里面有圆角. 不知道是不是没有写webkie 前缀,但是加上一段神奇的代码overflow:hidden的是时候在c ...

  2. 走进JUC的世界

    概念 同步锁:synchronized.Lock区别 1.synchronized是不需要进行手动解锁 2.synchronized可以锁方法.锁同步代码块 3.synchronized是Java自带 ...

  3. 掌握JavaScript中的迭代器和生成器,顺便了解一下async、await的原理

    掌握JavaScript中的迭代器和生成器,顺便了解一下async.await的原理 前言 相信很多人对迭代器和生成器都不陌生,当提到async和await的原理时,大部分人可能都知道async.aw ...

  4. Dockerfile入门

    1.Dockerfile介绍 在之前Docker的使用中,我们直接从仓库下载需要的镜像到本地,然后稍加配置就可以应用了,通常从仓库下载下来的镜像都是通用的,无任何私有化的东西,我们拿过来就需要加很多的 ...

  5. 在 Docker 上快速运行 Apache Airflow 2.2.4

    Docker 安装 Apache Airflow 参考资料 Running Airflow in Docker 安装依赖 Docker Engine Docker Composite 快速运行 Apa ...

  6. @JsonFormat、@DateTimeFormat、@JsonSerialize注解的使用

    @JsonFormat 是jackson的注解,用于后台返回前台的时候将后台的date类型数据转为string类型格式化显示在前台,加在get方法或者date属性上面,因为 @JsonFormat 注 ...

  7. STL空间分配器源码分析(二)mt_allocator

    一.简介 mt allocator 是一种以2的幂次方字节大小为分配单位的空间配置器,支持多线程和单线程.该配置器灵活可调,性能高. 分配器有三个通用组件:一个描述内存池特性的数据,一个包含该池的策略 ...

  8. 网页跟随系统 dark mode (暗黑模式) 的实现

    经过几十年的沉默, dark mode(暗黑模式) 又回到了我们面前,越来越多的 APP 有了暗黑主题,越来月多的操作系统原生添加了 "全局暗黑模式", 那么一个网站如何跟随系统的 ...

  9. Ubuntu 下 Mariadb 数据库的安装和目录迁移

    Ubuntu 下 Mariadb 数据库的安装和目录迁移 1.简介 本文主要是 Ubuntu 下 Mariadb 数据库的安装和目录迁移,同样适用于 Debian 系统:Ubuntu 20.0.4 M ...

  10. python学习-Day6

    目录 学习内容详述 上期 '猜年龄'练习详解: while循环补充 while + else 死循环与while的嵌套 while嵌套 for循环 range关键字 range用法 什么是爬虫??? ...