網站首頁 編程語言 正文
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 IEnumerableGet() { 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 IEnumerableGet() { return new string[] { "value3", "value4" }; } }
2.4項目運行
注:如果想把Swagger注釋警告提示取消,可以在對應項目文件.csproj的PropertyGroup節點上加入
輸入dotnet run --project 項目路徑\項目文件.csproj把三個項目啟動起來,通過在瀏覽器分別打開APIServiceA與APIServiceB兩個站點上游服務Swagger地址,會看到如下信息:
APIServiceA:
APIServiceB:
通過網關的路由配置,把Swagger集成到Ocelot中,統一入口管理,通過網關入口我們就能打開不同下游服務的Swagger。
原文鏈接:https://www.cnblogs.com/wzk153/p/14009897.html
相關推薦
- 2022-08-11 golang時間及時間戳的獲取轉換_Golang
- 2022-06-10 C語言?模擬實現strlen函數詳解_C 語言
- 2023-04-13 echarts儀表盤刻度浮點數取整問題
- 2022-10-03 python中pandas操作apply返回多列的實現_python
- 2022-01-17 Failed to load resource: the server responded with
- 2022-09-17 使用cache加快編譯速度的命令詳解_相關技巧
- 2022-04-18 pytorch自定義loss損失函數_python
- 2022-11-16 從Context到go設計理念輕松上手教程_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支