netcore简单使用Ocelot搭建网关
开发环境操作系统:版本Windows 10 专业版 操作系统内部版本19042.1165开发工具:Microsoft Visual Studio Community 2019版本 16.10.0开发框架:ASP.NET Core 3.1本文相关Nuget包:Ocelot -Version 16.0.1Swashbuckle.AspNetCore 5.6.3一、搭建一个webapi项目OcelotG
开发环境
操作系统:版本 Windows 10 专业版 操作系统内部版本 19042.1165
开发工具:Microsoft Visual Studio Community 2019版本 16.10.0
开发框架:ASP.NET Core 3.1
本文相关Nuget包:
Ocelot -Version 16.0.1
Swashbuckle.AspNetCore 5.6.3
一、搭建一个webapi项目OcelotGateway31
1、安装 Ocelot 与 Swashbuckle.AspNetCore 包
2、添加Ocelot的配置文件ocelot.json
在启动文件Program.cs中设置项目启动读取ocelot.json配置文件
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
3、OCELOT基本配置
先配置Startup.cs文件。在ConfigureServices()方法中,添加代码:
services.AddOcelot();
在Configure()方法中,UseRouting()中间件之前,添加Ocelot中间件:
app.UseOcelot().Wait();
在ocelot.json添加内容
{
"Routes": [
//APITest1
{
"UpstreamPathTemplate": "/api/test1/{url}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8801
}
],
"DownstreamPathTemplate": "/api/test1/{url}",
"DownstreamScheme": "http",
"DownstreamHeaderTransform": {
"Access-Control-Allow-Origin": "http://localhost:8800",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*"
}
},
//APITest1服务swagger
{
"UpstreamPathTemplate": "/test1/swagger/v1/swagger.json",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8801
}
],
"DownstreamPathTemplate": "/swagger/v1/swagger.json",
"DownstreamScheme": "http"
},
//APITest2
{
"UpstreamPathTemplate": "/api/test2/{url}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "PATCH", "Delete", "Options" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8802
}
],
"DownstreamPathTemplate": "/api/test2/{url}",
"DownstreamScheme": "http",
"DownstreamHeaderTransform": {
"Access-Control-Allow-Origin": "http://localhost:8800",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*"
}
},
//APITest2服务swagger
{
"UpstreamPathTemplate": "/test2/swagger/v1/swagger.json",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 8802
}
],
"DownstreamPathTemplate": "/swagger/v1/swagger.json",
"DownstreamScheme": "http"
}
]
}
4、添加swagger支持
在ConfigureServices()方法中,添加代码:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo { Title = "Gateway API", Version = "v1", Description = "# gateway api..." });
});
在Configure()方法中,Ocelot 中间件之前,添加swagger()中间件:
#region Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.SwaggerEndpoint("/test1/swagger/v1/swagger.json", "APITest1 API V1");
c.SwaggerEndpoint("/test2/swagger/v1/swagger.json", "APITest2 API V1");
});
#endregion
二、搭建APITest1的Api服务
1、安装 Swashbuckle.AspNetCore 包
2、注册swagger :配置Startup.cs文件。在ConfigureServices()方法中,添加代码:
#region 注册Swagger生成器,定义一个或多个Swagger文档
services.AddSwaggerGen(c =>
{
//Bearer 的scheme定义
var securityScheme = new OpenApiSecurityScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
//参数添加在头部
In = ParameterLocation.Header,
//使用Authorize头部
Type = SecuritySchemeType.Http,
//内容为以 bearer开头
Scheme = "bearer",
BearerFormat = "JWT"
};
//把所有方法配置为增加bearer头部信息
var securityRequirement = new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "bearerAuth"
}
},
new string[] {}
}
};
//注册到swagger中
c.AddSecurityDefinition("bearerAuth", securityScheme);
c.AddSecurityRequirement(securityRequirement);
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Test1 Web API",
Description = "APITest1,添加一个简单的ASP.NET Core Web API示例。",
Version = "v1",
TermsOfService = new Uri("https://sc.52abp.com"),
Contact = new OpenApiContact
{
Name = "",
Email = "11111111@qq.com",
},
});
});
#endregion
在Configure()方法中,UseRouting()中间件之前,添加Ocelot中间件:
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test1 v1"));
3、添加Api:HealthController 注意Route 要与网关中的配置中的路由关键字段一致
“test1”
[Route("api/test1/[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("Index")]
public string Index()
{
string info = $"This is Test1 HealthController ";
return info;
}
}
三、参照步骤二搭建API服务APITest2
四、调试运行:
更多推荐
所有评论(0)