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. xmanager 打开centos7图形化窗口

    centos7 最小化安装后,个别时候需要执行一些带图形界面的命令.比如安装oracle,打开xclock等. 前置条件:centos7系统 ,xmanager 已安装 用xclock做测试 1.因为 ...

  2. Jni中C++和Java的参数传递(转)

    如何使用JNI的一些基本方法和过程在网上多如牛毛,如果你对Jni不甚了解,不知道Jni是做什么的,如何建立一个基本的jni程序,或许可以参考下面下面这些文章:利用VC++6.0实现JNI的最简单的例子 ...

  3. Leetcode题解(十四)

    39.Combination Sum 题目 题目要求找出和为target的数字组合,并且每个整数可以多次使用.仔细思考可以发现,这道题目可以采用递归的方法来完成,比如举的例子,target=7,一开始 ...

  4. Can you solve this equation?

    Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its sol ...

  5. awk巩固练习题

    第1章 awk数组练习题 1.1 文件内容(仅第一行) [root@znix test]# head -1 secure-20161219 access.log ==> secure-20161 ...

  6. asp.net core webapi文件上传

    最近开发一个新项目,使用了asp.net core 2.0,采用webapi开发后台,postgresql为数据库.最先来的问题就是上传文件的问题. POST文件的一些坑 使用默认模板创建webapi ...

  7. c++学习笔记---01---C++语言与OO思想介绍

    C++语言与OO思想介绍 C++的特点与OO思想 C语言有一个优点,即它的速度可以很快.写出来的程序可以很精练.简单.小巧,不用为了解决某个问题环绕太平洋一大圈. 但如果将C和C++相比较,C++就经 ...

  8. redis数据库安装及简单的增删改查

    redis下载地址:https://github.com/MSOpenTech/redis/releases. 解压之后,运行 redis-server.exe redis.windows.conf  ...

  9. Array类的Sort()方法

    刚复习了Array类的sort()方法, 这里列举几个常用的,和大家一起分享. Array类实现了数组中元素的冒泡排序.Sort()方法要求数组中的元素实现IComparable接口.如System. ...

  10. percona_xtrabackup

    原理 percona xtrabackup备份过程主要分为以下几点: 1.xtrabackup在启动时会记住LSN(log sequence number),然后复制所有的数据文件 2.xtrabac ...