本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。
概述
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
人脸解锁扫脸支付随着人脸识别技术的不断发展,如今借助一个小小的摄像头就能让...
5G网络建设加快,超前布局6G 截止目前,我国累计建成的5G基站数量超过71.8万座,...
5G切片是新商业模式的关键推动者,也是增强5G潜力的关键概念。通信服务提供商可...
1.终有那么一个人,可以随时改变着你的心情。 2.有的东西你再喜欢也不会属於你...
近年来,因高空抛物、坠物造成的伤害事件屡上报端。水瓶、西瓜皮、易拉罐,甚至...
逛个动物园要指纹打卡,连回家进小区也要刷脸验明正身会议期间,记者在浙江代表...
整个欧洲向智能建筑迈进的步伐正在加快。随着各行各业的组织在客户和员工体验方...
1.总有一天,我们会过上我一翻身就可以偷亲你的日子。 2.即使一贫如洗,我会是...
3月15日消息 一年一度的央视财经 3.15 晚会正在进行中,从前言来看主要曝光问题...
iOS 11~iOS 14.3的越狱工具发布了un0ver6.0.0版本 支持iOS11-iOS 14.3系统设备进...