Ocelot网关

Ocelot +.net6

官方文档
虽然现在我们不用这个网关了,还是要记录一下笔记.我是用的是.net6配置的.

1.简单的手动在配置文件中配置服务

  • 安装Nuget Ocelot
  • 新增一个名为ocelot.json的配置文件
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        //可以多个负载均衡
        {
          "Host": "localhost",
          "Port": 5000 //服务端口
        },
        {
          "Host": "localhost",
          "Port": 5001 //服务端口
        }
      ],
      "UpstreamPathTemplate": "/{url}",
      "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ],
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //LeastConnection – 将请求发往最空闲的那个服务器;RoundRobin – 轮流发送;NoLoadBalance – 总是发往第一个请求或者是服务发现
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:8000"
  }
}
  • Program注册使用代码如下
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();

//将ocelot配置问题ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    //追加ocelot配置文件
    config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});

//注册Ocelot
//builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddOcelot();

var app = builder.Build();

app.UseOcelot().Wait();
app.Run();

2.使用Consul作为服务发现

  • Install-Package Ocelot.Provider.Consul
  • 配置ocelot.json配置文件
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",

      "UpstreamPathTemplate": "/{url}",
      "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ],

      "UseServiceDiscovery": true,
      "ServiceName": "service-a",
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //LeastConnection – 将请求发往最空闲的那个服务器;RoundRobin – 轮流发送;NoLoadBalance – 总是发往第一个请求或者是服务发现
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:8000",
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500
    }
  }
}

  • Program注册使用Ocelot和Consul,代码如下
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();

//将ocelot配置问题ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    //追加ocelot配置文件
    config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});

//注册Ocelot
builder.Services.AddOcelot()
.AddConsul()
.AddConfigStoredInConsul();

var app = builder.Build();

app.UseOcelot().Wait();

app.Run();

3.使用Cache

Caching — Ocelot 1.0.0 documentation

  • 安装Nuget Install-Package Ocelot.Cache.CacheManager
  • 注入CacheManager
s.AddOcelot()
    .AddCacheManager(x =>
    {
        x.WithDictionaryHandle();
    })
  • 在具体的Route里面添加如下配置
"FileCacheOptions": { "TtlSeconds": 15, "Region": "somename" }

4.限流

Rate Limiting — Ocelot 1.0.0 documentation

直接配置在具体的Route里面就行了

"RateLimitOptions": {
    "ClientWhitelist": [],//白名单
    "EnableRateLimiting": true,//是否启用限流
    "Period": "1s",//1s,5m,1h,1d
    "PeriodTimespan": 1,//多少秒之后客户端可以重试
    "Limit": 10 //统计时间段之内允许的最大请求数量 
}

还可以设置全局的限流提示

   "GlobalConfiguration": {
    "BaseUrl": "https://localhost:8000",
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500
    },
//全局限流提示配置
    "RateLimitOptions": {
      "DisableRateLimitHeaders": false,
      "QuotaExceededMessage": "Customize Tips!",
      "HttpStatusCode": 999,
      "ClientIdHeader": "Test"
    }
  }

超时/熔断 Polly

官方文档

Install-Package Ocelot.Provider.Polly

注册Polly

//将ocelot配置问题ocelot.json添加到配置提供程序
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
    //追加ocelot配置文件
    config.AddJsonFile("ocelot.json", optional: true, reloadOnChange: true);
});

//注册Ocelot
//builder.Services.AddOcelot(builder.Configuration);
//builder.Services.AddOcelot()
builder.Services.AddOcelot()
.AddCacheManager(x => x.WithDictionaryHandle())
.AddConsul()
.AddConfigStoredInConsul()
.AddPolly();

配置文件使用Polly

"QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,//代表发生错误的次数
        "DurationOfBreak": 10000,//代表熔断时间
        "TimeoutValue": 5000//代表超时时间
      }
原文地址:https://www.cnblogs.com/vsnb/p/15813699.html