.Net微服务实践(二):Ocelot介绍和快速开始

上篇.Net微服务实践(一):微服务框架选型 咱们对微服务框架总体作了介绍,接下来咱们从网关Ocelot开始,一一开始实践html

介绍

Ocelot是一个用.NET Core实现而且开源的API网关,它功能强大,包括了:路由、请求聚合、服务发现、认证、鉴权、限流熔断、并内置了负载均衡器与Service Fabric、Butterfly、Tracing集成。这些功能只都只须要简单的配置便可完成,下面咱们会对这些功能的配置一一进行说明。git

基本原理

简单的来讲Ocelot是一堆的asp.net core middleware组成的一个管道。当它拿到请求以后会用一个request builder来构造一个HttpRequestMessage发到下游的真实服务器,等下游的服务返回response以后再由一个middleware将它返回的HttpResponseMessage映射到HttpResponse对象并返回给客户端。github

集成方式

基本集成

用一台web service来host Ocelot,在这里有一个json配置文件,里面设置了全部对当前这个网关的配置。它会接收全部的客户端请求,并路由到对应的下游服务器进行处理,再将请求结果返回。而这个上下游请求的对应关系也被称之为路由。web

集成IdentityServer
json

当咱们涉及到认证和鉴权的时候,咱们能够跟Identity Server进行结合。当网关须要请求认证信息的时候会与Identity Server服务器进行交互来完成。api

网关集群
服务器

只有一个网关是很危险的,也就是咱们一般所讲的单点,只要它挂了,全部的服务全挂。这显然没法达到高可用,因此咱们也能够部署多台Ocelot网关。固然这个时候在多台网关前,你还须要一台负载均衡器app

Consul服务发现
负载均衡

在Ocelot已经支持简单的负载功能,也就是当下游服务存在多个结点的时候,Ocelot可以承担起负载均衡的做用。可是它不提供健康检查,服务的注册也只能经过手动在配置文件里面添加完成。这不够灵活而且在必定程度下会有风险。这个时候咱们就能够用Consul来作服务发现,它能与Ocelot完美结合。框架

集成Service Fabric

快速开始

建立订单服务

  • 新建一个新建asp.net core web api项目,命名为order-api, 模拟订单服务
  • 在order-api项目中,添加一个order api
[ApiController]
public class OrderController : ControllerBase
{
   // GET: api/Product
    [Route("api/orders")]
    [HttpGet]
    public  IEnumerable<string> Get()
    {
        return new string[] { "刘明的订单", "王天的订单" };
    }
}
  • 服务启动地址配置为http://localhost:5001

建立产品服务

  • 新建一个新建asp.net core web api项目,命名为product-api, 模拟产品服务
  • 在product-api项目中,添加一个product api
[ApiController]
public class ProductController : ControllerBase
{
    // GET: api/Product
    [Route("api/products")]
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "笔记本", "口罩" };
    }

}
  • 服务启动地址配置为http://localhost:5002

建立网关

  • 新建一个新建asp.net core web api项目,命名为ocelot-gateway, 这是网关

    项目结构图

  • Nuget添加Ocelot package

  • 添加Ocelot配置文件, 命名为Ocelot.json (放在appsettings.json平级目录)
    。咱们暂且无论配置文件内容的含义,先快速把网关运行起来

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/orders",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/api/orders",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/api/products",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5002
        }
      ],
      "UpstreamPathTemplate": "/api/products",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000"
  }
}
  • 修改Program.cs文件,添加Ocelot配置文件
Host.CreateDefaultBuilder(args)
 .ConfigureAppConfiguration((hostingContext, config) =>
 {
     config
         .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
         .AddJsonFile("appsettings.json", true, true)
         .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
         .AddJsonFile("Ocelot.json",false,true)
         .AddEnvironmentVariables();
 })
  • 修改Startup.cs, 在ConfigureServices中注入Ocelot组件
services.AddOcelot()
  • 修改Startup.cs, 在Configure中添加Ocelot中间件
app.UseOcelot().Wait();

-- 服务启动地址配置为http://localhost:5000

运行验证

最后

本篇咱们介绍了Ocelot,它的实现原理,以及如何快速建立ocelot的helo world项目,下面咱们会介绍Ocelot的主要特性:路由

示例代码下载地址: https://github.com/lcyhjx/ocelot-demo/tree/master

相关文章
相关标签/搜索