基础
在本文中,你将学会基于asp.net core 用Ocelot搭建一个简单的API网关。或许你会问:什么是API网关?我们先看下面的截图:
上面的截图可以帮助你清楚地理解它。
API网关是访问我们系统的入口,它包括很多东西,比如路由(Routing),身份验证(Authentication),服务发现(Service discovery),日志(Logging),等等。
Ocelot
Ocelot的目标用户是使用.NET运行微服务/面向服务的体系结构的开发者,他们需要一个统一的入口来访问他们的系统。您可以访问Ocelot获得更多信息。
我会用一个简单的例子来演示如何使用Ocelot。
让我们开始吧!
Step1
首先创建三个项目
项目名称 |
项目类型 |
描述 |
APIGateway |
ASP.NET Core Empty |
Demo的入口 |
CustomersAPIServices |
ASP.NET Core Web API |
API Service顾客相关操作 |
ProductsAPIServices |
ASP.NET Core Web API |
API Service 商品相关操作 |
Step2
首先完成两个API服务(webapi项目)。在CustimersAPIServices项目中创建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}"; } }
为了确定CustimersAPIServices的应用URL,我们应该在项目的类中添加UseUrls
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("http://localhost:9001") .Build();
在PriductsAPIServices项目中新建ProductsController
[Route("api/[controller]")] public class ProductsController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Surface Book 2", "Mac Book Pro" }; } }
同样在项目的类中添加UseUrls
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseUrls("http://localhost:9002") .Build();
注意
你可以通过项目的属性对应用的URL进行配置。
Step3
运行CustimersAPIServices和ProductsAPIServices。打开两个cmd终端,cd到两个服务的文件夹位置,输入"dotnet run" 启动两个项目。
运行成功如下所示。
Step4
接下来我们新建APIGateway项目,首先安装Ocelot安装包。
Install-Package Ocelot
安装成功后,如下图所示。
Step5
在项目下新建configuration.json如下所示。
{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/customers", "DownstreamScheme": "http", "DownstreamHost": "localhost", "DownstreamPort": 9001, "UpstreamPathTemplate": "/customers", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/customers/{id}", "DownstreamScheme": "http", "DownstreamHost": "localhost", "DownstreamPort": 9001, "UpstreamPathTemplate": "/customers/{id}", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/products", "DownstreamScheme": "http", "DownstreamPort": 9002, "DownstreamHost": "localhost", "UpstreamPathTemplate": "/api/products", "UpstreamHttpMethod": [ "Get" ] } ], "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "AdministrationPath": "/administration" } }
这个文件就是API网关的配置文件,由两部分构成——一系列ReRoutes和GlobalConfiguration配置。
ReRoutes是告诉Ocelot如何操作上游的request请求,
GlobalConfiguration有点黑客的感觉,允许对ReRoutes的设置进行重写。
用下面的片段介绍ReRoutes。
{ "DownstreamPathTemplate": "/api/customers/{id}", "DownstreamScheme": "http", "DownstreamHost": "localhost", "DownstreamPort": 9001, "UpstreamPathTemplate": "/customers/{id}", "UpstreamHttpMethod": [ "Get" ] }
以Downstream开头的项意味我们的请求会指向http://localhost:9001/api/customers/{id}
以Upstream开头的项意味我们应该使用/customers/{id} 的HTTP Get请求去访问服务。
Step6
修改Startup类,使用Ocelot。
public class Startup { public Startup(IHostingEnvironment env) { var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder(); builder.SetBasePath(env.ContentRootPath) //add configuration.json .AddJsonFile("configuration.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } // change public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { Action<ConfigurationBuilderCachePart> settings = (x) => { x.WithMicrosoftLogging(log => {log.AddConsole(LogLevel.Debug)}).WithDictionaryHandle()};services.AddOcelot(Configuration,settings)} // don't use Task here public async void Configure(IApplicationBuilder app, IHostingEnvironment env) { await app.UseOcelot();}
别忘了添加上面的configuration.json文件。
Step7
这一步至关重要,用来配置Ocelot。
我们新建IWebHostBuilder的新实例,不要使用var!!!
public class Program { public static void Main(string[] args) { IWebHostBuilder builder = new WebHostBuilder(); builder.ConfigureServices(s => { s.AddSingleton(builder); }); builder.UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .UseUrls("http://localhost:9000"); var host = builder.Build(); host.Run(); } }
同样我们需要指明应用的URL。
Step8
启动APIGateway,使用cmd通过dotnet run 命令。启动成功后,输入http://localhost:9000
当我们通过客户端访问http://localhost:9000/api/products,真实的路由是http://localhost:9002/api/products。
当我们访问<http://localhost:9000/customers,响应结果来源于http://localhost:9001/api/customers
当我们访问http://localhost:9000/customers/1, 返回的结果其实来源于http://localhost:9001/api/customers/1。
以下是我放在Github上的源码。
总结
本文介绍了通过Ocelot搭建API网关。希望对你有所帮助!
顺便说一句,由于只是简单示例。许多重要的特性没有涉及,比如服务发现,身份验证,服务质量(qos)。