上一次我们通过一张架构图(.net core with 微服务 – 架构图)来讲述了微服务的结构,分层等内容。从现在开始我们开始慢慢搭建一个最简单的微服务架构。这次我们先用几个简单的 web api 项目以及 ocelot 网关项目来演示下网关是如何配置,如何工作的。

ocelot 网关

ocelot 是使用 asp.net core 开发的一个 api 网关项目。它功能丰富,集成了路由、限流、缓存、聚合等功能。它使用 .net 编写,本质上就是一堆 asp.net core 的中间件,所以它天生对 .net 友好。这些中间件拦截外部的请求,根据路由配置转发到对应的内部服务上,再把内部的返回结果对外暴露。

搭建项目结构

新建一个解决方案,新建几个项目。

  • api_gateway api网关
  • hotel_base 酒店基本信息服务
  • member_center 会员中心服务
  • ordering 订单服务

安装 ocelot

在api网关项目上使用nuget安装ocelot的类库。ocelot本质上就是一堆 asp.net core 的 middleware。所以我们需要在useocelot扩展方法在注册这些中间件。

install-package ocelot
        public static void main(string[] args)
        {
            new webhostbuilder()
              .usekestrel()
              .usecontentroot(directory.getcurrentdirectory())
              .configureappconfiguration((hostingcontext, config) =>
              {
                  config
                      .setbasepath(hostingcontext.hostingenvironment.contentrootpath)
                      .addjsonfile($"appsettings.{hostingcontext.hostingenvironment.environmentname}.json", true, true)
                      .addjsonfile("routes.json")
                      .addenvironmentvariables();
              })
              .configureservices(s => {
                  s.addocelot();
              })
              .configurelogging((hostingcontext, logging) =>
              {
                  logging.addconsole();
               })
              .useiisintegration()
              .configure(app =>
              {
                  app.useocelot().wait();
              })
              .build()
              .run();
        }
    
    }

在 main 函数内注册ocelot的中间件,服务,使用addjsonfile指定路由的配置文件。

路由

ocelot最基本的功能就是反向代理。代理的配置通过一个json文件来配置。下面让我们来简单的演示下如何配置。
以下是通过网关代理访问酒店服务的酒店列表的配置示例。

 {
      //获取酒店列表
      "upstreampathtemplate": "/api/hotel",
      "upstreamhttpmethod": [ "get" ],
      "downstreampathtemplate": "/hotel",
      "downstreamscheme": "http",
      "downstreamhostandports": [
        {
          //hotel service
          "host": "localhost",
          "port": 6003
        }
      ]
}

配置主要是分为upstream跟downstream两部分。upstream其实就是指代ocelot网关本身。downstream代表真正的服务。

  • upstreampathtemplate 网关匹配的路径
  • upstreamhttpmethod 网关匹配的请求方法
  • downstreampathtemplate 服务匹配的路径
  • downstreamscheme 服务的scheme,http、https
  • downstreamhostandports 服务的主机地址跟端口

上面的配置描述的意思是:把对网关的/api/hotel的get请求转发到主机http://localhost:6003/hotel接口上。

路由参数

ocelot的path模板可以使用{param}模式来匹配参数,然后传递到下游服务器上。

    {
      //获取单个酒店
      "upstreampathtemplate": "/api/hotel/{hotel_id}",
      "upstreamhttpmethod": [ "get" ],
      "downstreampathtemplate": "/hotel/{hotel_id}",
      "downstreamscheme": "http",
      "downstreamhostandports": [
        {
          //hotel service
          "host": "localhost",
          "port": 6003
        }
      ],
      "key": "hotel_base_info",
    }

使用{hotel_id}匹配hotelid参数。

    {
      //获取酒店房间列表
      "upstreampathtemplate": "/api/hotel_rooms/{hotel_id}",
      "upstreamhttpmethod": [ "get" ],
      "downstreampathtemplate": "/room/hotel_rooms/{hotel_id}",
      "downstreamscheme": "http",
      "downstreamhostandports": [
        {
          //hotel service
          "host": "localhost",
          "port": 6003
        }
      ],
      "key": "hotel_rooms"
    }

使用{hotel_id}匹配hotelid参数。

    {
      //获取查询订单
      "upstreampathtemplate": "/api/order/query?day={day}",
      "upstreamhttpmethod": [ "get" ],
      "downstreampathtemplate": "/order/get_orders?day={day}",
      "downstreamscheme": "http",
      "downstreamhostandports": [
        {
          //order service
          "host": "localhost",
          "port": 6001
        }
      ]
    }

在querystring上使用{day}匹配参数。

限流

