.NET5微服务示例-Ocelot网关

文档:https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html

使用:

https://www.cnblogs.com/jesse2013/p/net-core-apigateway-ocelot-docs.html

http://letyouknow.net/ocelot/index.html

接着这篇文章:.NET5微服务示例-Polly熔断与降级

本文交互草图:

1、新建一个空的Web项目,命名为“SGZ.Gateway”,并安装 Ocelot Nuget包

2、Startup类配置如下

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot();
}

3、在Program类的CreateHostBuilder中加入

.ConfigureAppConfiguration(configure => {
    configure.AddJsonFile("ocelot.json", false, true);
})

4、创建 ocelot.json 配置文件,配置信息如下面的案例

4.1、配置文件案例1-基本使用

{
  //以前版本叫ReRoutes
  "Routes": [
    //部门服务示例
    {
      //上游访问地址,也就是客户端在浏览输入的地址
      "UpstreamPathTemplate": "/DepartmentService",
      //上游支持的请求方式,可以多个:[ "GET", "POST" ]
      "UpstreamHttpMethod": [ "GET" ],
      //下游访问地址,每个服务的路由地址
      "DownstreamPathTemplate": "/api/Department/GetList",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 551
        }
      ]
    },
    //人员服务示例
    {
      "UpstreamPathTemplate": "/PersonnelService",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/api/Personnel/GetList",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 661
        }
      ]
    }
  ]
}

访问部门服务:http://localhost:5000/DepartmentService

访问人员服务:http://localhost:5000/PersonnelService

4.2、配置文件案例2-通配符

{
  "Routes": [
    //部门服务示例
    {
      "UpstreamPathTemplate": "/DepartmentService/{url}",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 551
        }
      ]
    }
  ]
}

访问部门服务:http://localhost:5000/DepartmentService/api/Department/GetList

因为部门服务的路由是下图的配置,所以{url}就替换为“api/Department/GetList”

或者多个通配符:

4.3、配置文件案例3-负载均衡

{
  "Routes": [
    //部门服务示例
    {
      "UpstreamPathTemplate": "/DepartmentService/{url}",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 551
        },
        //增加多个实例地址
        {
          "Host": "192.168.2.119",
          "Port": 552
        },
        {
          "Host": "192.168.2.119",
          "Port": 553
        }
      ],
      //负载均衡配置:https://ocelot.readthedocs.io/en/latest/features/loadbalancer.html
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    }
  ]
}

部门服务加上调试信息,然后进入Debug开启多个cmd来运行部门服务

多次访问http://localhost:5000/DepartmentService/api/Department/GetList,就可以看到控制台打印出了调试信息 

4.4、配置文件案例4-结合Consul

{
  "Routes": [
    //部门服务示例
    {
      "UpstreamPathTemplate": "/DepartmentService/{url}",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      //表示DownstreamHostAndPorts配置的实例,从Consul中获取
      "ServiceName": "DepartmentService",
      //"DownstreamHostAndPorts": [
      //  {
      //    "Host": "192.168.2.119",
      //    "Port": 551
      //  },//  {
      //    "Host": "192.168.2.119",
      //    "Port": 552
      //  },
      //  {
      //    "Host": "192.168.2.119",
      //    "Port": 553
      //  }
      //],
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      }
    }
  ]
}

SGZ.Gateway项目,安装 Ocelot.Provider.Consul Nuget包,再配置Startup类;然后运行如案例3

4.5、配置文件案例5-结合Polly

{
  "Routes": [
    //部门服务示例
    {
      "UpstreamPathTemplate": "/DepartmentService/{url}",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 551
        }
      ],
      "QoSOptions": {
        //断路器次数(发生几次请求异常后进行熔断)
        "ExceptionsAllowedBeforeBreaking": 3,
        //断路器熔断时间(毫秒)
        "DurationOfBreak": 30000,
        //超时时间
        "TimeoutValue": 5000
      }
    }
  ]
}

SGZ.Gateway项目,安装 Ocelot.Provider.Polly Nuget包,再配置Startup类

4.6、配置文件案例6-限流

{
  "Routes": [
    //部门服务示例
    {
      "UpstreamPathTemplate": "/DepartmentService/{url}",
      "UpstreamHttpMethod": [ "GET" ],
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "192.168.2.119",
          "Port": 551
        }
      ],
      "RateLimitOptions": {
        //白名单,不受限流控制
        "ClientWhitelist": [],
        //是否开启限流
        "EnableRateLimiting": true,
        //限流的时间,如:1s, 5m, 1h, 1d
        "Period": "1s",
        //在限流控制时间段内最大访问数
        "Limit": 1,
        //多少秒后客户端可以重试
        "PeriodTimespan": 10
      }
    }
  ]
}

例子表示,1秒内只允许1条请求

4.7、配置文件案例7-动态路由

参考:https://www.cnblogs.com/irocker/p/ocelot-servicediscovery.html

原文:https://ocelot.readthedocs.io/en/latest/features/servicediscovery.html

{
  "Routes": [],
  "Aggregates": [],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    },
    "LoadBalancerOptions": {
      "Type": "LeastConnection"
    },
    "DownstreamScheme": "http"
  }
}

比如访问http://localhost:5000/DepartmentService/api/Department/GetList,它会从Consul中查找DepartmentService服务,并且拼接后面的api地址。所以聚合服务也需要注册到Consul中

本文代码:https://files.cnblogs.com/files/shousiji/net5_ocelot.rar

原文地址:https://www.cnblogs.com/shousiji/p/15271767.html