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

ASP.NET Core3.1 Ocelot路由的實現(xiàn)

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

1.路由

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

2.Ocelot基礎(chǔ)項目構(gòu)建(APIGatewayBasicDemo)

現(xiàn)在我們根據(jù)GitHub貢獻者開源項目來學(xué)習(xí)Ocelot,根據(jù)下載下來Ocelot基礎(chǔ)項目結(jié)構(gòu)來看,我們能看到有一個網(wǎng)關(guān)項目(APIGateway),一個客戶API項目(CustomersAPIServices),一個產(chǎn)品API項目(ProductsAPIServices)。如下圖所示:

2.1Ocelot網(wǎng)關(guān)配置

APIGateway網(wǎng)關(guān)項目根目錄下面有一個configuration.json配置文件,內(nèi)容如下:

{
 //Routes:處理上游請求的對象(客戶端),每個數(shù)組{}就是配置:上游地址和對應(yīng)下游地址
 "Routes": [
 {
  //以Downstream開頭的,是要轉(zhuǎn)發(fā)到下游服務(wù)器的地址(CustomersAPIServices),與nginx轉(zhuǎn)發(fā)類似
  //下面所有Downstream開頭的,組成一個轉(zhuǎn)發(fā)url,轉(zhuǎn)發(fā)地址是http://localhost:9001/api/customers
  "DownstreamPathTemplate": "/api/customers",
  "DownstreamScheme": "http",
  // "DownstreamHost": "localhost",
  // "DownstreamPort": 9001,
  //轉(zhuǎn)發(fā)到下游服務(wù)器的主機和端口。
  "DownstreamHostAndPorts": [
  {
   "Host": "localhost",
   "Port": 9001
  }
  ],
  //Upstream開頭是指上游服務(wù)器(客戶端)訪問地址,通過http get方式訪問。
  //也就是說客戶端訪問http://localhost:9000/customers 實際是轉(zhuǎn)發(fā)到了http://localhost:9001/api/customers的服務(wù)
  "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特定設(shè)置
 "GlobalConfiguration": {
 "RequestIdKey": "OcRequestId",
 "AdministrationPath": "/administration"
 }
}

下面我們就文件中這些屬性進行解釋:
DownstreamPathTemplate:下游路由服務(wù)地址。
DownstreamScheme:下游服務(wù)地址訪問協(xié)議類型http或者https。
DownstreamHostAndPorts:是一個數(shù)據(jù)集合,用于定義您希望將請求轉(zhuǎn)發(fā)到的任何下游服務(wù)的主機和端口。通常,它僅包含一個條目,但是有時您可能希望對下游服務(wù)進行負載均衡請求,Ocelot允許您添加多個條目,然后選擇一個負載均衡器。
UpstreamPathTemplate:上游服務(wù)地址,即下游服務(wù)真實訪問地址。
UpstreamHttpMethod:上游服務(wù)HTTP請求方式,例如Get、Post。
GlobalConfiguration:顧名思義就是全局配置,此節(jié)點的配置允許覆蓋Routes里面的配置,你可以在這里進行通用的一些配置信息。
在Ocelot中,您可以以{something}的形式將變量的占位符添加到模板中。占位符變量需要同時存在于DownstreamPathTemplate和UpstreamPathTemplate屬性中。當(dāng)設(shè)置為Ocelot時,Ocelot將嘗試為每個請求Ocelot進程將UpstreamPathTemplate占位符中的值替換為DownstreamPathTemplate。例如上述/customers/{id}。

2.2網(wǎng)關(guān)項目中添加Ocelot支持

現(xiàn)在我們在網(wǎng)關(guān)項目中添加Ocelot支持,代碼如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
    //.UseStartup<Startup>()
    //設(shè)置網(wǎng)關(guān)url
    .UseUrls("http://*:9000")
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
     //添加Ocelot配置文件
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
       .AddJsonFile("configuration.json")
       .AddEnvironmentVariables();
    })
    .ConfigureServices(s =>
    {
     //添加Ocelot服務(wù)
     s.AddOcelot();
    })
    .Configure(a =>
    {
     //使用Ocelot中間件
     a.UseOcelot().Wait();
    });

Ocelot的配置如上代碼基本完成了,下面我們看看一個基礎(chǔ)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運行測試

上面這三個下游路由地址根據(jù)configuration.json配置文件都分別配置了上游分發(fā)地址,我們把這三個項目根據(jù)配置信息分別在IIS上部署起來,當(dāng)然你們也可以使用dotnet run命令分別啟動這個三個項目。APIGateway、CustomersAPIServices、ProductsAPIServices項目綁定主機地址分別是http://localhost:9000、http://localhost:9001、http://localhost:9002。
當(dāng)我們在瀏覽器上打開http://localhost:9000/customers時候,會發(fā)現(xiàn)瀏覽器上輸出如下信息:


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


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


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

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

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

相關(guān)文章

最新評論