.net Core微服务架构之Gateway,过载保护,异常处理,熔断,缓存

结构图

Program类代码
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(conf =>
            {
                conf.AddJsonFile("configuration.json", optional: false, reloadOnChange: true);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
Startup类代码
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot().AddConsul().AddPolly();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseOcelot();
    }
}
configuration.json 
{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{url}", //服务地址--url变量
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/consul/{url}", //网关地址--url变量
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "ServiceName": "lqwvje", //consul服务名称
      "LoadBalancerOptions": {
        "Type": "RoundRobin" //轮询      LeastConnection-最少连接数的服务器   NoLoadBalance不负载均
      },
      "UseServiceDiscovery": true,
      //"RateLimitOptions": {
      //  "ClientWhitelist": [], //白名单
      //  "EnableRateLimiting": true,
      //  "Period": "5m", //1s, 5m, 1h, 1d
      //  "PeriodTimespan": 5, //多少秒之后客户端可以重试
      //  "Limit": 5 //统计时间段内允许的最大请求数量
      //},

      //"FileCacheOptions": {
      //  "TtlSeconds": 10
      //}, //"缓存"

      //"QoSOptions": {
      //  "ExceptionsAllowedBeforeBreaking": 3, //允许多少个异常请求
      //  "DurationOfBreak": 10000, // 熔断的时间,单位为ms
      //  "TimeoutValue": 10000 //如果下游请求的处理时间超过多少则自如将请求设置为超时 默认90秒
      //}
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://127.0.0.1:5099", //网关对外地址
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul" //由Consul提供服务发现
    },
    "RateLimitOptions": {
      "QuotaExceededMessage": "Too many requests, maybe later?11", // 当请求过载被截断时返回的消息
      "HttpStatusCode": 666 // 当请求过载被截断时返回的http status
    }

  }
}

关于Consul内容请防问 http://www.luofenming.com/show.aspx?id=ART2019121800002

源码下载地址:https://pan.baidu.com/s/1uTIACL49mkWPdFLAy5g85Q 提取码:4rct

服务启动流程
启动 Consul服务
consul_1.6.2.exe agent -dev -http-port 8500 -ui -client 0.0.0.0
//查看Consul状态
http://localhost:8500 
//启动服务端  端口可以随意写  多启动几个
dotnet apiServerDemo.dll --urls="http://*:5004"  --ip="127.0.0.1" --port=5004
dotnet apiServerDemo.dll --urls="http://*:5005"  --ip="127.0.0.1" --port=5005
dotnet apiServerDemo.dll --urls="http://*:5006"  --ip="127.0.0.1" --port=5006

//启动网关
dotnet apiOcelot.dll --urls="http://*:5099"  --ip="127.0.0.1" --port=5099

//浏览器打开
http://localhost:5099/consul/Students

评论