.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 ...
随机推荐
- HTML禁用一块区域点击
style="pointer-events: none;" 此方法可以禁止鼠标点击指定区域,但是对于键盘事件无法屏蔽,最好禁用一下键盘事件,如:tab
- (三)wait()、notify()、notifyAll()
有新理解持续更新 轮询 线程本身是操作系统中独立的个体,但是线程与线程之间不是独立的个体,因为它们彼此之间要相互通信和协作. 想像一个场景,A线程做int型变量i的累加操作,B线程等待i到了10000 ...
- Springboot打包成jar包形式发布
1.修改配置文件pom.xml 添加打包形式设置为jar形式 <packaging>jar</packaging> 2.在build标签内添加内容如下 finalname为打包 ...
- Java实现一行一行读取文件内容(进行编码处理)
// 读取文件内容public String readFile(){ String path = ""; File file = new File(path); StringBui ...
- C# Tcp和Socket 网络(五)
TcpReceive public Form1() { InitializeComponent(); new Thread(() => { IPAddress ip = IPAddress.Pa ...
- 03 Vue -课程详细(传参id)、图片显示、推荐课程(主动重定向)
1.CourseDetail 课程详细信息 1.如何传入参数id (1)router中导入 (2) router-link 关联子组件 (3)detail.vue接受id (4)通过id查询详细 ...
- MFC 树形控件
Tree Control属性:Has Buttons.Has Lines.Lines At Root这三个设为True 常用的事件:SelectChanged() ico图片放到项目的res文件夹中( ...
- MacOs High Sierra 升级失败解决办法
进入recovery的方法: Command-R 重新安装您在 Mac 上安装过的最新 macOS,但不会升级到更高的版本. Option-Command-R升级到与您的 Mac 兼容的最新 macO ...
- HTML JS 弹层后底部页面禁止滚动处理
1.打开新页面时需要禁止鼠标滚轮,禁止页面滑动: 1 2 3 4 在调用显示层时加上这句js代码就可以了: document.documentElement.style.overflow = &quo ...
- Codevs 4909 寂寞的堆
4909 寂寞的堆 时间限制: 1 s 空间限制: 8000 KB 题目等级 : 大师 Master 题目描述 Description 堆,是一种神奇的数据结构 不寂寞的堆,是一棵满二叉树,其儿子节点 ...