ocelot支持对请求的限流操作。

 "ratelimitoptions": {
        "enableratelimiting": true,
        "period": "1s",
        "periodtimespan": 1,
        "limit": 1
      }

在路由配置节点添加ratelimitoptions节点。

  • enableratelimiting = true 开启限流
  • period = 1s 限流的时间区间为1s
  • periodtimespan = 1 限流后重置时间
  • limit = 1 限制请求的数量

上面的配置的意思是1秒内限制一次请求,1秒后重置这个限制。

缓存

ocelot可以对请求的响应值提供缓存服务。

//缓存5s
      "filecacheoptions": { "ttlseconds": 5 }

在路由配置节点上配置filecacheoptions字段,ttlseconds代表需要缓存的时间,单位是秒。

聚合

上一回我们讲微服务架构的时候说到“聚合服务层”,我们说这一层的主要功能是对请求进行聚合适配跟裁剪。其实ocelot已经提供了简单的api聚合功能。如果聚合的需求比较简单,那么可以使用ocelot直接实现。

简单聚合

简单聚合可以通过配置把几个请求的聚合成一个请求,一次性返回几个请求的响应。响应通过json格式被包装返回。

  "aggregates": [
    {
      //聚合 查询酒店信息跟酒店房间列表
      "routekeys": [
        "hotel_base_info",
        "hotel_rooms"
      ],
      "upstreampathtemplate": "/api/hotel_detail/{hotel_id}"
    },
  ]

routekeys 代表需要聚合的请求的键值。

使用代码聚合

上面我们直接通过配置实现了api之间聚合请求。这种聚合比较简单,会把聚合的几个请求的响应值原封不动的返回回来。有的时候我们需要对返回值做一些转换或者裁剪,比如同一个api我们对移动端的响应可能需要裁剪掉部分字段。这种需求在ocelot内我们可以使用代码来完成。
这里不太推荐这种聚合方式,这会造成网关跟下游服务的强耦合关系。

这里我们演示下如何把获取酒店信息跟酒店房间列表的返回值进行裁剪,并返回一个新的响应。

    public class hoteldetailinfoformobileaggregator : idefinedaggregator
    {
        public async task<downstreamresponse> aggregate(list<httpcontext> responses)
        {
            dynamic hotelinfo = new expandoobject();
            list<dynamic> rooms = new list<dynamic>();
            foreach (var context in responses)
            {
                if ((context.items["downstreamroute"] as dynamic).key == "hotel_base_info")
                {
                    var respcontent = await context.items.downstreamresponse().content.readasstringasync();
                    hotelinfo = jsonconvert.deserializeobject<dynamic>(respcontent);
                }
                if ((context.items["downstreamroute"] as dynamic).key == "hotel_rooms")
                {
                    var respcontent = await context.items.downstreamresponse().content.readasstringasync();
                    rooms = jsonconvert.deserializeobject<list<dynamic>>(respcontent);
                }
            }

            dynamic newresponse = new expandoobject();
            newresponse.hotel = new { 
                hotelinfo.id,
                hotelinfo.name
            };
            newresponse.rooms = rooms.select(x => new { 
                x.id,
                x.no
            });

            var stringcontent = new stringcontent(jsonconvert.serializeobject(newresponse));

            return new downstreamresponse(
                stringcontent, 
                system.net.httpstatuscode.ok, 
                responses.selectmany(x => x.items.downstreamresponse().headers).tolist(),
                "ok");
        }
    }

每一个聚合都需要继承idefinedaggregator这个接口然后实现aggregate方法。在这个方法内对每个请求的响应值进行裁剪,然后重新组合。

    {
      //聚合 查询酒店信息跟酒店房间列表 移动端 裁剪
      "routekeys": [
        "hotel_base_info",
        "hotel_rooms"
      ],
      "upstreampathtemplate": "/api/m/hotel_detail/{hotel_id}",
      "aggregator": "hoteldetailinfoformobileaggregator"
    }

在配置文件的aggregates内添加一个配置节点在“aggregator”字段上指定aggregator的类名。

  .configureservices(s => {
                  s.addocelot()
                  .addtransientdefinedaggregator<hoteldetailinfoformobileaggregator>();
              })

同时在configureservices方法内配置hoteldetailinfoformobileaggregator的依赖注入。

总结

本次我们通过几个最简单的web api项目,演示了如何使用 ocelot 网关进行反向代理,限流,聚合等常用功能。可以看到 ocelot 的配置使用还是比较简单的。因为是 .net 代码编写,所以对.net 开发者比较友好,我们可以直接使用 .net 代码来编写一些功能,比如直接使用代码来聚合请求的结果。

相关文章

net core with 微服务 – 什么是微服务
.net core with 微服务 – 架构图

演示代码

关注我的公众号一起玩转技术