Ocelot路由配置路径冲突
Path conflict in Ocelot routing configuration
你会如何设置重新路由遵循两组相似的路由到不同的机器。请指教
Problem/Question: 冲突情况在 /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"
}
}
只是在评论中根据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}",
...
},
...
],
...
}
或使用优先级属性,优先级用于指示所需的评估顺序:
{
"ReRoutes":[
{
"UpstreamPathTemplate":"/customers/{id}",
"Priority": 0,
...
},
{
"UpstreamPathTemplate":"/customers/{id}/products", // <-- will be evaluated first
"Priority": 1,
...
},
...
],
...
}
你会如何设置重新路由遵循两组相似的路由到不同的机器。请指教
Problem/Question: 冲突情况在 /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"
}
}
只是在评论中根据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}",
...
},
...
],
...
}
或使用优先级属性,优先级用于指示所需的评估顺序:
{
"ReRoutes":[
{
"UpstreamPathTemplate":"/customers/{id}",
"Priority": 0,
...
},
{
"UpstreamPathTemplate":"/customers/{id}/products", // <-- will be evaluated first
"Priority": 1,
...
},
...
],
...
}