zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    腾讯与唯品会笔试面试经历
    JavaCodeTra 猴子选猴王 约瑟夫循环
    HBase开发错误记录(一):java.net.UnknownHostException: unknown host: master
    fedora
    Qt5.1 静态编译
    Linux/Ubuntu下 静态编译Qt程序
    地铁车型
    交流屏和直流屏的区别
    不间断电源(UPS)
    一级负荷供电
  • 原文地址:https://www.cnblogs.com/1285026182YUAN/p/15238226.html
Copyright © 2011-2022 走看看