zoukankan      html  css  js  c++  java
  • .Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合

     

    在实际的应用当中,经常会遇到同一个操作要请求多个api来执行。这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别、年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelot中为我们提供了很好的解决方法。

    路由聚合

    继续使用前边的文章建立的项目,在WebApiA项目中添加一个新的WebApi控制器命名为UserController,代码如下:

    [Produces("application/json")]
    [Route("api/[controller]/[action]")]
    public class UserController : Controller
    {
        [HttpGet]
        public string GetSex(string name)
        {
            if (name == "Jonathan")
                return "Man";
            return null;
        }
    
        [HttpGet]
        public int? GetAge(string name)
        {
            if (name == "Jonathan")
                    return 24;
            return null;
        }
    }
    

    启动WebApiA,然后使用Postman分别访问
    http://localhost:5001/api/User/GetSex?name=Jonathan
    http://localhost:5001/api/User/GetAge?name=Jonathan
    结果如下:

     
    GetSex.png
     
    GetAge.png


    修改configuration.json文件,向ReRoutes节点中添加如下配置:

    {
        "DownstreamPathTemplate": "/api/User/GetSex",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
        {
            "Host": "localhost",
            "Port": 5001
        }],
        "UpstreamPathTemplate": "/Sex",
        "UpstreamHttpMethod": [ "Get" ],
        "Key": "Sex"
    },
    {
        "DownstreamPathTemplate": "/api/User/GetAge",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
        {
            "Host": "localhost",
            "Port": 5001
        }],
        "UpstreamPathTemplate": "/Age",
        "UpstreamHttpMethod": [ "Get" ],
        "Key": "Age"
    }
    

    在与ReRoutes同级的位置添加如下配置:

    "Aggregates": [
    {
        "ReRouteKeys": [
        "Sex",
        "Age"
        ],
        "UpstreamPathTemplate": "/GetUserInfo"
    }]
    

    请求聚合执行的操作为:当请求GetUserInfo时,自动到Reoutes中查找ReRouteKeys下Key值相同的路由,并全部请求,然后将请求结果拼成Json格式返回。
    Postman访问http://localhost:5000/GetUserInfo,结果如下:

     
    GetUserInfo_1.png


    Ocelot请求聚合现在只支持get方法,点击https://github.com/ThreeMammals/Ocelot/blob/master/src/Ocelot/Configuration/File/FileAggregateReRoute.cs查看源码及注释说明。

    请求聚合的页面404

    请求聚合下不会对404的页面返回任何结果,我们现在在configuration.json文件中的ReRoutes节点下添加如下内容:

    {
        "DownstreamPathTemplate": "/api/User/GetID",
        "DownstreamScheme": "http",
        "DownstreamHostAndPorts": [
        {
            "Host": "localhost",
            "Port": 5001
        }],
        "UpstreamPathTemplate": "/ID",
        "UpstreamHttpMethod": [ "Get" ],
        "Key": "ID"
    }
    

    向Aggregates节点下的ReRoutesKeys节点下添加一个ID
    注意:在WebApiA项目中是没有/api/User/GetID方法的,所以会返回404
    然后我们启动项目访问http://localhost:5000/api/GetUserInfo,结果如下:

     
    GetUserInfo_2.png


    可以看到返回的数据中ID为空。
    源码下载

    完,下一篇介绍服务发现

     
     
    1人点赞
     
    Ocelot
     
  • 相关阅读:
    《网络对抗技术》Exp6 MSF应用基础
    用Onenote写博客日志 
    C语言文法
    0909
    使用jQuery解决溢出文本省略
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    jQuery实现动态加载大尺寸图片
    常用jQuery插件推荐
    使用不带单位的lineheight
    JavaScript懒加载技术 lazyload
  • 原文地址:https://www.cnblogs.com/lhxsoft/p/11724721.html
Copyright © 2011-2022 走看看