当前位置:网站首页>(2)ASP.NET Core3.1 Ocelot路由
(2)ASP.NET Core3.1 Ocelot路由
2020-11-06 20:13:37 【itread01】
1.路由
前一個章節我們已經介紹過Ocelot,相信大家也瞭解到,Ocelot的主要功能是接收客戶端等傳入的HTTP請求,並將其轉發到下游服務。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開頭的,是要轉發到下游伺服器的地址(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}。
2.2Core專案中新增Ocelot支援
現在我們在core專案中新增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(); s.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }) .Configure(a => { //使用Ocelot中介軟體 a.UseOcelot().Wait(); });
Ocelot的配置如上程式碼基本完成了,下面我們看看一個基礎Ocelot路由正常工作流程。
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:9001、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官網
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://www.itread01.com/content/1604649902.html
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World