ASP.NET Core Api网关Ocelot初探

网络 通信技术
Ocelot面向使用.NET运行微型服务/面向服务的体系结构的人员,这些体系结构需要在系统中具有统一的入口点。特别是我想与IdentityServer参考和承载令牌轻松集成。Ocelot是按特定顺序排列的一堆中间件。

[[387094]]

本文转载自微信公众号「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"falsetrue); 
            }) 
                .ConfigureWebHostDefaults(webBuilder => 
                { 
                    webBuilder.UseStartup<Startup>(); 
                }); 
    } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

5、Startup文件配置

services.AddOcelot(Configuration); 
 
app.UseOcelot().Wait(); 
  • 1.
  • 2.
  • 3.

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" ] 
    } 
  ] 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

7、2个项目运行,测试

代码地址

https://gitee.com/conanOpenSource_admin/Example/commit/b3b5a6b15a060b46c5ecd2ea31f0d36791cda18c

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2009-08-03 14:22:33

什么是ASP.NET

2021-01-13 07:33:41

API数据安全

2018-08-20 08:03:46

跨平台 Web操作系统

2024-09-09 07:37:51

AspJWT权限

2024-06-11 09:00:00

异步编程代码

2025-01-15 00:01:00

开发应用界面

2021-10-19 10:42:00

MVCAPI.NET

2024-09-10 08:13:16

Asp项目轻量级

2021-02-19 06:54:33

配置系统ASP.NET Cor

2024-07-02 08:45:08

2024-12-05 08:14:41

2010-03-10 09:35:18

ASP.NET缓存

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache缓存框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-04 11:10:29

容器化Docker虚拟机

2021-02-03 13:35:25

ASPweb程序

2021-01-31 22:56:50

FromServiceASP

2021-01-28 22:39:35

LoggerMessa开源框架

2025-01-10 00:41:38

版本控制API
点赞
收藏

51CTO技术栈公众号