欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.路由

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

2.Ocelot基礎項目構建(APIGatewayBasicDemo)

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

2.1Ocelot網關配置

APIGateway網關項目根目錄下面有一個configuration.json配置文件,內容如下:

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

下面我們就文件中這些屬性進行解釋:
DownstreamPathTemplate:下游路由服務地址。
DownstreamScheme:下游服務地址訪問協(xié)議類型http或者https。
DownstreamHostAndPorts:是一個數據集合,用于定義您希望將請求轉發(fā)到的任何下游服務的主機和端口。通常,它僅包含一個條目,但是有時您可能希望對下游服務進行負載均衡請求,Ocelot允許您添加多個條目,然后選擇一個負載均衡器。
UpstreamPathTemplate:上游服務地址,即下游服務真實訪問地址。
UpstreamHttpMethod:上游服務HTTP請求方式,例如Get、Post。
GlobalConfiguration:顧名思義就是全局配置,此節(jié)點的配置允許覆蓋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配置文件都分別配置了上游分發(fā)地址,我們把這三個項目根據配置信息分別在IIS上部署起來,當然你們也可以使用dotnet run命令分別啟動這個三個項目。APIGateway、CustomersAPIServices、ProductsAPIServices項目綁定主機地址分別是http://localhost:9000、http://localhost:9001、http://localhost:9002。
當我們在瀏覽器上打開http://localhost:9000/customers時候,會發(fā)現瀏覽器上輸出如下信息:


為什么輸入網關主機地址,返回過來卻是客戶主機處理結果?那是因為當客戶端訪問上游服務http://localhost:9000/customers時候,Ocelot會根據配置信息中下游模版把請求分發(fā)到http://localhost:9001/api/Customers/Get去處理,然后返回結果。
而當我們打開http://localhost:9000/customers/1時候,也會輸出如下信息:


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


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

參考文獻:
Ocelot官網

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

相關文章

最新評論