zoukankan      html  css  js  c++  java
  • .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" ]
    }
    

    以上配置实现

    {
        "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
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    Kubernetes 再深入一点点
    10分钟完成 mongodb replSet 部署
    网络篇
    p2p 打洞专场(转)
    Dockerfile 编写
    关于github 代码管理,协作开发
    Kubernetes 基于 ubuntu18.04 手工部署 (k8s)
    备忘 ubuntu ip 及 dns 的坑
    各种语言web性能简单对比测试
    vue 按需加载
  • 原文地址:https://www.cnblogs.com/lhxsoft/p/11724707.html
Copyright © 2011-2022 走看看