.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 ...
随机推荐
- ShedLock日常使用
首发于个人博客:ShedLock日常使用 场景模拟 定时器Scheduler在平时使用比较频繁,比如定时数据整理,定时向客户发送问候信息等...,定时任务的配置比较简单,比如在springboot中, ...
- linux批量添加用户和批量修改密码
一.批量创建用户通过命令newusers可以实现批量的创建用户.这个命令的用法为 newusers file.txt(一个文本文件)文本文件内存放需要批量添加的用户信息但是对格式有要求格式:pw_na ...
- Java笔记(第六篇-网络通信)
TCP/IP模式是一种层次结构,共分为四层,分别为应用层.传输层.互联网层和主机到网络层. 在TCP/IP协议栈中,有两个高级协议是网络应用程序编写者应该了解的,即“传输控制协议”(TCP)与“用户数 ...
- Apache、nginx 、lighttpd性能比较
Apache.nginx .lighttpd性能比较 1. web服务器简介 1. lighttpd Lighttpd是一个德国人领导的开源软件,其根本的目的是提供一个专门针对高性能网站,安全.快速. ...
- python打开excel跳转对应分页
需求: 在python脚本层,直接打开excel,跳转特定分页,方便策划编辑 尝试了几种不同的方法,最终选择了方法4.4种方法都可以实现打开外部文件,3.4可以实现跳转特定分页.3和4的区别是,3通过 ...
- springboot 详解RestControllerAdvice(ControllerAdvice)(转)
springboot 详解RestControllerAdvice(ControllerAdvice)拦截异常并统一处理简介 @Target({ElementType.TYPE}) @Retentio ...
- PHP mysqli_get_connection_stats() 函数
定义和用法 mysqli_get_connection_stats() 函数返回有关客户端连接的统计. 语法 mysqli_get_connection_stats(connection); 返回有关 ...
- 【Eureka】 作为服务注册中心,Eureka比Zookeeper好在哪里
著名的 CAP 理论指出,一个分布式系统不可能同时满足 C(一致性) A(可用性) 和 P(分区容错性).由于分区容错性 P 是在分布式系统中必须保证的,因此我们只能在 A 和 C 之间进行权衡. Z ...
- Nginx反向代理设置header
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- HearthBuddy CSharpCodeProvider 如何编译cs文件
源码 源码1 namespace Hearthbuddy.Windows{ // Token: 0x02000041 RID: 65 public class MainWindow : W ...