一、概要

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:

  1. public class Startup
  2. {
  3. public Startup(IConfiguration configuration)
  4. {
  5. Configuration = configuration;
  6. }
  7.  
  8. public IConfiguration Configuration { get; }
  9.  
  10. // This method gets called by the runtime. Use this method to add services to the container.
  11. public void ConfigureServices(IServiceCollection services)
  12. {
  13. services.AddMvc();
  14. services.AddOcelot(new ConfigurationBuilder().AddJsonFile("ocelot_config.json").Build());
  15. }
  16.  
  17. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  18. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  19. {
  20. if (env.IsDevelopment())
  21. {
  22. app.UseDeveloperExceptionPage();
  23. }
  24. app.UseOcelot().Wait();
  25. app.UseMvc();
  26. }
  27. }

修改Program.cs:

  1. public class Program
  2. {
  3. public static void Main(string[] args)
  4. {
  5. BuildWebHost(args).Run();
  6. }
  7.  
  8. public static IWebHost BuildWebHost(string[] args) =>
  9. WebHost.CreateDefaultBuilder(args)
  10. .UseKestrel()
  11. .UseUrls("http://*:5000")
  12. .UseContentRoot(Directory.GetCurrentDirectory())
  13. .UseStartup<Startup>()
  14. .Build();
  15. }

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

  1. {
  2. "ReRoutes": [
  3.  
  4. {
  5. "DownstreamPathTemplate": "/api/users",
  6. "DownstreamScheme": "http",
  7. "DownstreamHostAndPorts": [
  8. {
  9. "Host": "localhost",
  10. "Port":
  11. }
  12. ],
  13. "UpstreamPathTemplate": "/users/values",
  14. "UpstreamHttpMethod": [ "Get" ],
  15. "QoSOptions": {
  16. "ExceptionsAllowedBeforeBreaking": ,
  17. "DurationOfBreak": ,
  18. "TimeoutValue":
  19. }
  20. },
  21. {
  22. "DownstreamPathTemplate": "/api/news",
  23. "DownstreamScheme": "http",
  24. "DownstreamHostAndPorts": [
  25. {
  26. "Host": "localhost",
  27. "Port":
  28. }
  29. ],
  30. "UpstreamPathTemplate": "/news/values",
  31. "UpstreamHttpMethod": [ "Get" ],
  32. "QoSOptions": {
  33. "ExceptionsAllowedBeforeBreaking": ,
  34. "DurationOfBreak": ,
  35. "TimeoutValue":
  36. }
  37. }
  38. ],
  39.  
  40. "GlobalConfiguration": {
  41. //"BaseUrl": "https://api.mybusiness.com"
  42. }
  43. }

修改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. [C++]Qt程式异常崩溃处理技巧(Win)

    文章转载来自     http://www.cnblogs.com/lcchuguo/p/5177715.html     作者 lcchuguo https://blog.csdn.net/baid ...

  2. Cordova Error: cmd: Command failed with exit code ENOENT

    执行Cordova platform add android时提示:Error: cmd: Command failed with exit code ENOENT. 网上搜索后得到如下结果: 比对着 ...

  3. selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None;Message: unexpected alert open: {Alert text : 您点击的频率过快!请稍后再试}

    报错 Traceback (most recent call last): File "C:/myFiles/code/cnki/cnki_1/core/knavi.py", li ...

  4. BarTender中如何调整数据输入表单的大小?

    BarTender中的表单设计,是一个简单而又复杂的操作.简单的是它提供很多实用的工具,帮助用户实现更多的功能,复杂的是要对其进行排版设计,这就要看小伙伴们的个人要求高低了. 自定义数据输入表单时,你 ...

  5. fiddler 使用记录

    fiddler 工作原理 Fiddler 启动后将自己变成一个代理服务器,这个代理服务器默认监听 127.0.0.1:8888. Filddler 启动后浏览器的代理会被自动更改为 127.0.0.1 ...

  6. Oracle 11gR2(11.2.0.4)安装包(7个)作用说明

    在之前使用Oracle10G的时候,官网下载的数据库安装包只有两个文件,解压合并后为完整的安装包. 后来因为检查出多个Oracle漏洞,需要现场Oracle数据库版本需要升级到11.2.0.4,下载的 ...

  7. python中的pymongo连接脚本

    author: headsen chen date: 2019-04-12  17:39:12 先安装python3,pymongo [root@localhost mnt]# cat /root/p ...

  8. 腾讯云云机安装dockers

    云机的配置 首先更新一下源(更新前一直装不了) 下载dockers-ce(社区版) 启动dockers服务 使用hello-world进行测试(由于本地没有hello-world这个镜像,所以dock ...

  9. 16.vue-cli跨域,swiper,移动端项目

    ==解决跨域:== 1.后台 cors cnpm i -S cors 2.前端 jsonp 3.代理 webpack: myvue\config\index.js 找 proxyTable proxy ...

  10. CF1B Spreadsheets

    题意翻译 人们常用的电子表格软件(比如: Excel)采用如下所述的坐标系统: 第一列被标为A,第二列为B,以此类推,第26列为Z.接下来为由两个字母构成的列号: 第27列为AA,第28列为AB... ...