.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

上一篇【.Net Core微服务入门全纪录(八)——Docker Compose与容器网络】完成了docker-compose.yml文件的编写,最后使用docker compose的一个up指令即可在docker中运行整个复杂的环境。本篇简单介绍一下Ocelot与Swagger的集成,方便在网关项目中统一查看各个服务的api文档。

开始

首先,网关项目,服务项目 NuGet安装Swashbuckle.AspNetCore:

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

服务项目

Order.API项目Startup:

public void ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Order API", Version = "v1", Description = "# order service api..." }); // Set the comments path for the Swagger JSON and UI. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Description = "在下框中输入请求头中需要添加Jwt授权Token:Bearer Token", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, BearerFormat = "JWT", Scheme = "Bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme{ Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer"} },new string[] { } } }); }); services.AddControllers(); ...... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, OrderContext orderContext) { ...... app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Order API V1"); }); app.UseRouting(); ...... }

打开项目文件Order.API.csproj,添加生成文档的配置,swagger要用到:

<GenerateDocumentationFile>true</GenerateDocumentationFile> <NoWarn>$(NoWarn);1591</NoWarn>

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

Product.API项目也是类似的修改,就不贴了。

网关项目

然后是Ocelot网关项目的Startup:

public void ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "Gateway API", Version = "v1", Description = "# gateway api..."}); }); services.AddControllers(); ...... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/order/swagger/v1/swagger.json", "Order API V1"); c.SwaggerEndpoint("/product/swagger/v1/swagger.json", "Product API V1"); }); //设置Ocelot中间件 app.UseOcelot().Wait(); }

ocelot.json配置文件,Routes节点下增加2个路由配置,不做授权,限流,熔断等限制:

{ "DownstreamPathTemplate": "/swagger/v1/swagger.json", "DownstreamScheme": "http", "UpstreamPathTemplate": "/product/swagger/v1/swagger.json", "UpstreamHttpMethod": [ "Get" ], "ServiceName": "ProductService", "LoadBalancerOptions": { "Type": "RoundRobin" } }, { "DownstreamPathTemplate": "/swagger/v1/swagger.json", "DownstreamScheme": "http", "UpstreamPathTemplate": "/order/swagger/v1/swagger.json", "UpstreamHttpMethod": [ "Get" ], "ServiceName": "OrderService", "LoadBalancerOptions": { "Type": "RoundRobin" } } 运行测试

使用docker-compose build:

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

build完成后启动:

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

浏览器访问网关项目::9070/swagger

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

接口测试:

.Net Core微服务入门全纪录(完结)——Ocelot与Swagger

此时因为没有授权所以返回401,为了方便获取token,我在IDS4.AuthCenter项目增加了一个客户端配置:

new Client { ClientId = "postman client", ClientName = "Postman Client", AllowedGrantTypes = GrantTypes.ClientCredentials, ClientSecrets = { new Secret("postman client secret".Sha256()) }, AllowedScopes = new [] {"orderApiScope", "productApiScope"}, }

使用postman获取token:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsjjp.html