1

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

问题/问题:冲突的情况在/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

豹猫.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"
  }

}
4

1 回答 1

1

只需在评论中添加基于 OP 自己的解决方案的答案。

这不起作用的原因是因为评估顺序。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}",
         ...
      },
      ...
   ],
   ...
}

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

{
   "ReRoutes":[
      {
         "UpstreamPathTemplate":"/customers/{id}",
         "Priority": 0, 
         ...
      },
      {
         "UpstreamPathTemplate":"/customers/{id}/products", // <-- will be evaluated first
         "Priority": 1, 
         ...
      },
      ...
   ],
   ...
}
于 2019-11-19T11:00:14.167 回答