环境:
window 10 x64vs2019 16.7.4asp.net core 3.1Ocelot 16.0.1实验代码下载:https://download.csdn.net/download/u010476739/13032294
Ocelot是C#开发的,主要用于网关服务的,比如:转发请求、限流等,可类比Nginx。
本次体验一下ocelot的转发请求功能,实验的架构如下:
新建空白解决方案OcelotTrial以及三个WebApi项目如下: 其中,OcelotDemo为网关项目,它开启后用来将浏览器的请求转发到GoodApi和OrderApi服务上,而GoodApi和OrderApi为正常的webapi项目。
在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" }在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" }给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中间件 } } }将这三个项目编译后分别启动,然后通过网关访问GoodApi和OrderApi的服务:
通过网关访问GoodApi服务: http://localhost:5500/good/api/demo/echo 通过网关访问OrderApi服务:http://localhost:5500/order/api/demo/echo