Ocelot 请求聚合
请求聚合需注意以下三点:
-
仅支持
GET
方式 -
下游服务返回类型要求为application/json
-
返回内容类型为application/json,不会返回404请求
以上文中项目为例:https://www.cnblogs.com/1285026182YUAN/p/15234331.html
1. 项目 OService1 增加接口
namespace OService1.Controllers { [Route("api/[controller]")] [ApiController] public class kettleController : ControllerBase { [Route("GetSig")] [HttpGet] public IActionResult GetSig() { return new JsonResult(new { name = "kiti", size = 23 }); } } }
访问接口:
https://localhost:6001/api/kettle/GetSig
2. 项目OService2 增加接口
namespace OService2.Controllers { [Route("api/[controller]")] [ApiController] public class CupController : ControllerBase { [Route("GetCup")] [HttpGet] public IActionResult GetCup() { return new JsonResult(new { name = "cupp", size = 1, foot = new List<string>() { "aa", "bb" } }); } } }
访问接口:
https://localhost:6002/api/cup/getcup
3. 修改Ocelot.json
{ "Routes": [ //路由一 { "DownstreamPathTemplate": "/api/kettle/GetSig", //下游路径 "DownstreamScheme": "https", //http,https "DownstreamHostAndPorts": [ { "Host": "localhost", //下游地址 "Port": 6001 //下游端口 } ], "UpstreamPathTemplate": "/ocelot/GetSig", //上游路径 "UpstreamHttpMethod": [ "Get" ], "Key": "aggr_s1" }, //路由二 { "DownstreamPathTemplate": "/api/cup/getcup", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 6002 } ], "UpstreamPathTemplate": "/ocelot/getcup", "UpstreamHttpMethod": [ "Get" ], "Key": "aggr_s2" } ], "Aggregates": [ { "RouteKeys": [ "aggr_s1", "aggr_s2" ], "UpstreamPathTemplate": "/aggrssr" } ], "GlobalConfiguration": { "BaseUrl": "https://localhost:5001" } }
Ocelot仅支持GET
方式的请求聚合。
Ocelot总是以application/json
的格式返回一个聚合请求的,
当下游服务是返回404状态码,在返回结果中,其对应的值则为空值,
即使聚合路由中所有的下游服务都返回404状态码,聚合路由的返回结果也不会是404状态码。
参考:http://letyouknow.net/ocelot/ocelot-tutorial-2.html
项目:https://gitee.com/wuxincaicai/ocelothost.git