SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(3)-- Ribbon负载均衡服务调用

╰+攻爆jí腚メ 2022-12-28 14:17 250阅读 0赞

上一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(2)— 服务注册与发现

一、Ribbon负载均衡服务调用

1、概述

①、是什么

在这里插入图片描述

②、官网资料

https://github.com/Netflix/ribbon/wiki/Getting-Started
Ribbon目前也进入维护模式
在这里插入图片描述
未来替换方案
在这里插入图片描述

③、能干嘛

LB(负载均衡)
在这里插入图片描述
集中式LB
在这里插入图片描述
进程内LB
在这里插入图片描述
前面我们讲解过了80通过轮询负载访问8001/8002
一句话:负载均衡+RestTemplate调用

2、Ribbon负载均衡演示

①、架构说明

在这里插入图片描述
总结:Ribbon其实就是一个软负载均衡的客户端组件,他可以和其他所需请求的客户端结合使用,和eureka结合只是其中的一个实例。

②、pom

在这里插入图片描述
在这里插入图片描述

③、二说RestTemplate的使用

官网
https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html

在这里插入图片描述
getForObject方法/getForEntity方法
在这里插入图片描述
postForObject/postForEntity
在这里插入图片描述
GET请求方法
POST请求方法

3、Ribbon核心组件IRule

①、IRule:根据特定算法从服务列表中选取一个要访问的服务

在这里插入图片描述

在这里插入图片描述

②、如何替换

1)、修改cloud-consumer-order80
2)、注意配置细节、
在这里插入图片描述
3)、新建package(com.atguigu.myrule)
在这里插入图片描述
4)、上面包下新建MySelfRule规则类

  1. package com.atguigu.myrule;
  2. import com.netflix.loadbalancer.IRule;
  3. import com.netflix.loadbalancer.RandomRule;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class MySelfRule {
  8. @Bean
  9. public IRule myRule(){
  10. return new RandomRule();//定义为随机
  11. }
  12. }

5)、主启动类添加@RibbonClient

  1. package com.atguigu.springcloud;
  2. import com.atguigu.myrule.MySelfRule;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.ribbon.RibbonClient;
  7. @EnableEurekaClient
  8. @SpringBootApplication
  9. @RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
  10. public class OrderMain80 {
  11. public static void main(String[] args) {
  12. SpringApplication.run(OrderMain80.class,args);
  13. }
  14. }

6)、测试
http://localhost/consumer/payment/get/31

4、Ribbon负载均衡算法

①、原理

在这里插入图片描述

②、RoundRobinRule源码
③、自己试着写一个本地负载均衡器试试

下一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(4)— OpenFeign服务接口调用

发表评论

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

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

相关阅读