1. 程式人生 > 程式設計 >ASP.NET Core3.1 Ocelot路由的實現

ASP.NET Core3.1 Ocelot路由的實現

1.路由

前一個章節我們已經介紹過Ocelot,相信大家也瞭解到,Ocelot的主要功能是接收客戶端等傳入的HTTP請求,並將其轉發到下游服務。Ocelot當前僅以另一個http請求的形式支援此功能(將來可能是任何傳輸機制)。
Ocelot將一個請求路由到另一個請求。為了讓Ocelot正常工作,您需要在配置中設定一個Route。下面我們就Ocelot基礎專案構建簡單介紹下路由功能。

2.Ocelot基礎專案構建(APIGatewayBasicDemo)

現在我們根據GitHub貢獻者開源專案來學習Ocelot,根據下載下來Ocelot基礎專案結構來看,我們能看到有一個閘道器專案(APIGateway),一個客戶API專案(CustomersAPIServices),一個產品API專案(ProductsAPIServices)。如下圖所示:

ASP.NET Core3.1 Ocelot路由的實現

2.1Ocelot閘道器配置

APIGateway閘道器專案根目錄下面有一個configuration.json配置檔案,內容如下:

{
 //Routes:處理上游請求的物件(客戶端),每個陣列{}就是配置:上游地址和對應下游地址
 "Routes": [
 {
  //以Downstream開頭的,是要轉發到下游伺服器的地址(CustomersAPIServices),與nginx轉發類似
  //下面所有Downstream開頭的,組成一個轉發url,轉發地址是http://localhost:9001/api/customers
  "DownstreamPathTemplate": "/api/customers","DownstreamScheme": "http",// "DownstreamHost": "localhost",// "DownstreamPort": 9001,//轉發到下游伺服器的主機和埠。
  "DownstreamHostAndPorts": [
  {
   "Host": "localhost","Port": 9001
  }
  ],//Upstream開頭是指上游伺服器(客戶端)訪問地址,通過http get方式訪問。
  //也就是說客戶端訪問http://localhost:9000/customers 實際是轉發到了http://localhost:9001/api/customers的服務
  "UpstreamPathTemplate": "/customers","UpstreamHttpMethod": [ "Get" ]
 },{
  "DownstreamPathTemplate": "/api/customers/{id}","DownstreamHostAndPorts": [
  {
   "Host": "localhost","UpstreamPathTemplate": "/customers/{id}",{
  "DownstreamPathTemplate": "/api/products",// "DownstreamPort": 9002,"Port": 9002
  }
  ],"UpstreamPathTemplate": "/products","UpstreamHttpMethod": [ "Get" ]
 }
 ],//全域性配置,允許覆蓋Routes特定設定
 "GlobalConfiguration": {
 "RequestIdKey": "OcRequestId","AdministrationPath": "/administration"
 }
}

下面我們就檔案中這些屬性進行解釋:
DownstreamPathTemplate:下游路由服務地址。
DownstreamScheme:下游服務地址訪問協議型別http或者https。
DownstreamHostAndPorts:是一個數據集合,用於定義您希望將請求轉發到的任何下游服務的主機和埠。通常,它僅包含一個條目,但是有時您可能希望對下游服務進行負載均衡請求,Ocelot允許您新增多個條目,然後選擇一個負載均衡器。
UpstreamPathTemplate:上游服務地址,即下游服務真實訪問地址。
UpstreamHttpMethod:上游服務HTTP請求方式,例如Get、Post。

GlobalConfiguration:顧名思義就是全域性配置,此節點的配置允許覆蓋Routes裡面的配置,你可以在這裡進行通用的一些配置資訊。
在Ocelot中,您可以以{something}的形式將變數的佔位符新增到模板中。佔位符變數需要同時存在於DownstreamPathTemplate和UpstreamPathTemplate屬性中。當設定為Ocelot時,Ocelot將嘗試為每個請求Ocelot程序將UpstreamPathTemplate佔位符中的值替換為DownstreamPathTemplate。例如上述/customers/{id}。

2.2閘道器專案中新增Ocelot支援

現在我們在閘道器專案中新增Ocelot支援,程式碼如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
    //.UseStartup<Startup>()
    //設定閘道器url
    .UseUrls("http://*:9000")
    .ConfigureAppConfiguration((hostingContext,config) =>
    {
     //新增Ocelot配置檔案
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
       .AddJsonFile("configuration.json")
       .AddEnvironmentVariables();
    })
    .ConfigureServices(s =>
    {
     //新增Ocelot服務
     s.AddOcelot();
    })
    .Configure(a =>
    {
     //使用Ocelot中介軟體
     a.UseOcelot().Wait();
    });

Ocelot的配置如上程式碼基本完成了,下面我們看看一個基礎Ocelot路由正常工作流程。

2.3CustomersAPIServices和ProductsAPIServices專案

CustomersAPIServices專案的CustomersController有如下兩個方法:

[Route("api/[controller]")]
public class CustomersController : Controller
{  
 [HttpGet]
 public IEnumerable<string> Get()
 {
  return new string[] { "Catcher Wong","James Li" };
 }

 [HttpGet("{id}")]
 public string Get(int id)
 {
  return $"Catcher Wong - {id}";
 }   
}

ProductsAPIServices專案的ProductsController有如下一個方法:

[Route("api/[controller]")]
public class ProductsController : Controller
{
 [HttpGet]
 public IEnumerable<string> Get()
 {
  return new string[] { "Surface Book 2","Mac Book Pro" };
 }
}

2.4執行測試

上面這三個下游路由地址根據configuration.json配置檔案都分別配置了上游分發地址,我們把這三個專案根據配置資訊分別在IIS上部署起來,當然你們也可以使用dotnet run命令分別啟動這個三個專案。APIGateway、CustomersAPIServices、ProductsAPIServices專案繫結主機地址分別是http://localhost:9000、http://localhost:9001、http://localhost:9002。
當我們在瀏覽器上開啟http://localhost:9000/customers時候,會發現瀏覽器上輸出如下資訊:

ASP.NET Core3.1 Ocelot路由的實現

為什麼輸入閘道器主機地址,返回過來卻是客戶主機處理結果?那是因為當客戶端訪問上游服務http://localhost:9000/customers時候,Ocelot會根據配置資訊中下游模版把請求分發到http://localhost:9001/api/Customers/Get去處理,然後返回結果。
而當我們開啟http://localhost:9000/customers/1時候,也會輸出如下資訊:

ASP.NET Core3.1 Ocelot路由的實現

配置資訊中上游模版/customers/{id}對應下游模版/api/customers/{id},當我們請求的路徑為http://localhost:9000/customers/1時候,Ocelot會根據配置資訊分發到對應的下游路由http://localhost:9001/api/Customers/Get/1去處理,然後返回結果。
同理,當客戶端訪問上游服務http://localhost:9000/products時候,Ocelot也會分發到對應的下游路由http://localhost:9002/api/Products去處理,然後返回結果:

ASP.NET Core3.1 Ocelot路由的實現

根據上面測試結果,也就是說我們的Ocelot已經在起作用了,而且根據上下游路由進行了對映。當然該章節也只是簡單介紹Ocelot路由功能,而configuration.json配置中有些屬性還沒有進行描述,例如負載均衡、限流,熔斷等等。下面我會繼續根據GitHub貢獻者開源專案繼續講解其功能。

參考文獻:
Ocelot官網

到此這篇關於ASP.NET Core3.1 Ocelot路由的實現的文章就介紹到這了,更多相關ASP.NET Core Ocelot路由內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!