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

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

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

1.路由

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

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

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

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

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

{
 //Routes:處理上游請(qǐng)求的對(duì)象(客戶端),每個(gè)數(shù)組{}就是配置:上游地址和對(duì)應(yīng)下游地址
 "Routes": [
 {
  //以Downstream開(kāi)頭的,是要轉(zhuǎn)發(fā)到下游服務(wù)器的地址(CustomersAPIServices),與nginx轉(zhuǎn)發(fā)類似
  //下面所有Downstream開(kāi)頭的,組成一個(gè)轉(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ù)器的主機(jī)和端口。
  "DownstreamHostAndPorts": [
  {
   "Host": "localhost",
   "Port": 9001
  }
  ],
  //Upstream開(kāi)頭是指上游服務(wù)器(客戶端)訪問(wèn)地址,通過(guò)http get方式訪問(wèn)。
  //也就是說(shuō)客戶端訪問(wèn)http://localhost:9000/customers 實(shí)際是轉(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"
 }
}

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

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

現(xiàn)在我們?cè)诰W(wǎng)關(guān)項(xiàng)目中添加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的配置如上代碼基本完成了,下面我們看看一個(gè)基礎(chǔ)Ocelot路由正常工作流程。

2.3CustomersAPIServices和ProductsAPIServices項(xiàng)目

CustomersAPIServices項(xiàng)目的CustomersController有如下兩個(gè)方法:

[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項(xiàng)目的ProductsController有如下一個(gè)方法:

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

2.4運(yùn)行測(cè)試

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


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


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


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

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

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

相關(guān)文章

最新評(píng)論