亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET Core3.1 Ocelot負載均衡的實現(xiàn)

 更新時間:2020年11月12日 11:18:31   作者:暗斷腸  
這篇文章主要介紹了ASP.NET Core3.1 Ocelot負載均衡的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.負載均衡

Ocelot可以在每個路由的可用下游服務中實現(xiàn)負載均衡,這使我們更有效地選擇下游服務來處理請求。負載均衡類型:

  • LeastConnection:根據(jù)服務正在處理請求量的情況來決定哪個服務來處理新請求,即將新請求發(fā)送到具有最少現(xiàn)有請求的服務去處理。算法狀態(tài)沒有分布在Ocelot集群中。
  • RoundRobin:遍歷可用服務并發(fā)送請求。算法狀態(tài)沒有分布在Ocelot集群中。
  • NoLoadBalancer:從配置或服務發(fā)現(xiàn)中獲取第一個可用服務來處理新請求。
  • CookieStickySessions:通過使用Cookie,確保特定的請求能夠被分配到特定的服務上進行處理。

Ocelot負載均衡項目示例中,通過網(wǎng)關項目的路由LoadBalancerOptions選項可以配置負載均衡類型:

{
 "Routes": [
 {
  //下游路由服務地址
  "DownstreamPathTemplate": "/api/values",
  //下游服務地址訪問協(xié)議類型http或者https
  "DownstreamScheme": "http",
  //下游服務的主機和端口
  "DownstreamHostAndPorts": [
  {
   "Host": "localhost",
   "Port": 9001
  },
  {
   "Host": "localhost",
   "Port": 9002
  }
  ],
  //上游服務地址,即下游服務真實訪問地址
  "UpstreamPathTemplate": "/",
  //負載均衡類型:輪詢
  "LoadBalancerOptions": {
  "Type": "RoundRobin"
  },
  //上游服務HTTP請求方式,例如Get、Post
  "UpstreamHttpMethod": [ "Get" ]
 }
 ]
}

新請求通過上游訪問下游服務的時候,Ocelot會根據(jù)LoadBalancerOptions負載均衡選項類型來分發(fā)到具體下游服務。

2.服務發(fā)現(xiàn)

下面展示如何使用服務發(fā)現(xiàn)來設置路由:

{
 "DownstreamPathTemplate": "/api/posts/{postId}",
 "DownstreamScheme": "https",
 "UpstreamPathTemplate": "/posts/{postId}",
 "UpstreamHttpMethod": [ "Put" ],
 "ServiceName": "product",
 "LoadBalancerOptions": {
  "Type": "LeastConnection"
 }
}

設置此選項后,Ocelot將從服務發(fā)現(xiàn)提供程序中查找下游主機和端口,并在所有可用服務中進行負載平衡請求。如果您從服務發(fā)現(xiàn)提供者(領事)中添加和刪除服務,Ocelot會停止調(diào)用已刪除的服務,并開始調(diào)用已添加的服務。后續(xù)學習服務發(fā)現(xiàn)這塊知識點時候會重新再講解。

3.項目演示

3.1APIGateway項目

該項目通過LoadBalancerOptions配置選項定義服務負載均衡請求機制,事例項目使用的負載均衡類型是RoundRobin,在Program添加Ocelot支持代碼如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
 WebHost.CreateDefaultBuilder(args)
   //.UseStartup<Startup>()
   .UseUrls("http://*:9000")
   .ConfigureAppConfiguration((hostingContext, config) =>
  {
   config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
    //添加Ocelot配置文件
.AddJsonFile("configuration.json")
    .AddEnvironmentVariables();
  })
  .ConfigureServices(s =>
  {
   //添加Ocelot服務;
   s.AddOcelot();
  })
  .Configure(a =>
  {
    //使用Ocelot
   a.UseOcelot().Wait();
  });

3.2APIServicesA和APIServicesB下游服務項目

APIServicesA和APIServicesB項目分別新建兩個GET請求方法,代碼分別如下:

//APIServicesA
[Route("api/[controller]")]
public class ValuesController : Controller
{
 // GET api/values
 [HttpGet]
 public string Get()
 {
  return "From APIServiceA";
 }
}
//APIServicesB
[Route("api/[controller]")]
public class ValuesController : Controller
{
 // GET api/values
 [HttpGet]
 public string Get()
 {
  return "From APIServiceB";
 }  
}

通過dotnet run命令啟動APIGateway項目(網(wǎng)關層)

dotnet run --project APIGateway項目路徑\APIGateway.csproj


通過dotnet run命令啟動APIServicesA項目

dotnet run --project APIGateway項目路徑\APIGateway.csproj


通過dotnet run命令啟動APIServicesB項目

dotnet run --project APIServicesB項目路徑\APIServicesB.csproj


通過瀏覽器查看輪詢分發(fā)給下游服務返回的結果:



負載均衡輪詢分發(fā)下游服務成功。

4.自定義負載均衡

Ocelot支持自定義負載均衡的方法。自定義負載均衡的類需要繼承ILoadBalancer接口類,下面我們定義一個簡單的負載均衡循環(huán)輸出下游服務的示例:

public class CustomLoadBalancer : ILoadBalancer
{
 private readonly Func<Task<List<Service>>> _services;
 private readonly object _lock = new object();
 private int _last;

 public CustomLoadBalancer(Func<Task<List<Service>>> services)
 {
  _services = services;
 }
 public async Task<Response<ServiceHostAndPort>> Lease(HttpContext httpContext)
 {
  var services = await _services();
  lock (_lock)
  {
   if (_last >= services.Count)
   {
    _last = 0;
   }
   var next = services[_last];
   _last++;
   return new OkResponse<ServiceHostAndPort>(next.HostAndPort);
  }
 }
 public void Release(ServiceHostAndPort hostAndPort)
 {
 }
}

在Ocelot中注冊此類:

Func<IServiceProvider, DownstreamRoute, IServiceDiscoveryProvider, CustomLoadBalancer> loadBalancerFactoryFunc =
(serviceProvider, Route, serviceDiscoveryProvider) => new CustomLoadBalancer(serviceDiscoveryProvider.Get);
s.AddOcelot().AddCustomLoadBalancer(loadBalancerFactoryFunc);

最后在路由的LoadBalancerOptions配置選項上修改為CustomLoadBalancer自定義負載均衡類名:

"LoadBalancerOptions": {
 "Type": "CustomLoadBalancer"
}

運行項目調(diào)試查看結果:
第一次請求時候分發(fā)到APIServicesA項目。


第二次請求時候分發(fā)到APIServicesB項目。


從上面結果來看,自定義負載均衡成功。

參考文獻:
Ocelot官網(wǎng)

到此這篇關于ASP.NET Core3.1 Ocelot負載均衡的實現(xiàn)的文章就介紹到這了,更多相關ASP.NET Core3.1 Ocelot負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論