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