环境准备
为了查看效果,新建三个WebApi项目,Api.Catalog、Api.Ordering、ApiGateway.Ocelot。
Ocelot
1、给ApiGateway.Ocelot项目添加Ocelot依赖
在VSCode里使用命令:
dotnet add package Ocelot
2、添加Ocelot配置文件
在ApiGateway.Ocelot项目目录内添加ocelot.json文件,并添加以下配置信息:
{
"GlobalConfiguration": {
},
"Routes": [
{
"DownstreamPathTemplate": "/{everything}",//下游服务路由地址
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5051 //下游戏服务地址端口
}
],
"UpstreamPathTemplate": "/api1/{everything}",//上游服务路由地址
"UpstreamHttpMethod": [ "Get", "Post" ] // 支持的上游服务请求方式
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5052
}
],
"UpstreamPathTemplate": "/api2/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ]
}
]
}
以上配置详细可以在Ocelot官方网站查看。
3、使用Ocelot中间件
需要在项目里加载ocelot.json文件
然后在Startup.cs里注入服务依赖及添加中间件
1) 注入服务依赖
// 注入Ocelot服务
services.AddOcelot();
2) 使用中间件
app.UseOcelot().Wait(); //使用中间件
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using IdentityServer4.AccessTokenValidation;
namespace ApiGateway.Ocelot
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ApiGateway.Ocelot", Version = "v1" });
});
// services.AddAuthentication()
// .AddIdentityServerAuthentication("Api.Catalog", i =>
// {
// i.Authority = "http://localhost:8888"; //配置Identityserver的授权地址
// i.RequireHttpsMetadata = false; //不需要https
// i.ApiName = "Api.Catalog"; //api的name,需要和config的名称相同
// i.SupportedTokens = SupportedTokens.Both;
// }).AddIdentityServerAuthentication("Api.Ordering", y =>
// {
// y.Authority = "http://localhost:8888"; //配置Identityserver的授权地址
// y.RequireHttpsMetadata = false; //不需要https
// y.ApiName = "Api.Ordering"; //api的name,需要和config的名称相同
// y.SupportedTokens = SupportedTokens.Both;
// });
// 注入Ocelot服务
services.AddOcelot();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiGateway.Ocelot v1"));
}
app.UseHttpsRedirection();
app.UseOcelot().Wait(); //使用中间件
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
结果
上面配置了上游戏服务地址/api1/{everything},所以在这里请求http://localhost:5050/api1/weatherforecast等同于访问http://localhost:5051/weatherforecast
同理api2/{everything},所以在这里请求http://localhost:5050/api2/weatherforecast等同于访问http://localhost:5052/weatherforecast