tnblog
首页
视频
资源
登录

.netCore3.1 Ocelot 与 Consul 熔断,限流与缓存

7869人阅读 2020/3/3 21:19 总访问:3467409 评论:0 收藏:0 手机
分类: .net后台框架

目录与前言


目录链接:.net core Ocelot 简单网关集群熔断架构整合目录


Unsplashed background img 1


基于上一篇文章展开 OcelotConsul 进行合并


创建一个Ocelot网关项目



1. 创建项目 AiDaSi.OcDemo.GateWay 项目结构如下图所示


2. 双击项目名修改如下

  1. <Project Sdk="Microsoft.NET.Sdk.Web">
  2.   <PropertyGroup>
  3.     <TargetFramework>netcoreapp3.1</TargetFramework>
  4.   </PropertyGroup>
  5.   <ItemGroup>
  6.     <PackageReference Include="Ocelot" Version="14.0.11" />
  7.     <PackageReference Include="Ocelot.Provider.Consul" Version="14.0.11" />
  8.     <PackageReference Include="Ocelot.Provider.Polly" Version="14.0.11" />
  9.   </ItemGroup>
  10. </Project>


3. 修改 Startup.cs 如下

  1. public class Startup
  2. {
  3.     public Startup(IConfiguration configuration)
  4.     {
  5.         Configuration = configuration;
  6.     }
  7.     public IConfiguration Configuration { get; }
  8.     public void ConfigureServices(IServiceCollection services)
  9.     {
  10.         //services.AddControllers();
  11.         services.AddOcelot()
  12.             //使用 counsulconfiguration.json 并添加 Ocelot.Provider.Consul 包
  13.             .AddConsul()
  14.             //使用 counsulpollyconfiguration.json 并添加 Ocelot.Provider.Polly 包
  15.             .AddPolly()
  16.             ;
  17.     }
  18.     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  19.     {
  20.         app.UseOcelot();
  21.     }


4. 创建 configuration.json 


注意所有 json 都需要设置属性


然后先来 康康 负载均衡的配置

  1. {
  2.   "ReRoutes": [
  3.     {
  4.       "DownstreamPathTemplate""/api/{everything}",
  5.       "DownstreamScheme""http",
  6.       "DownstreamHostAndPorts": [
  7.         {
  8.           "Host""localhost",
  9.           "Port"5726
  10.         },
  11.         {
  12.           "Host""localhost",
  13.           "Port"5727
  14.         },
  15.         {
  16.           "Host""localhost",
  17.           "Port"5728
  18.         }
  19.       ],
  20.       "UpstreamPathTemplate""/{everything}",
  21.       "UpstreamHttpMethod": [ "Get""Post" ],
  22.       "LoadBalancerOptions": {
  23.         "Type""RoundRobin"
  24.       }
  25.     }
  26.   ],
  27.   "GlobalConfiguration": {
  28.   }
  29. }


5. 修改 Program.cs

  1. public class Program
  2. {
  3.     public static void Main(string[] args)
  4.     {
  5.         CreateHostBuilder(args).Build().Run();
  6.     }
  7.     public static IHostBuilder CreateHostBuilder(string[] args) =>
  8.         Host.CreateDefaultBuilder(args)
  9.             .ConfigureAppConfiguration((hostingContext, config) =>
  10.             {
  11.                 config
  12.                     .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
  13.                     .AddJsonFile("configuration.json"falsetrue)
  14.                     .AddEnvironmentVariables();
  15.             })
  16.             .ConfigureWebHostDefaults(webBuilder =>
  17.             {
  18.                 webBuilder.UseStartup<Startup>();
  19.             });
  20. }


6. 运行 康康


多节点网关分配


1. 创建的 manyconfiguration.json 配置如下

  1. {
  2.   "ReRoutes": [
  3.     {
  4.       "UpstreamPathTemplate""/T5726/{url}",
  5.       "UpstreamHttpMethod": [ "Get""Post" ],
  6.       "DownstreamPathTemplate""/api/{url}",
  7.       "DownstreamScheme""http",
  8.       "DownstreamHostAndPorts": [
  9.         {
  10.           "Host""localhost",
  11.           "Port"5726
  12.         }
  13.       ]
  14.     },
  15.     {
  16.       "UpstreamPathTemplate""/T5727/{url}",
  17.       "UpstreamHttpMethod": [ "Get""Post" ],
  18.       "DownstreamPathTemplate""/api/{url}",
  19.       "DownstreamScheme""http",
  20.       "DownstreamHostAndPorts": [
  21.         {
  22.           "Host""localhost",
  23.           "Port"5727
  24.         }
  25.       ]
  26.     }
  27.   ],
  28.   "GlobalConfiguration": {
  29.   }
  30. }


2.修改 Program.cs

  1. .AddJsonFile("manyconfiguration.json"falsetrue)


3. 运行结果


好了回归主题!

Ocelot与Consul结合服务进行配置


counsulconfiguration.json

  1. {
  2.   "ReRoutes": [
  3.     {
  4.       "UpstreamPathTemplate""/consul/{url}",
  5.       "UpstreamHttpMethod": [ "Get""Post" ],
  6.       "DownstreamPathTemplate""/api/{url}",
  7.       "DownstreamScheme""http",
  8.       "ServiceName""AiDaSiService",
  9.       "LoadBalancerOptions": {
  10.         "Type""RoundRobin"
  11.       },
  12.       "UseServiceDiscovery"true
  13.     }
  14.   ],
  15.   "GlobalConfiguration": {
  16.     "BaseUrl""http://127.0.0.1:6299",
  17.     "ServiceDiscoveryProvider": {
  18.       "Host""localhost",
  19.       "Port"8500,
  20.       "Type""Consul"
  21.     }
  22.   }
  23. }


过程我都不说了

直接运行



心细的你相信已经看到了,访问的是 node 节点的 hostname

这里是通过 Consul 服务节点进行的访问


接下来我们继续!!!


Ocelot与Consul 熔断与限流配置


counsulpollyconfiguration.json

  1. {
  2.   "ReRoutes": [
  3.     {
  4.       "UpstreamPathTemplate""/consul/{url}",
  5.       "UpstreamHttpMethod": [ "Get""Post" ],
  6.       "DownstreamPathTemplate""/api/{url}",
  7.       "DownstreamScheme""http",
  8.       "ServiceName""AiDaSiService",
  9.       "LoadBalancerOptions": {
  10.         "Type""RoundRobin"
  11.       },
  12.       "UseServiceDiscovery"true,
  13.       //"RateLimitOptions": {
  14.       //  "ClientWhitelist": [], //白名单
  15.       //  "EnableRateLimiting": true,
  16.       //  "Period": "5m",     // 1s,5m,1h,1d
  17.       //  "PeriodTimespan": 5,//多少秒之后客户端可以重试
  18.       //  "Limit": 5          //统计时间段内允许的最大请求数量
  19.       //},
  20.       //缓存
  21.       "FileCacheOptions": {
  22.         "TtlSeconds"10
  23.       }
  24.       //"QoSOptions": {
  25.       //  "ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
  26.       //  "DurationOfBreak": 10000, // 熔断的时间,单位为ms
  27.       //  "TimeoutValue": 10000 // 如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
  28.       //}
  29.     }
  30.   ],
  31.   "GlobalConfiguration": {
  32.     "BaseUrl""http://127.0.0.1:6299",
  33.     "ServiceDiscoveryProvider": {
  34.       "Host""localhost",
  35.       "Port"8500,
  36.       "Type""Consul"
  37.     },
  38.     "RateLimitOptions": {
  39.       "QuotaExceededMessage""Too many requests,maybe later?11"// 当请求过载被截断时返回的消息
  40.       "HttpStatusCode"666 // 当请求过载被截断时返回的http status
  41.     }
  42.   }
  43. }


这里我们缓存了 10s 我们一起来看看是怎么缓存的





感谢各位老铁的观看!!完结!!!撒花!!!


更多参考文献:



Jesse大佬的:

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

http://www.jessetalk.cn/2018/03/19/net-core-apigateway-ocelot-docs/













欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

分布式服务架构微服务架构概念的区别联系

分布式:分散压力。微服务:分散能力。当下理解分布式:不同模块部署在不同服务器上作用:分布式解决网站高并发带来问题集...

jsController中分割字符串的方法

js: varstr=OpenRule; varstrs=newArray(); strs=str.split(&quot;,&quot;); for(vari=0;i&lt;strs.length;i++){ $(&q...

Service-stack.redis配置连接池读写分离(处理并发相关等)

配置连接池与读写分类 //写节点(主节点) List&lt;string&gt;writes=newList&lt;string&gt;(); writes.Add(&quot;123456a...

CSS相对定位绝对定位

一般相对定位和绝对定位可以配合起来使用 例如实现如下的效果 只需要在外层div设置为相对定位,在内部设置为绝对定位就...

C委托事件

1.什么是委托?  委托在C#里的意义和在现实里差不多,从字面意思理解即可。举个例子:领导委托小张去传递个文件,这就是...

asp.net core2.0 依赖注入 AddTransientAddScoped的区别

asp.net core主要提供了三种依赖注入的方式其中AddTransient与AddSingleton比较好区别AddTransient瞬时模式:每次都获取一...

Vue.js+Layer实现表格数据绑定更新

一:使用Vue.js绑定好数据与更新事件 使用v-on绑定好事件,在事件里边直接把该行数据传递进去,在更新方法里边就可以直接...

下划线、换行、回车、空格ASCII码值对照表

下划线,ASCII码95换行 , ASCII码10回车 , ASCII码13空格 , ASCII码32ASCII码表:Bin(二进制)Oct(八进制)Dec(十进制)Hex(...

数据读取器指定的"xx"不兼容。某个类型为"xx"的成员在同名的数据读取器中没有对应的列

报错的地方var result= _db.Database.SqlQuery&lt;SMachine&gt;(sql).FirstOrDefault();经过分析,是因为SqlQuery方法查询...

git 下载提交命令

一.先使用git clone下载一个项目 git clone &#39;项目地址&#39; 这里要注意: clone的项目里边会自带git的一些信息,...

微信开发四 接受用户普通消息回复消息

微信接收用户普通消息的文章可以在官方中直接看微信普通消息分类:接受用户文本消息 与 回复文本信息 注意在接收用户普通...

记忆糖的关系【阅读听力】

Link Between Memory and SugarSugar On The BrainIt’s long been understood that there is a connection between memory...

婚姻心脏健康的关系【阅读听力】

Marriage and Heart HealthPlenty of studies have found that being married is generally good for health. One study ze...

iframe自适应高度配合net core使用

去掉iframe边框frameborder=&quot;0&quot;去掉滚动条scrolling=&quot;no&quot;iframe 自适应高度如果内容是固定的,那么就...

net core中使用url编码解码操作

net core中暂时还没有以前asp.net与mvc中的server对象。获取url的编码与解码操作不能使用以前的server对象来获取。使用的是...

Sqlerver添加用户授权

添加用户安全性--&gt;登录名,然后右键新建登录名就可以了然后填写好相关信息就可以了右键属性,用户映射可以选择该用户可...
这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术