环境:
- window 10 x64
- vs2019 16.7.4
- asp.net core 3.1
- Ocelot 16.0.1
实验代码下载:https://download.csdn.net/download/u010476739/13032294
一、Ocelot介绍和作用
Ocelot
是C#开发的,主要用于网关服务的,比如:转发请求、限流等,可类比Nginx
。
二、体验Ocelot的转发功能
本次体验一下ocelot的转发请求功能,实验的架构如下:
新建空白解决方案OcelotTrial
以及三个WebApi项目如下:
其中,OcelotDemo
为网关项目,它开启后用来将浏览器的请求转发到GoodApi
和OrderApi
服务上,而GoodApi
和OrderApi
为正常的webapi项目。
2.1 准备OrderApi
项目
在OrderApi
项目上新建DemoController
控制器,代码如下:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace OrderApi.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class DemoController : ControllerBase
{
public string Echo()
{
return "OrderApi";
}
}
}
配置OrderApi
项目的AppSettings.json
文件,强制运行url为:http://localhost:5502
,配置如下:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"urls": "http://localhost:5502"
}
2.2 准备GoodApi
项目
在GoodApi
项目上新建DemoController
控制器,代码如下:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GoodApi.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class DemoController : ControllerBase
{
public string Echo()
{
return "GoodApi";
}
}
}
配置GoodApi
项目的AppSettings.json
文件,强制运行url为:http://localhost:5501
,配置如下:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"urls": "http://localhost:5501"
}
2.3 准备OcelotDemo
项目
给OcelotDemo
项目添加Ocelot
包引用:
<ItemGroup>
<PackageReference Include="Ocelot" Version="16.0.1" />
</ItemGroup>
新建配置文件ocelot.json
,如下:
{
"Routes": [
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5501
}
],
"UpstreamPathTemplate": "/good/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": { //这里可以配置多个goodapi实例
"Type": "RoundRobin"
}
},
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [ //这里可以配置多个orderapi实例
{
"Host": "localhost",
"Port": 5502
}
],
"UpstreamPathTemplate": "/order/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
],
"GlobalConfiguration": {
}
}
修改Program.cs
文件,将ocelot.json
配置文件载入:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace OcelotDemo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("ocelot.json");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
修改Startup.cs
文件,注入Ocelot
服务,并在http管道中启用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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 Ocelot.DependencyInjection;
using Ocelot.Middleware;
namespace OcelotDemo
{
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.AddOcelot();//注入Ocelot服务
}
// 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.UseOcelot().Wait();//使用Ocelot中间件
}
}
}
2.4 测试效果
将这三个项目编译后分别启动,然后通过网关访问GoodApi
和OrderApi
的服务:
通过网关访问GoodApi
服务: http://localhost:5500/good/api/demo/echo
通过网关访问OrderApi
服务:http://localhost:5500/order/api/demo/echo