亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

1.前言

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

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

2.1APIGateway項(xiàng)目

添加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項(xiàng)目

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");
});

添加一個(gè)Get方法,對(duì)應(yīng)APIGateway項(xiàng)目的路由上下游配置,具體代碼如下:

/// <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項(xiàng)目

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");
});

添加一個(gè)Get方法,對(duì)應(yīng)APIGateway項(xiàng)目的路由上下游配置,具體代碼如下:

/// <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項(xiàng)目運(yùn)行

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

APIServiceB:

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

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

相關(guān)文章

最新評(píng)論