.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" ]
}

以上配置实现

{
"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,可以发现大小写拼写有误的链接已经访问不了了

 
运行效果.png

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

 
运行效果.png

源码下载

完,下一篇将介绍路由聚合

作者:Weidaicheng
链接:https://www.jianshu.com/p/05ccf87a3091
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

.Netcore 2.0 Ocelot Api网关教程(2)- 路由的更多相关文章

  1. .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理

    本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...

  2. .Netcore 2.0 Ocelot Api网关教程(7)- 限流

    本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对  ...

  3. .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation

    本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...

  4. .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权

    本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...

  5. .Netcore 2.0 Ocelot Api网关教程(1)- 入门

    Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...

  6. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  7. .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现

    本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...

  8. .Netcore 2.0 Ocelot Api网关教程(8)- 缓存

    Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...

  9. .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合

    在实际的应用当中,经常会遇到同一个操作要请求多个api来执行.这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别.年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelo ...

随机推荐

  1. File "/usr/bin/pip", line 11, in <module> sys.exit(__main__._main()) AttributeError: 'module' object has no attribute '_main'

    多个版本pip共存导致 直接使用pip2进行安装即可 如:pip2 install requests

  2. java实现简单的单点登录 (转)

    摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...

  3. python的内置函数(一)

    1.数学计算函数 abs(x) 求绝对值1.参数可以是整型,也可以是复数2.若参数是复数,则返回复数的模 complex([real[, imag]]) 创建一个复数 divmod(a, b) 分别取 ...

  4. Java8-Stream-No.10

    import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.u ...

  5. centos6.5解压及压缩zip压缩包

    查看zip压缩文件的内容而不解压:unzip -l filename.zip 将zip包解压到指定路径(若不指定路径则为当前目录):unzip filename.zip -d /usr/file 压缩 ...

  6. delete elasticsearch

    在elasticsearch-head 插件中遇到的删除特定的数据需求 DELETE /索引名/需要清空的type/_query { "query": { "match_ ...

  7. YOLOv3的Darknet在OpenCV3.4.1(bug)下编译出错填坑

    刚配置完环境 https://www.cnblogs.com/clemente/p/11029117.html 能正常跑原版 darknet ,但是跑了一下别人修改的版本出现了错误 查Google之后 ...

  8. vue实战教程

    转载自 https://www.cnblogs.com/sunsets/p/7760454.html

  9. Leetcode题目141.环形链表(简单)

    题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...

  10. CISCO实验记录一:路由器基本配置

    一.路由器基本配置要求 1.设置路由器名为:hehe 2.设置特权模式下password为ccna,secret为ccnp,vty线路密码为ccie 3.所有明文密码都加密 二.路由器基本配置命令 1 ...