.NetCore Ocelot

Open source API Gateway - Ocelot

蘇漢祥 2019/12/24 16:39:37
3513

概要:

 API Gateway 主要工作是將所有內部服務封裝起來,並只提供單一入口而達到管理與監控的工作。每個來自客戶端的請求都需經過這單一入口上的路由資訊,一一對應到內部的各項服務。並且可依需求實作授權、負載均衡、快取 等相關功能。

 

      Ocelot 是一個透過 .NET Core 實作且開源的API Gateway 它的功能相大強大 其中包括了:路由、請求聚合、服務發現、認證、鑑權、限流熔斷、並內建了負載均衡器與Service FabricButterfly Tracing 整合。

 

簡易功能實作:

 

1.建立 Product 與 Payment 等 API

   Env:Product API  http://localhost:6001

 

   Env:Payment API http://localhost:6002

 

2.建立 Ocelot 專案

 Env:https://localhost:5001/

  一.透過 Nuget 安裝 Package Ocelot

 

  二.建立Ocelot.json 檔案,並加入專案中

 

  三.設定並啓用Ocelot功能

 Program.cs -> CreateHostBuilder

 

 Startup.cs -> ConfigureServices

 

 Startup.cs -> Configure

 

測試:

Product API 測試

Payment API 測試

Ocelot API Gateway 測試

Payment API 

 

Product API

 

參考資源:

1. Ocelot Documentationhttps://ocelot.readthedocs.io/en/latest/

2. Microsoft 使用 Ocelot實作 API Gatewayhttps://docs.microsoft.com/zh-tw/dotnet/architecture/microservices/multi-container-microservice-net-applications/implement-api-gateways-with-ocelot

蘇漢祥