欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger

 更新時間:2022年04月08日 14:20:15   作者:暗斷腸  
這篇文章介紹了ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.前言

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

{
  "Routes": [
    {
      //下游服務(wù)地址
      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
      //傳輸協(xié)議
      "DownstreamScheme": "http",
      //下游主機跟端口號(數(shù)組)
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9001
        }
      ],
      //上游服務(wù)地址
      "UpstreamPathTemplate": "/a/swagger/v1/swagger.json",
      //上游服務(wù)Http端口號(數(shù)組)
      "UpstreamHttpMethod": [ "Get", "POST" ]
    },
    {
      //下游服務(wù)地址
      "DownstreamPathTemplate": "/swagger/v1/swagger.json",
      //傳輸協(xié)議
      "DownstreamScheme": "http",
      //上游服務(wù)Http端口號(數(shù)組)
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 9002
        }
      ],
      //上游服務(wù)地址
      "UpstreamPathTemplate": "/b/swagger/v1/swagger.json",
      //上游服務(wù)Http端口號(數(shù)組)
      "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服務(wù)注入:

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服務(wù);
            s.AddOcelot();
            s.AddMvc();
            //添加Swagger服務(wù);
            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服務(wù)注入:
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方法,對應(yīng)APIGateway項目的路由上下游配置,具體代碼如下:

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

2.3APIServiceB項目

Startup添加Swagger服務(wù)注入:
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方法,對應(yīng)APIGateway項目的路由上下游配置,具體代碼如下:

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

2.4項目運行

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

APIServiceB:

通過網(wǎng)關(guān)的路由配置,把Swagger集成到Ocelot中,統(tǒng)一入口管理,通過網(wǎng)關(guān)入口我們就能打開不同下游服務(wù)的Swagger。

到此這篇關(guān)于ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論