日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

ASP.NET?Core為Ocelot網關配置Swagger_實用技巧

作者:暗斷腸 ? 更新時間: 2022-06-10 編程語言

1.前言

前端與后端的聯系更多是通過API接口對接,API文檔變成了前后端開發人員聯系的紐帶,開始變得越來越重要,而Swagger就是一款讓你更好的書寫規范API文檔的框架。在Ocelot Swagger項目示例中,通過APIGateway項目路由配置網關、上下游服務Swagger。對解決方案中的示例APIServiceA、APIServiceB項目Get方法進行配置,文件配置具體代碼如下:

{
  "Routes": [
    {
      //下游服務地址
      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
      //傳輸協議
      "DownstreamScheme": "http",
      //下游主機跟端口號(數組)
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9001
        }
      ],
      //上游服務地址
      "UpstreamPathTemplate": "/a/swagger/v1/swagger.json",
      //上游服務Http端口號(數組)
      "UpstreamHttpMethod": [ "Get", "POST" ]
    },
    {
      //下游服務地址
      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
      //傳輸協議
      "DownstreamScheme": "http",
      //上游服務Http端口號(數組)
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9002
        }
      ],
      //上游服務地址
      "UpstreamPathTemplate": "/b/swagger/v1/swagger.json",
      //上游服務Http端口號(數組)
      "UpstreamHttpMethod": [ "Get", "POST" ]
    },
    {
      "DownstreamPathTemplate": "/a",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9001
        }
      ],
      "UpstreamPathTemplate": "/a",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/b",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9002
        }
      ],
      "UpstreamPathTemplate": "/b",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration"
  }
}

2.項目演示

2.1APIGateway項目

添加Ocelot、Swagger服務注入:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:9000")
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                .AddJsonFile("ocelot.json")
                .AddEnvironmentVariables();
        })
        .ConfigureServices(s =>
        {
            //添加Ocelot服務;
            s.AddOcelot();
            s.AddMvc();
            //添加Swagger服務;
            s.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "GW", Version = "v1" });
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                var xmlPath = Path.Combine(basePath, "APIGateway.xml");
                c.IncludeXmlComments(xmlPath);
            });
        })
        .Configure(a =>
        {
            //使用Swagger
            a.UseSwagger().UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/a/swagger/v1/swagger.json", "APIServiceA");
                c.SwaggerEndpoint("/b/swagger/v1/swagger.json", "APIServiceB");
            });

            //使用Ocelot;
            a.UseOcelot().Wait();
        })
        .Build();

2.2APIServiceA項目

Startup添加Swagger服務注入:
ConfigureServices:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "APIServiceA", Version = "v1" });
    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
    var xmlPath = Path.Combine(basePath, "APIServiceA.xml");
    c.IncludeXmlComments(xmlPath);
});
Configure:
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "APIServiceA");
});

添加一個Get方法,對應APIGateway項目的路由上下游配置,具體代碼如下:

/// 
/// Values controller.
/// 
[Route("a")]
public class ValuesController : Controller
{
    // GET api/values
    /// 
    /// Get values.
    /// 
    /// The get.
    [HttpGet]
    public IEnumerable Get()
    {
        return new string[] { "value1", "value2" };
    }
}

2.3APIServiceB項目

Startup添加Swagger服務注入:
ConfigureServices:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "APIServiceB", Version = "v1" });
    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
    var xmlPath = Path.Combine(basePath, "APIServiceB.xml");
    c.IncludeXmlComments(xmlPath);
});

Configure:

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "APIServiceB");
});

添加一個Get方法,對應APIGateway項目的路由上下游配置,具體代碼如下:

/// 
/// Values controller.
/// 
[Route("b")]
public class ValuesController : Controller
{
    // GET api/values
    /// 
    /// Get values.
    /// 
    /// The get.
    [HttpGet]
    public IEnumerable Get()
    {
        return new string[] { "value3", "value4" };
    }
}

2.4項目運行

注:如果想把Swagger注釋警告提示取消,可以在對應項目文件.csproj的PropertyGroup節點上加入$(NoWarn);1591這一行代碼。
輸入dotnet run --project 項目路徑\項目文件.csproj把三個項目啟動起來,通過在瀏覽器分別打開APIServiceA與APIServiceB兩個站點上游服務Swagger地址,會看到如下信息:
APIServiceA:

APIServiceB:

通過網關的路由配置,把Swagger集成到Ocelot中,統一入口管理,通過網關入口我們就能打開不同下游服務的Swagger。

原文鏈接:https://www.cnblogs.com/wzk153/p/14009897.html

欄目分類
最近更新