【问题标题】:Path conflict in Ocelot routing configurationOcelot路由配置路径冲突
【发布时间】:2020-02-11 14:10:13
【问题描述】:

您将如何根据两组相似的路由设置重新路由到不同的机器。请指教。

问题/问题:冲突的情况是在/customers/1/customers/1/products 之间,每个人都去不同的机器。

- 机器名称:customer

GET /customers
GET /customers/1
POST /customers
PUT /customers1
DELETE /customers/1

- 机器名称:customerproduct

GET /customers/1/products
PUT /customers/1/products

ocelot.json

 {
  "ReRoutes": [

    {  
      "DownstreamPathTemplate": "/customers/{id}", 
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "customer",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/customers/{id}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
    },

    {
      "DownstreamPathTemplate": "/customers/{id}/products",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "customerproduct",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/customers/{id}/products",
      "UpstreamHttpMethod": [ "Get", "Put" ]
    }

  ],

  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:80"
  }

}

【问题讨论】:

  • 为什么这不起作用?你得到什么错误?
  • 问题是/customers/{id}/customers/1/customers/1/product1 拦截
  • 通过更改顺序解决,将最具体的路线上移到通用路线。
  • 你想添加这个作为答案还是我应该这样做:)
  • 我已经做到了。希望没关系

标签: .net-core microservices url-routing service-discovery ocelot


【解决方案1】:

只是根据 OP 在 cmets 中自己的解决方案添加一个答案。

这不起作用的原因是评估顺序。 Ocelot 将按照指定的顺序评估路径,除非在路由上指定了 priority 属性(请参阅文档https://ocelot.readthedocs.io/en/latest/features/routing.html#priority

所以在以下路由配置中:

{
   "ReRoutes":[
      {
         "UpstreamPathTemplate":"/customers/{id}", // <-- will be evaluated first
         ...
      },
      {
         "UpstreamPathTemplate":"/customers/{id}/products",
         ...
      },
      ...
   ],
   ...
}

将评估第一个路径,即使上游调用指定了 /products 子路径,Ocelot 也会匹配到该路径。

要解决此问题,要么更改顺序,以便首先评估更具体的路径:

{
   "ReRoutes":[
      {
         "UpstreamPathTemplate":"/customers/{id}/products", // <-- will be evaluated first
         ...
      },
      {
         "UpstreamPathTemplate":"/customers/{id}",
         ...
      },
      ...
   ],
   ...
}

使用优先级属性,优先级用于指示所需的评估顺序:

{
   "ReRoutes":[
      {
         "UpstreamPathTemplate":"/customers/{id}",
         "Priority": 0, 
         ...
      },
      {
         "UpstreamPathTemplate":"/customers/{id}/products", // <-- will be evaluated first
         "Priority": 1, 
         ...
      },
      ...
   ],
   ...
}

【讨论】:

  • 感谢优先示例。
猜你喜欢
  • 2016-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2017-04-15
  • 2018-03-25
  • 2018-06-07
  • 2018-05-23
相关资源
最近更新 更多