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")

.UseStartup<Startup>()

.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 IEnumerable<string> Get()

{

return new string[] { "DemoA服务", "请求" };

}

……

}

[Route("demobapi/[controller]")]

public class ValuesController : Controller

{

[HttpGet]

public IEnumerable<string> Get()

{

return new string[] { "DemoB服务", "请求" };

}

……

}

最后在解决方案属性->多个启动项目中,把DemoAAPI,DemoBAPI,OcelotGateway都设成启动,开始启动解决方案,效果如下图

Ocelot网关的更多相关文章

  1. 基于.NET平台的Ocelot网关框架教程汇总

    Ocelot 框架是基于.NET 开发的 API 网关,API网关是系统内部服务暴露在外部的一个访问入口,类似于代理服务器,就像一个公司的门卫承担着寻址.限制进入.安全检查.位置引导等工作,我们可以形 ...

  2. 微服务之:从零搭建ocelot网关和consul集群

    介绍 微服务中有关键的几项技术,其中网关和服务服务发现,服务注册相辅相成. 首先解释几个本次教程中需要的术语 网关 Gateway(API GW / API 网关),顾名思义,是企业 IT 在系统边界 ...

  3. Ocelot网关统一查看多个微服务asp.net core项目的swagger API接口

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 一.准备 前提需要下载安装consul,项目需要懂添加swagger 统一在网关中配置多个微服务的swagger,需要用到服务注册 ...

  4. .net core下,Ocelot网关与Spring Cloud Gateway网关的对比测试

    有感于 myzony 发布的 针对 Ocelot 网关的性能测试 ,并且公司下一步也需要对.net和java的应用做一定的整合,于是对Ocelot网关.Spring Cloud Gateway网关做个 ...

  5. Ocelot网关+IdentityServer4实现API权限认证

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...

  6. 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现

    庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介      在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...

  7. 庐山真面目之七微服务架构Consul集群、Ocelot网关集群和IdentityServer4版本实现

    庐山真面目之七微服务架构Consul集群.Ocelot网关集群和IdentityServer4版本实现 一.简介      在上一篇文章<庐山真面目之六微服务架构Consul集群.Ocelot网 ...

  8. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  9. .Net Core with 微服务 - Ocelot 网关

    上一次我们通过一张架构图(.Net Core with 微服务 - 架构图)来讲述了微服务的结构,分层等内容.从现在开始我们开始慢慢搭建一个最简单的微服务架构.这次我们先用几个简单的 web api ...

  10. 微服务之十四如何在 Ocelot 网关中配置多实例 Swagger 访问

    一.介绍 当我们开发基于微服务的应用程序的时候,有一个环节总是跳不过去的,那就是要创建 WebApi,然后,我们的应用程序基于 WebApi 接口去访问.在没有 Swagger 以前,我们开发好了 W ...

随机推荐

  1. 启动tomcat爆错 the JRE could not be found

    启动报错,如下图: 之前更改了了一个较低的jdk的版本看了看一个项目的代码,不知所云,然后再改回来, 混乱之中只要启动Tomcat就出现这种错误,还是无法找到JRE,最后如此解决: 在Windows- ...

  2. [PGM] Bayes Network and Conditional Independence

    2 - 1 - Semantics & Factorization 2 - 2 - Reasoning Patterns 2 - 3 - Flow of Probabilistic Influ ...

  3. Leetcode题解(20)

    59. Spiral Matrix II 题目 这道题copy网上的代码 class Solution { private: ][]; ][]; public: void dfs(int dep, v ...

  4. js foreach、map函数

    语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...

  5. linux 安装 Elasticsearch5.6.x 详细步骤以及问题解决方案

    在网上有很多那种ES步骤和问题的解决 方案的,不过没有一个详细的整合,和问题的梳理:我就想着闲暇之余,来记录一下自己安装的过程以及碰到的问题和心得:有什么不对的和问题希望及时拍砖. 第一步:环境 li ...

  6. android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

    注:本文内容来自:https://developer.android.com/training/wearables/apps/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 创 ...

  7. python定时利用QQ邮件发送天气预报

    大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...

  8. [转载] 网络IO模型

    转载自http://blog.csdn.net/zhoudaxia/article/details/8974779 同步(synchronous) IO和异步(asynchronous) IO,阻塞( ...

  9. Git问题集锦

    1.初始新建git,出现No refs in common and none specified; doing nothing 解决方案:Perhaps you should specify a br ...

  10. STM32F4中USB与PC双向通信

    STM32F4系列处理器内部集成了USB-OTG控制器,在不要外部USB IC下就可以实现USB通信,最近两天看了下如何利用STM32的USB实现通信,记录下关键步骤: 1. 从http://www. ...