ASP.NET Core為Ocelot網(wǎng)關(guān)配置Swagger
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í)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流
- ASP.NET?Core中的Ocelot網(wǎng)關(guān)介紹
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot超時、熔斷、限流
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot集成Consul
- .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot基礎(chǔ)介紹及集成
- ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
- ASP.NET Core3.1 Ocelot負(fù)載均衡的實現(xiàn)
- ASP.NET Core3.1 Ocelot認(rèn)證的實現(xiàn)
- ASP.NET Core3.1 Ocelot路由的實現(xiàn)
- Asp.Net?Core使用Ocelot結(jié)合Consul實現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
相關(guān)文章
.NET醫(yī)院公眾號系統(tǒng)線程CPU雙高問題分析
這篇文章主要介紹了.NET醫(yī)院公眾號系統(tǒng) 線程CPU雙高分析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類2008-11-11Asp.net中的數(shù)據(jù)綁定Eval和Bind應(yīng)用示例
這篇文章主要介紹了Asp.net中的數(shù)據(jù)綁定Eval和Bind的應(yīng)用,需要的朋友可以參考下2014-05-05.Net Core自動化部署之利用docker版jenkins部署dotnetcore應(yīng)用的方法
這篇文章主要給大家介紹了關(guān)于.Net Core自動化部署之利用docker版jenkins部署dotnetcore應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06