ASP.NET Core Api网关Ocelot的使用初探
概述
Ocelot面向使用.NET运行微型服务/面向服务的体系结构的人员,这些体系结构需要在系统中具有统一的入口点。特别是我想与IdentityServer参考和承载令牌轻松集成。Ocelot是按特定顺序排列的一堆中间件。Ocelot将HttpRequest对象操作到由其配置指定的状态,直到到达请求构建器中间件,在该中间件中它创建一个HttpRequestMessage对象,该对象用于向下游服务发出请求。发出请求的中间件是Ocelot管道中的最后一件事。它不会调用下一个中间件。有一块中间件可将HttpResponseMessage映射到HttpResponse对象,然后将其返回给客户端。基本上,它具有许多其他功能。
代码实现
1、新建api客户端1
2、新建api 网关test
3、nuget安装Ocelot
4、Program文件添加ConfigureAppConfiguration
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(conf => { conf.AddJsonFile("ocelot.json", false, true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
5、Startup文件配置
services.AddOcelot(Configuration);
app.UseOcelot().Wait();
6、网关项目下添加文件ocelot.json
{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/WeatherForecast/GetList", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/GetList", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/{everything}", "UpstreamHttpMethod": [ "Post" ] }, { "DownstreamPathTemplate": "/api/WeatherForecast/GetModel?id={s1}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/GetModel?id={s1}", "UpstreamHttpMethod": [ "Get" ] } ] }
7、2个项目运行,测试
代码地址
https://gitee.com/conanOpenSource_admin/Example/commit/b3b5a6b15a060b46c5ecd2ea31f0d36791cda18c
以上就是ASP.NET Core Api网关Ocelot的使用初探的详细内容,更多关于ASP.NET Core Api网关Ocelot的资料请关注脚本之家其它相关文章!
相关文章
Entity Framework使用Code First模式管理数据库
本文详细讲解了Entity Framework使用Code First模式管理数据库的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-03-03ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
我们就要让这些文件上传框动态添加,下面我以我做的一个图库管理中的上传图片的功能为例2011-10-10Javascript 直接调用服务器C#代码 ASP.NET Ajax实例
近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。2010-03-03
最新评论