服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - ASP.NET Core Api网关Ocelot的使用初探

ASP.NET Core Api网关Ocelot的使用初探

2021-12-09 14:59UP技术控 ASP.NET教程

这篇文章主要介绍了ASP.NET Core Api网关Ocelot的使用初探,帮助大家更好的理解和学习使用.NET技术,感兴趣的朋友可以了解下

概述

Ocelot面向使用.NET运行微型服务/面向服务的体系结构的人员,这些体系结构需要在系统中具有统一的入口点。特别是我想与IdentityServer参考和承载令牌轻松集成。Ocelot是按特定顺序排列的一堆中间件。Ocelot将HttpRequest对象操作到由其配置指定的状态,直到到达请求构建器中间件,在该中间件中它创建一个HttpRequestMessage对象,该对象用于向下游服务发出请求。发出请求的中间件是Ocelot管道中的最后一件事。它不会调用下一个中间件。有一块中间件可将HttpResponseMessage映射到HttpResponse对象,然后将其返回给客户端。基本上,它具有许多其他功能。

代码实现

1、新建api客户端1

ASP.NET Core Api网关Ocelot的使用初探

2、新建api 网关test

ASP.NET Core Api网关Ocelot的使用初探

3、nuget安装Ocelot

ASP.NET Core Api网关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个项目运行,测试

ASP.NET Core Api网关Ocelot的使用初探

代码地址

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

以上就是ASP.NET Core Api网关Ocelot的使用初探的详细内容,更多关于ASP.NET Core Api网关Ocelot的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/lyl6796910/p/14521929.html

延伸 · 阅读

精彩推荐