当前位置: 首页 > news >正文

Ribbon:负载均衡及Ribbon

什么是负载均衡?

第一种轮询算法,依次遍历去执行,达到负载均衡 

 

 

 

 集成Ribbon

导入pom,在消费者服务里的pom文件导入

  <!-- Ribbon 集成      --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId><version>1.4.6.RELEASE</version></dependency>

还要导入Eureka客户端

        <!--    Eureka    --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.4.6.RELEASE</version></dependency>

然后消费者服务里的yml

server:port: 80#Eureka 配置
eureka:client:register-with-eureka: false #不向Eureka注册自己service-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

主启动类加上注解

package com.kuang.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer80 {public static void main(String[] args) {SpringApplication.run(DeptConsumer80.class,args);}
}

在配置类上加一个注解,即可实现Ribbon的负载均衡的RestTemplate

package com.kuang.springcloud.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration//
public class ConfigBean {//配置负载均衡实现RestTemplate,只需要加个注解@LoadBalanced ,它去注册中心去请求的时候,会平均分请求每个,eureka中的注册的服务,@Bean@LoadBalanced //Ribbon 基于这个注解实现的public RestTemplate restTemplate(){return new RestTemplate();}}

在Controller访问那里别写死了,改成 服务名,因为服务在三个注册中心Eureka上面都注册了,但是都是同一个服务名,三个服务器相互绑定,你只需要该地址为服务名,他会用负载均衡算法来计算出下一个将要访问的服务器

package com.kuang.springcloud.controller;import com.kuang.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
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;import java.util.List;@RestController
public class DeptConsumerController {//理解:消费者,不应该有Service层//模板有很多方法供我们去使用//注入过来
//(String url, 实体:map, Class<T> responseType, Object... uriVariables)
//(url,实体,map,Class<T> responseType 返回值类型)  三个参数  五种请求方式@Autowiredprivate RestTemplate restTemplate;
//Ribbon 我们这里的地址,应该是一个变量,通过服务名字来访问// private static final String REST_URL_PREFIX="http://localhost:8001";private static final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";@RequestMapping("/consumer/dept/get/{id}")public Dept get(@PathVariable("id")Long id){return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);}@RequestMapping("/consumer/dept/add")public boolean add(Dept dept){return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);}@RequestMapping("/consumer/dept/list")public List<Dept> list(){return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);}}

 默认是随机路由

实现的是这个接口

 

 

 得出来一个重大结论

Ribbon和Eureka整合以后,客户端可以直接调用不用关心 ip地址和端口号~

再来总结一下

Eureka :就是注册中心,其实也是一个服务,给服务提供一个注册的容器,让服务可以注册进来供消费者使用,它可以多个注册中心组合在一起,变成一个集群,在每个注册中心上,服务提供者都把自己的服务放到注册中心上,只要还有一个服务器没有瘫痪,它照旧可以运行,它属于AP可用性与容错性一体的架构。

Ribbon:是让多个注册中心,相互调用,相当于一个整体,在每个注册中心上,服务提供者都会注册一个它的服务,服务名称都一样,所以整合以后,只需要调用服务名称,不用关心 ip地址和端口号~ 即可访问服务,会通过负载均衡算法进行选择调用哪个 Eureka。

http://www.lryc.cn/news/132005.html

相关文章:

  • 【声波】声波在硼酸、硫酸镁 (MgSO4) 和纯水中的吸收研究(Matlab代码实现)
  • 使用swoole实现实时消息推送给客户端
  • Ordinals 之后,以太坊铭文协议 Ethscriptions 如何再塑 NFT 资产形态
  • Python绘制爱心代码(七夕限定版)
  • Java两整数相除向上取整
  • Linux学习之Telnet明文漏洞
  • 产品经理如何提高用户画像效果?SIKT模型
  • ubuntu安装Microsoft Edge并设置为中文
  • Host/ KVM/ Docker/ K8s/ OpenStack/ Mesos简单介绍和区别
  • 关于Transformer中的位置编码
  • ABAP 期初库存批量导入 demo1
  • 想用 Python 写游戏,都有哪些好用的游戏开发库?
  • vue3 路由缓存问题
  • 如何找到一个数的所有质因数,以及如何快速判断一个数是不是质数
  • 西瓜书之神经网络
  • C++进阶 特殊类的设计
  • NLP序列标注问题,样本不均衡怎么解决?
  • 大端和小端
  • C++快速回顾(二)
  • 【LVS】1、LVS负载均衡群集
  • el-tree 懒加载树
  • 到江西赣州ibm维修服务器之旅-联想X3850 x6黄灯故障
  • VMware 虚拟机三种网络模式详解
  • ASP.NET指定变量数据类型,速度提高了100倍
  • PyArmor 一键加密
  • redis--持久化
  • 管理外部表
  • 数字图像处理-AWB跳变
  • DNNGP、DeepGS 和 DLGWAS模型构成对比
  • postgresSQL 配置文件设置