Ocelot 请求聚合

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

 

posted @   无心々菜  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示