SpringCloud微服务

SpringCloud 负载平衡

Netflix Ribbon

Netflix Ribbon是 Netflix开源软件的一部分(Netflix OSS)。它是一个提供 客户端负载均衡的云库。由于它是Netflix家族的成员,它会自动与 Netflix Service Discovery (Eureka)进行交互。
Ribbon主要提供客户端负载平衡算法。它是客户端负载平衡器,可控制 HTTP TCP 客户端的行为。重要的一点是,当我们使用 Feign 时, Ribbon 也适用。

Ribbon的功能

负载平衡 容错 异步模型中的多种协议支持 缓存和批处理

模块

ribbon: 这是一个集成了负载平衡,容错,缓存的API ribbon-loadbalancer: 这是一个负载平衡器API,可以单独使用或与其他模块一起使用。 ribbon eureka 它使用 Eureka 客户端,该客户端为Spring Cloud提供了动态服务器列表。 ribbon-transport: 这是支持 HTTP,TCP, UDP 的传输客户端,这些协议使用RxNetty 具有负载平衡功能。 ribbon-httpclient: 这是一个基于Apache HttpClient并与负载均衡器集成的REST客户端。 ribbon-core: 这是一个客户端配置API。

负载均衡类型:

负载均衡有两种类型
服务器端负载平衡: 服务器端负载平衡是整体式,它适用于客户端和服务器之间。它接受传入的网络,应用程序流量,并使用各种方法在多个后端服务器之间分配流量。中间组件负责将客户端请求分发到服务器。 客户端负载平衡: 客户端保存服务器IP列表,以便可以传递请求。客户端从列表中随机选择一个IP,然后将请求转发到服务器。
让我们在项目中配置功能区服务器。
步骤1: 转到项目 currency-conversion-service
步骤2: 打开 pom.xml 文件,并添加 功能区依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
添加依赖项后,我们需要在代理上启用功能区。
步骤3: 打开 CurrencyExchangeServiceProxy.java 文件。通过添加注释 @RibbonClient 启用 Ribbon ,并指定我们要与之通话的服务的名称。功能区客户端为客户端提供声明式配置。
@RibbonClient(name="currency-exchange-service")
CurrencyExchangeServiceProxy.java
package com.lidihuo.microservices.currencyconversionservice;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
//@FeignClient(name="currency-exchange-service", url="localhost:8000")
//Enabling feign
@FeignClient(name="currency-exchange-service")
//enabling ribbon
@RibbonClient(name="currency-exchange-service")
public interface CurrencyExchangeServiceProxy 
{
@GetMapping("/currency-exchange/from/{from}/to/{to}")      //where {from} and {to} are path variable
public CurrencyConversionBean retrieveExchangeValue(@PathVariable("from") String from, @PathVariable("to") String to); //from map to USD and to map to INR
}
步骤4: 在注释 @FeignClient 中,删除属性 URL 。因为我们不需要与一种特定的服务进行交谈。我们将在 application.properties 文件中配置该URL。
步骤5: 打开以下网址的 application.properties 文件: 项目 currency-conversion-service 并配置服务器。我们必须配置的属性是:
name-of-the-application.ribbon.listOfServers=URLs
我们已经配置了我们要调用的两个currency-exchange-service实例。
currency-exchange-service.ribbon.listOfServers=http://localhost:8000, http://localhost:8001
application.properties
spring.application.name=currency-conversion-service
server.port=8100
currency-exchange-service.ribbon.listOfServers=http://localhost:8000, http://localhost:8001

使用功能区运行客户端负载平衡

我们有两个 CurrentlyExchangeServiceApplication.java 实例,如下图所示:
使用Ribbon进行客户端负载平衡
首先,在端口 8000 上运行CurrencyExchangeServiceApplication,然后在端口 8001 上运行CurrencyExchangeServiceApplication。
在两个端口上都运行CurrencyExchangeServiceApplication之后,请运行 CurrencyConversionServiceApplication.java 通过发送请求 http://localhost:8100/currency -converter-feign/from/EUR/to/INR/quantity/10000 。它返回以下响应。
使用Ribbon的客户端负载均衡
在上图中,端口8000表示currency-exchange-service在端口8000上运行并正在处理当前请求。
现在,刷新页面。除了端口号和数量外,我们得到了相同的响应,因为我们已更改了请求中的数量。
使用功能区进行客户端负载平衡
在上图中,端口8001表示货币兑换服务正在端口8001上运行并正在处理当前请求。
让我们通过图形来了解负载平衡:
客户端功能区上的负载平衡
在上图中,功能区在三个活动的CurrencyExchangeServices之间分配负载。 CurrencyExchangeService1 在端口 8000上运行, CurrencyExchangeService2 在端口 8001上运行,依此类推。因此,使用Ribbon通过CurrencyCalculationService进行的任何调用都将分配给这三个服务。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4