一、概要

Ocelot是.Net Core下一个开源API网关;Ocelot主要目标是在.NET在微服务或面向服务架构中提供统一的入口服务,

Ocelot拿到HttpRequest对象到管道后,先创建HttpRequestMessage对象,该对象用于向下游服务发出请求。再将HttpResponseMessage映射到HttpResponse对象上,并返回给客户端。。

主要功能:统一入口、认证、鉴权、限流熔断、内置了负载均衡等等

二、单网关QuickStart

2.1 API网关

新建.Net Core 2.0 webapi项目:Practice.ApiGetway

添加Ocelot的Nuget包引用到该项目:Install-Package Ocelot

修改Startup.cs:

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.AddMvc();
services.AddOcelot(new ConfigurationBuilder().AddJsonFile("ocelot_config.json").Build());
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseOcelot().Wait();
app.UseMvc();
}
}

修改Program.cs:

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseUrls("http://*:5000")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
}

添加一个json文件ocelot_config.json,设置属性:始终复制

{
"ReRoutes": [ {
"DownstreamPathTemplate": "/api/users",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port":
}
],
"UpstreamPathTemplate": "/users/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
}
},
{
"DownstreamPathTemplate": "/api/news",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port":
}
],
"UpstreamPathTemplate": "/news/values",
"UpstreamHttpMethod": [ "Get" ],
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": ,
"DurationOfBreak": ,
"TimeoutValue":
}
}
], "GlobalConfiguration": {
//"BaseUrl": "https://api.mybusiness.com"
}
}

修改launchSettings.json设置个固定的端口

2.2 下游实际API

最后在新建两个API项目:

项目:Practice.NewsApi  、Controller:UsersController、端口:5002

项目:Practice.UsersApi  、Controller:NewsController、端口:5001

运行三个项目

不再截图,运行结果:

访问网关:http://localhost:5000/users/values   会得到 http://localhost:5001/api/users 结果

访问网关:http://localhost:5000/news/values   会得到 http://localhost:5002/api/news  结果

资源:

项目开源地址:https://github.com/ThreeMammals/Ocelot

官方文档:http://ocelot.readthedocs.io/en/latest/

资源:https://github.com/geffzhang/awesome-ocelot

博客:

http://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html

https://www.cnblogs.com/jackcao/

Ocelot:API网关概要的更多相关文章

  1. 微服务(入门三):netcore ocelot api网关结合consul服务发现

    简介 api网关是提供给外部调用的统一入口,类似于dns,所有的请求统一先到api网关,由api网关进行指定内网链接. ocelot是基于netcore开发的开源API网关项目,功能强大,使用方便,它 ...

  2. .Netcore 2.0 Ocelot Api网关教程(7)- 限流

    本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对  ...

  3. .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理

    本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...

  4. .Netcore 2.0 Ocelot Api网关教程(2)- 路由

    .Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...

  5. .NET Core微服务二:Ocelot API网关

    .NET Core微服务一:Consul服务中心 .NET Core微服务二:Ocelot API网关 .NET Core微服务三:polly熔断与降级 本文的项目代码,在文章结尾处可以下载. 本文使 ...

  6. ASP.NET Core on K8S学习之旅(13)Ocelot API网关接入

    本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇介绍了Ingress的基本概念和Nginx Ingress的基本配置和使 ...

  7. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(四)

    在上一讲中,我们已经完成了一个完整的案例,在这个案例中,我们可以通过Angular单页面应用(SPA)进行登录,然后通过后端的Ocelot API网关整合IdentityServer4完成身份认证.在 ...

  8. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(一)

    好吧,这个题目我也想了很久,不知道如何用最简单的几个字来概括这篇文章,原本打算取名<Angular单页面应用基于Ocelot API网关与IdentityServer4+ASP.NET Iden ...

  9. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(二)

    上文已经介绍了Identity Service的实现过程.今天我们继续,实现一个简单的Weather API和一个基于Ocelot的API网关. 回顾 <Angular SPA基于Ocelot ...

随机推荐

  1. mysql分区方案的研究

    笔者觉得,分库分表确实好的.但是,动不动搞分库分表,太麻烦了.分库分表虽然是提高数据库性能的常规办法,但是太麻烦了.所以,尝试研究mysql的分区到底如何. 之前写过一篇文章,http://www.c ...

  2. 微信jssdk常见错误及解决方法

    调用config 接口的时候传入参数 debug: true 可以开启debug模式,页面会alert出错误信息.以下为常见错误及解决方法: invalid url domain当前页面所在域名与使用 ...

  3. python faker 生成随机类型字符串

    以前生成测试字符时,用random模块拼来拼去来生成随机串,如姓名,手机,身份证等,还是费一些功夫,不过有了faker模块,一切变得简单起来 基本使用: from faker import Faker ...

  4. quartz与spring boot-最简模式

    多年前使用过quartz,今天又需要再用,而且是在spring boot框架下.很神奇,spring也是十年前用过的. 这里仅记录下完成的最快速和简单的操作,高级的使用以后有空弄明白了再写: 1.增加 ...

  5. 卸载ie

    今天卸载ie11失败,最后使用下面这个命令实现了卸载,记录下 IE11卸载命令如下: FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Wind ...

  6. [Manthan, Codefest 18][Codeforces 1037F. Maximum Reduction]

    题目链接:1037F - Maximum Reduction 题目大意:给出一段代码,给你一个长度为n的数组和数字k,求程序运行结果,mod 1e9+7输出 简单翻译下代码的意思,初始定义一个空数组b ...

  7. php框架中,try,catch不能用的问题(转载)

    本文转自:http://blog.csdn.net/sangjinchao/article/details/71436557 最近再用laravel框架发现,try catch用了没有效果,依然不能阻 ...

  8. 【C++/实验三】类和对象

    1.定义一个矩形类,有长,宽两个属性,有成员函数计算矩形的面积. 在该矩形类中,我做了5个主要的测试. 构造函数带默认值参数,利用默认值参数计算矩形面积:rectangle(double x=2.0, ...

  9. 剑指offer——python【第21题】栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  10. Grunt Bower构建前端

    Grunt + Bower—前端构建利器   目前比较流行的WEB开发的趋势是前后端分离.前端采用重量级的Javascript框架,比如Angular,Ember等,后端采用restful API的W ...