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


                .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——初识基于.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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322653.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux服务器部署laravel出现putenv() has been disabled for security reasons

putenv() has been disabled for security reasons 进入 www/serve/php/72/etc 找到 disable_functions后面的函数&#xff0c;删除 putenv() 如果报错 The Process class relies on proc_open, which is not available on your PHP installation. 删除 proc_open

欢乐纪中A组周六赛【2019.3.23】

前言 做A组被虐好惨 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC1313132017WYC2017WYC2017WYC1901901909090901001001000001919192017HZB2017HZB2017HZB1101101101001001001010100002727272017XJQ2017XJQ2017XJQ10010010010010010…

百度OCR文字识别-身份证识别

简介 答应了园区大牛张善友 要写AI 的系列博客&#xff0c;所以开始了AI 系列之旅。 一、介绍 身份证识别 API 接口文档地址&#xff1a;http://ai.baidu.com/docs#/OCR-API/top 接口描述 用户向服务请求识别身份证&#xff0c;身份证识别包括正面和背面。 请求说明 请求示例…

Spring Boot Elasticsearch 入门

转载自 芋道 Spring Boot Elasticsearch 入门 1. 概述 如果胖友之前有用过 Elasticsearch 的话&#xff0c;可能有过被使用的 Elasticsearch 客户端版本搞死搞活。如果有&#xff0c;那么一起握个抓。所以&#xff0c;我们在文章的开始&#xff0c;先一起理一理这块。 Elas…

内存不足The following exception is caused by a lack of memory or swap, or not having swap

在linux执行以下三个命令即可 /bin/dd if/dev/zero of/var/swap.1 bs1M count1024 /sbin/mkswap /var/swap.1 /sbin/swapon /var/swap.1

P2513-[HAOI2009]逆序对数列【dp,前缀和】

正题 题目链接:https://www.luogu.org/problemnew/show/P2513 题目大意 求长度为nnn逆序对为kkk个的序列总数。 解题思路 设fi,jf_{i,j}fi,j​表示1∼i1\sim i1∼i的排列逆序对个数为jjj 然后显然:fi,j∑k1i−1fi−1,j−kf_{i,j}\sum_{k1}^{i-1}f_{i-1,j-k}fi,j​k1∑i−1​…

在.NET Core类库中使用EF Core迁移数据库到SQL Server

前言 如果大家刚使用EntityFramework Core作为ORM框架的话&#xff0c;想必都会遇到数据库迁移的一些问题。 起初我是在ASP.NET Core的Web项目中进行的&#xff0c;但后来发现放在此处并不是很合理&#xff0c;一些关于数据库的迁移&#xff0c;比如新增表&#xff0c;字段&…

Spring Boot MongoDB 入门

转载自 芋道 Spring Boot MongoDB 入门 1. 概述 可能有一些胖友对 MongoDB 不是很了解&#xff0c;这里我们引用一段介绍&#xff1a; FROM 《分布式文档存储数据库 MongoDB》 MongoDB 是一个介于关系数据库和非关系数据库之间的产品&#xff0c;是非关系数据库当中功能最…

composer配置阿里云镜像

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

CF735D-Taxes【数学,数论】

正题 luogu题目链接:https://www.luogu.org/problemnew/show/CF735D 题目大意 将一个数分解成若干个数使得这若干个数的最大因子最小。 解题思路 如果是质数就是1。如果是偶数根据哥德巴赫猜想就是2。或者把一个奇数减去2后是个质数也是2。否则就是3。 codecodecode #inclu…

Spring框架-事务管理注意事项

转载自 Spring框架-事务管理注意事项 常见事务问题 事务不起作用 可能是配置不起效&#xff0c;如扫描问题 事务自动提交了&#xff08;批量操作中&#xff09; 可能是在没事务的情况下&#xff0c;利用了数据库的隐式提交 事务配置说明 通常情况下我们的Spring Component扫…

P2568-GCD【欧拉函数,欧拉筛】

正题 题目链接:https://www.luogu.org/problemnew/show/P2568 题目大意 求有多少个数对满足gcd(x,y)pri(x,y≤n)gcd(x,y)pri(x,y\leq n)gcd(x,y)pri(x,y≤n) 解题思路 首先对于 gcd(x,y)pgcd(x,y)pgcd(x,y)p >gcd(x/p,y/p)1>gcd(x/p,y/p)1>gcd(x/p,y/p)1 那么对数就…

laravel允许所有网站进行跨域操作

共三步&#xff1a; 1、新建中间件&#xff1a; php artisan make:middleware EnableCrossRequestMiddleware2、EnableCrossRequestMiddleware.php中重写中间件里面的内容&#xff1a; <?php namespace App\Http\Middleware; use Closure; class EnableCrossRequestMidd…

Ocelot统一权限验证

Ocelot作为网关&#xff0c;可以用来作统一验证&#xff0c;接上一篇博客Ocelot网关&#xff0c;我们继续 前一篇&#xff0c;我们创建了OcelotGateway网关项目&#xff0c;DemoAAPI项目&#xff0c;DemoBAPI项目&#xff0c;为了验证用户并分发Token&#xff0c;现在还需要添…

P1081-开车旅行【倍增,链表,dp】

正题 题目大意:https://www.luogu.org/problemnew/show/P1081 题目大意 有若干个城市有不同的海拔hhh&#xff0c;两个城市之间的距离定义为∣hx−hy∣|h_x-h_y|∣hx​−hy​∣ 小A每次走次近的&#xff0c;小B每次走最近的。它们轮流开车。且只会往编号更大的城市开。 问一:…

Spring Boot之程序性能监控

转载自 Spring Boot之程序性能监控 Spring Boot特别适合团队构建各种可快速迭代的微服务&#xff0c;同时为了减少程序本身监控系统的开发量&#xff0c;Spring Boot提供了actuator模块&#xff0c;可以很方便的对你的Spring Boot程序做监控。 1. actuator接口说明 Spring B…

laravel部署在linux出现404 not found

laravel项目放在本地服务器后&#xff0c;访问是成功的 http://localhost/blogkjh/public/article 但是在linux服务器上访问显示404 not found 但是我在服务器上使用 php artisan serve --host 0.0.0.0 命令 却可以访问 甚至连swagger都可以访问 关于这个我最近就特别疑…

Visual Studio 2017 15.6版本预览,增加新功能

上周Visual Studio 2017 15.5 版本已正式发布&#xff0c;同时发布的还有 Visual Studio for Mac 7.3 。 Visual Studio 2017 15.6 版本预览&#xff0c;这个最新的预览包含新功能&#xff0c;生产力改进和其他增强功能&#xff0c;以解决客户的反馈意见。 本发行版中的更新摘要…

POJ1275-Cashier Employment【差分约束系统】

正题 题目链接:http://poj.org/problem?id1275 题目大意 1∼241\sim 241∼24小时中第iii个小时需要rir_iri​个出纳员 有nnn个人应聘&#xff0c;第iii从xix_ixi​开始工作&#xff0c;一直工作8个小时。 求至少要招募多少人应聘。 解题思路 numinum_inumi​表示第iii个小时有…

使用 mono 编译 .NET Standard 应用

微软发布 .NET Standard 2.0 已经有一段时间了&#xff0c; 根据 .NET Standard 2.0 支持版本的文档&#xff0c; Mono 5.4 是支持 .NET Standard 2.0 的&#xff0c; 对于 .NET Standard 2.0 应用的开发的介绍&#xff0c; 几乎全部都是在 Windows 系统下使用 Visual Studio 2…