Ocelot快速入门教程
Ocelot是什么
Ocelot是一个用.NET Core实现并且开源的API网关,就像一个公司的门卫承担着寻址、限制进入、安全检查、位置引导、等等功能。它的功能包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器、Service Fabric、Skywalking等的集成。而且这些功能都只需要简单的配置即可完成。
Ocelot部署场景

一般而言,我们不会只部署一台网关服务器,那样太危险了。只要有一台挂了,就全完蛋了。为了实现高可用,我们会部署多台,当然在多台网关前,你还需要一台负载均衡器。Ocelot类型Nginx,内置了负载均衡器,但是他无法提供健康检查功能,服务注册也只能通过手动在配置文件里面添加完成。不够灵活。这个时候我们会采用Consul来做服务发现。Consul与Ocelot完美结合。
Ocelot配置说明
安装直接nuget上下载,再Add一下,这里就不说了。
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/Products/{everything}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/Products/{everything}",
"ServiceName": "ProductService",
"UpstreamHttpMethod": [ "Get" ],
"FileCacheOptions": { "TtlSeconds": 15 },
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
}
},
{
"DownstreamPathTemplate": "/api/Products",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/Products",
"ServiceName": "ProductService",
"UpstreamHttpMethod": [ "Get" ],
"FileCacheOptions": { "TtlSeconds": 15 },
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
}
},
{
"DownstreamPathTemplate": "/api/Offer",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/Offers",
"ServiceName": "PolicyService",
"UpstreamHttpMethod": [ "Post" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
},
"AddHeadersToRequest": {
"AgentLogin" : "Claims[http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier] > value[0] > |"
}
},
{
"DownstreamPathTemplate": "/api/Policy",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/Policies",
"ServiceName": "PolicyService",
"UpstreamHttpMethod": [ "Post" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
}
},
{
"DownstreamPathTemplate": "/api/Policy/{number}",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/Policies/{number}",
"ServiceName": "PolicyService",
"UpstreamHttpMethod": [ "Get" ],
"FileCacheOptions": { "TtlSeconds": 15 },
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
}
},
{
"DownstreamPathTemplate": "/api/PolicySearch",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/PolicySearch",
"ServiceName": "PolicySearchService",
"UpstreamHttpMethod": [ "Get" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "ApiSecurity",
"AllowedScopes": []
},
"RouteClaimsRequirement": {
"userType" : "SALESMAN"
}
}
],
"GlobalConfiguration": {
"RequestIdKey": "OcRequestId",
"AdministrationPath": "/administration",
"UseServiceDiscovery" : true,
"ServiceDiscoveryProvider": { "Type": "Eureka", "Host" : "localhost", "Port" : "8761"}
}
}
两个根节点ReRoutes,GlobalConfiguration。
ReRoutes (告诉Ocelot如何处理上游请求)
【DownstreamPathTemplate】
下游路由模板,即真实处理请求的路径模板
【DownstreamScheme】
请求方式,http/https
【UpstreamPathTemplate】
上游请求的模板,即用户真实请求的链接
【UpstreamHttpMethod】
上游请求的http方法,是个数组,你可以写多个
【LoadBalancerOptions】
LeastConnection : 将请求发往最空闲的那个服务器
RoundRobin :轮流发送
NoLoadBalance :不启用负载均衡,总是发往第一个请求或者服务发现的那个服务器
【RateLimitOptions】
限流相关配置
GlobalConfiguration(顾名思义,全局配置)
包括了服务发现Eureka
Ocelot快速入门教程的更多相关文章
- 专为设计师而写的GitHub快速入门教程
专为设计师而写的GitHub快速入门教程 来源: 伯乐在线 作者:Kevin Li 原文出处: Kevin Li 在互联网行业工作的想必都多多少少听说过GitHub的大名,除了是最大的开源项目 ...
- EntityFramework6 快速入门教程
EntityFramework6 快速入门教程 不得不说EF在国内实在是太小众,相关的技术文章真实屈指可数,而且很多文章都很旧了,里面使用的版本跟如今的EF6差别还是比较大.我刚开始弄这个的时候真是绕 ...
- Apple Watch开发快速入门教程
Apple Watch开发快速入门教程 试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...
- 指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程
指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程 1.4.2 指示灯组 指示灯组的放大图如图1.5所示. 图1.5 指示灯组 各个指示灯对应的功能如下: q RX:对应于0号端口, ...
- 游戏控制杆OUYA游戏开发快速入门教程
游戏控制杆OUYA游戏开发快速入门教程 1.2.2 游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4 游戏控制杆各个角度的 ...
- Query 快速入门教程
Query 快速入门教程 http://www.365mini.com/page/jquery-quickstart.htm#what_is_jquery jquery常用方法及使用示例汇总 http ...
- Realm for Android快速入门教程
介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...
- CMake快速入门教程-实战
http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ http://blog.csdn.net/dbzhang800/article/detai ...
- .NET Core 快速入门教程
.NET Core 快速学习.入门系列教程.这个入门系列教程主要跟大家聊聊.NET Core的前世今生,以及Windows.Linux(CentOS.Ubuntu)基础开发环境的搭建.第一个.NET ...
随机推荐
- 【硬核】Dubbo常见面试题
有情怀,有干货,微信搜索[三太子敖丙]关注这个不一样的程序员. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系列文章. ...
- DE2资源集锦
1.The School of Electrical and Computer Engineering (ECE) at the Georgia Institute of Technology:htt ...
- Prometheus入门教程(二):Prometheus + Grafana实现可视化、告警
文章首发于[陈树义]公众号,点击跳转到原文:https://mp.weixin.qq.com/s/56S290p4j9KROB5uGRcGkQ Prometheus UI 提供了快速验证 PromQL ...
- 安装Node,创建vue项目,运行及打包
1.安装node js 下载地址:http://nodejs.cn/download/ 2.安装完成后运行Node.js command prompt(node -v查看安装版本) 3.安装npm(由 ...
- excel 无效引用 所引用的单元格不能位于256列
无效引用 该文件版本所包含的公式中,所引用的单元格不能位于256列(列IW 或更远)或 65536 行以外的区域. 原因及解决方法: 1.版本问题,把两文件都另存为一致的版本,改为后缀为xlsx. 2 ...
- elasticsearch-安装-centos7- es7.5 搭建
centos6 搭建 参考 https://www.cnblogs.com/php-linux/p/8758788.html 搭建linux虚拟机 https://www.cnblogs ...
- spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)
一,什么是poi? 1,poi poi是用来兼容微软文档格式的java api, 它是apache的顶级项目之一, 也是我们在生产环境中导出excel时使用最多的库 2,poi官方网站: http:/ ...
- centos8安装sersync为rsync实现实时同步
一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...
- pycharm2018.1下载激活(mac平台)
此教程实时更新,请放心使用:如果有新版本出现猪哥都会第一时间尝试激活: pycharm官网下载地址:http://www.jetbrains.com/pycharm/download/ 激活前准备工作 ...
- 通过SQL自动添加流水号
通过SQL自动添加流水号 项目中往往有一些单据流水号或者流程流水号是希望通过新增一条记录后自动产生一个编号的,比如新增一条流程就自动根据当前日期自动添加该流程的流程流水号,下面介绍两种不同类型流水号通 ...