.Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1)
路由介绍
上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes是Ocelot配置文件中最重要的部分,实现了由上游到下游的路由转发。
上一篇文章中使用的configuration.json文件如下:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"UpstreamPathTemplate": "/webapia/values",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}
],
"UpstreamPathTemplate": "/webapib/values",
"UpstreamHttpMethod": [ "Get" ]
}
]
}
Routes是一个数组,其中包含了若干个路由配置,上边的配置文件中包含了2个路由配置,以第一个为例介绍(以下简称配置)。
- DownstreamPathTemplate:下游路径
- DownstreamScheme:下游协议
- DownstreamHostAndPorts:下游主机及端口,该部分为一个数组,包含若干个Host及Port配置
以上三个下游配置组成了下游路由的完整链接,配置的完整链接为:http://localhost:5001/api/values - UpstreamPathTemplate:上游路径
- UpstreamHttpMethod:上游使用的http方法,该部分为一个数组,包含若干个http方法,配置中使用的为get方法
如此组成了一个路由配置,具体实现的功能为:当Ocelot网关接收到链接为http(s)://yourdomain.com(:port)/webapia/values的get方法时转发到http://localhost:5001/api/values
但是在我们的实际应用中不会把所有链接都去配置一个路由(每个链接都去配置这不可能实现),Ocelot为我们提供了占位符(placeholder)。
占位符
继续使用上一篇中创建的项目我们首先修改WebApiA中的ValuesController类中的public string Get(int id)方法
[HttpGet("{id}")]
public string Get(int id)
{
return $"value {id} from WebApiA";
}
同样,WebApiB
[HttpGet("{id}")]
public string Get(int id)
{
return $"value {id} from WebApiB";
}
然后向configuration.json配置文件中的ReRoutes节点添加如下配置:
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/webapia/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
},
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}],
"UpstreamPathTemplate": "/webapib/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
}
以上配置实现
- http(s)://yourdomain.com(:port)/webapia/values/{id} => http://localhost:5001/api/values/{id}
- http(s)://yourdomain.com(:port)/webapib/values/{id} => http://localhost:5002/api/values/{id}
分别运行WebApiA、WebApiB、OcelotGetway,之后浏览器分别访问http://http://localhost:5000/webapia/values/5 http://localhost:5000/webapib/values/6 运行效果如下图
运行效果.png如果需要上游链接对大小写敏感可以添加
ReRouteIsCaseSensitive属性,该属性默认为false,修改配置文件如下(注意UpstreamPathTemplate链接):
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/WebApiA/values/{id}",
"UpstreamHttpMethod": [ "Get" ],
"ReRouteIsCaseSensitive": true
},
{
"DownstreamPathTemplate": "/api/values/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5002
}],
"UpstreamPathTemplate": "/WebApib/values/{id}",
"UpstreamHttpMethod": [ "Get" ]
}
再次运行,浏览器分别访问http://localhost:5000/WebApiA/values/5 http://localhost:5000/webapia/values/5,可以发现大小写拼写有误的链接已经访问不了了

而另一个没有添加ReRouteIsCaseSensitive的配置可以正常访问

完,下一篇将介绍路由聚合
作者:Weidaicheng
链接:https://www.jianshu.com/p/05ccf87a3091
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
.Netcore 2.0 Ocelot Api网关教程(2)- 路由的更多相关文章
- .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理
本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation
本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...
- .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权
本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...
- .Netcore 2.0 Ocelot Api网关教程(1)- 入门
Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...
- .Netcore 2.0 Ocelot Api网关教程(8)- 缓存
Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...
- .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合
在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...
随机推荐
- Ubuntu安装libssl-dev失败(依靠aptitude管理降级软件)并记录dpkg展示安装软件列表
Ubuntu 12.04LTS下直接安装 libssl-dev 失败 提示错误: $ sudo apt-get install libssl-dev Reading package lists... ...
- 学习elasticsearch(一)linux环境搭建(1)
首先安装了Oracle Virtual Box 然后安装了最小版的CentOS.由于vbox自带的操作面板不太好用,于是用了xshell,XShell连接最小版的centOS时遇到的问题记录下. 1. ...
- 打开myeclipse提示An internal error occurred during: "CheckLicensesAndNotify". com/genuitec/pulse2/client/targetcfg/ui/PulseActivator
打开myeclipse提示An internal error occurred during: "CheckLicensesAndNotify". com/genuitec/pul ...
- solrcloud2
分片的原因 由于底层Lucene的限制,每个solr索引中包含的文档数不能超过231个,大约是21亿个.但是solr分片一般不是基于这个的原因,因为一般没有到这个峰值的之后,solr的各中性能问题就暴 ...
- 理解Event冒泡模型
本文探索一下Event的冒泡过程和初学遇到的几个小bug DOM Event概述 Event接口是检测在DOM中的发生的所有事件,我们一直在用,而且从DOM的很早的版本就一直在用着.早期的网景(后来的 ...
- 解读>/dev/null 2>&1
背景 我们经常能在shell脚本中发现>/dev/null 2>&1这样的语句.以前的我并没有去深入地理解这段命令的作用,照搬照用,今天开始去解读>/dev/null 2&g ...
- remmina连接xfce桌面的centos7
vnc无法连到linux server,但ssh可以的解决方法 原文引自:https://blog.csdn.net/h00ahaha/article/details/84440449 今天用vn ...
- 【Android-关闭所有Activity】关闭activity之前的所有activity,重启actibity
Android关闭activity之前的所有activity,重启actibity 直接关闭一个activity之前的所有Activity页面 解决方法:清理activity堆栈 Intent ine ...
- composer查看全局配置
composer config -l -g composer 更新慢 composer下载不下来问题解决 使用 Composer 镜像加速有两种选项: 选项一:全局配置,这样所有项目都能惠及(推荐): ...
- 在一个非默认的位置包含头文件stdafx.h
如果stdafx.h和你当前的工程不在一个文件夹下,当你在代码中第一行写下#include "stdafx.h"时,VC编译器会根据编译规则(相关的规则请查看MSDN)来区别std ...