前一個章節咱們已經介紹過Ocelot,相信你們也瞭解到,Ocelot的主要功能是接收客戶端等傳入的HTTP請求,並將其轉發到下游服務。Ocelot當前僅以另外一個http請求的形式支持此功能(未來多是任何傳輸機制)。
Ocelot將一個請求路由到另外一個請求。爲了讓Ocelot正常工做,您須要在配置中設置一個Route。下面咱們就Ocelot基礎項目構建簡單介紹下路由功能。html
如今咱們根據GitHub貢獻者開源項目來學習Ocelot,根據下載下來Ocelot基礎項目結構來看,咱們能看到有一個網關項目(APIGateway),一個客戶API項目(CustomersAPIServices),一個產品API項目(ProductsAPIServices)。以下圖所示:nginx
APIGateway網關項目根目錄下面有一個configuration.json配置文件,內容以下:git
{ //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}", "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:下游服務地址訪問協議類型http或者https。
DownstreamHostAndPorts:是一個數據集合,用於定義您但願將請求轉發到的任何下游服務的主機和端口。一般,它僅包含一個條目,可是有時您可能但願對下游服務進行負載均衡請求,Ocelot容許您添加多個條目,而後選擇一個負載均衡器。
UpstreamPathTemplate:上游服務地址,即下游服務真實訪問地址。
UpstreamHttpMethod:上游服務HTTP請求方式,例如Get、Post。
GlobalConfiguration:顧名思義就是全局配置,此節點的配置容許覆蓋Routes裏面的配置,你能夠在這裏進行通用的一些配置信息。
在Ocelot中,您能夠以{something}的形式將變量的佔位符添加到模板中。佔位符變量須要同時存在於DownstreamPathTemplate和UpstreamPathTemplate屬性中。當設置爲Ocelot時,Ocelot將嘗試爲每一個請求Ocelot進程將UpstreamPathTemplate佔位符中的值替換爲DownstreamPathTemplate。例如上述/customers/{id}。github
如今咱們在網關項目中添加Ocelot支持,代碼以下:json
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路由正常工做流程。api
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" }; } }
上面這三個下游路由地址根據configuration.json配置文件都分別配置了上游分發地址,咱們把這三個項目根據配置信息分別在IIS上部署起來,固然大家也能夠使用dotnet run命令分別啓動這個三個項目。APIGateway、CustomersAPIServices、ProductsAPIServices項目綁定主機地址分別是http://localhost:9000、http://localhost:900一、http://localhost:9002。
當咱們在瀏覽器上打開http://localhost:9000/customers時候,會發現瀏覽器上輸出以下信息:服務器
爲何輸入網關主機地址,返回過來倒是客戶主機處理結果?那是由於當客戶端訪問上游服務http://localhost:9000/customers時候,Ocelot會根據配置信息中下游模版把請求分發到http://localhost:9001/api/Customers/Get去處理,而後返回結果。
而當咱們打開http://localhost:9000/customers/1時候,也會輸出以下信息:
配置信息中上游模版/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去處理,而後返回結果:
根據上面測試結果,也就是說咱們的Ocelot已經在起做用了,並且根據上下游路由進行了映射。固然該章節也只是簡單介紹Ocelot路由功能,而configuration.json配置中有些屬性尚未進行描述,例如負載均衡、限流,熔斷等等。下面我會繼續根據GitHub貢獻者開源項目繼續講解其功能。
參考文獻:
Ocelot官網負載均衡