百度收录左侧带图片的网站,四川清风建设工程有限公司网站,做渔船的网站,可做笔记的阅读网站Ocelot是一个.net core框架下的网关的开源项目#xff0c;下图是官方给出的基础实现图#xff0c;即把后台的多个服务统一到网关处#xff0c;前端应用#xff1a;桌面端#xff0c;web端#xff0c;app端都只用访问网关即可。 Ocelot的实现原理就是把客户端对网关的请求… Ocelot是一个.net core框架下的网关的开源项目下图是官方给出的基础实现图即把后台的多个服务统一到网关处前端应用桌面端web端app端都只用访问网关即可。 Ocelot的实现原理就是把客户端对网关的请求(Request)按照configuration.json的映射配置转发给对应的后端http service然后从后端http service获取响应(Response)后再返回给客户端。当然有了网关后我们可以在网关这层去做统一验证也可以在网关处统一作监控。 接下来做个Demo 新建三个asp.net core web aip项目 OcelotGateway网关项目端口是5000 DemoAAPI项目A端口是5001 DemoBAPI项目B端口是5002 (注端口可以在每个项目的Properties下的launchSettings.json中修改发布后的端口可以在Program.cs中用UseUrls(“http://*:5000”)来修改) 对于OcelotGateway 引用Ocelot的Nuget包 视图-其他窗口-程序包管理控制台Install-Package Ocelot 或项目右键“管理Nuget程序包”在浏览里查找Ocelot进行安装 在OcelotGateway项目中添加一个configuration.json文件关把它的属性“复制到输出目录”设成“始终复制”内容如下 { ReRoutes: [ { DownstreamPathTemplate: /demoaapi/values, DownstreamScheme: http, DownstreamPort: 5001, DownstreamHost: localhost, UpstreamPathTemplate: /demoaapi/values, UpstreamHttpMethod: [ Get ], QoSOptions: { ExceptionsAllowedBeforeBreaking: 3, DurationOfBreak: 10, TimeoutValue: 5000 }, HttpHandlerOptions: { AllowAutoRedirect: false, UseCookieContainer: false }, AuthenticationOptions: { AuthenticationProviderKey: , AllowedScopes: [] } }, { DownstreamPathTemplate: /demobapi/values, DownstreamScheme: http, DownstreamPort: 5002, DownstreamHost: localhost, UpstreamPathTemplate: /demobapi/values, UpstreamHttpMethod: [ Get ], QoSOptions: { ExceptionsAllowedBeforeBreaking: 3, DurationOfBreak: 10, TimeoutValue: 5000 }, HttpHandlerOptions: { AllowAutoRedirect: false, UseCookieContainer: false }, AuthenticationOptions: { AuthenticationProviderKey: , AllowedScopes: [] } } ] } 接下来对OcelotGateway的Program.cs进行改造 using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace OcelotGateway { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { IWebHostBuilder builder new WebHostBuilder(); //注入WebHostBuilder return builder.ConfigureServices(service { service.AddSingleton(builder); }) //加载configuration配置文人年 .ConfigureAppConfiguration(conbuilder { conbuilder.AddJsonFile(configuration.json); }) .UseKestrel() .UseUrls(http://*:5000) .UseStartupStartup() .Build(); } } } 同时修改Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Ocelot.DependencyInjection; using Ocelot.Middleware; namespace OcelotGateway { public class Startup { public Startup(IConfiguration configuration) { Configuration configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { //注入配置文件AddOcelot要求参数是IConfigurationRoot类型所以要作个转换 services.AddOcelot(Configuration as ConfigurationRoot); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //添加中间件 app.UseOcelot().Wait(); } } } 为了测试数据好看我们把DemoAAPI项目和DemoBAPI项目的ValuesController作一下修改 [Route(demoaapi/[controller])] public class ValuesController : Controller { [HttpGet] public IEnumerablestring Get() { return new string[] { DemoA服务, 请求 }; } //…… } [Route(demobapi/[controller])] public class ValuesController : Controller { [HttpGet] public IEnumerablestring Get() { return new string[] { DemoB服务, 请求 }; } //…… } 最后在解决方案属性-多个启动项目中把DemoAAPIDemoBAPIOcelotGateway都设成启动开始启动解决方案效果如下图 相关文章 Ocelot——初识基于.Net Core的API网关 Ocelot API网关的实现剖析 微服务网关Ocelot API网关Ocelot 使用Polly 处理部分失败问题 谈谈微服务中的 API 网关API Gateway 原文http://www.cnblogs.com/axzxs2001/p/8005041.html .NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com