Ocelot 配置初始

{
  "ReRoutes": [
    {
      /*将用户的请求 /post/1 转发到 localhost/api/post/1*/
      /*
      DownstreamPathTemplate:转到的地址
      DownstreamScheme:转到的请求协议
      DownstreamHostAndPorts:转到的端口地址及端口信息
      UpstreamPathTemplate:监听路由地址
      UpstreamHttpMethod:监听路由请求类型 可用数组
      Priority:路由的优先级Prority是大的会被优先选择
      万能模版转发:
                  {
                      "DownstreamPathTemplate": "/{url}",
                      "DownstreamScheme": "https",
                      "DownstreamHostAndPorts": [
                              {
                                  "Host": "localhost",
                                  "Port": 80,
                              }
                          ],
                      "UpstreamPathTemplate": "/{url}",
                      "UpstreamHttpMethod": [ "Get" ]
                  }
      */
      "DownstreamPathTemplate": "/api/post/{postId}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/post/{postId}",
      "UpstreamHttpMethod": [ "Get" ],
      "Priority": 1,



      /*设置HttpHeaders*/
      "AddHeadersToRequest": {},
      "AddClaimsToRequest": {},
      /*
      鉴权
      我们通过认证中的AllowedScopes 拿到claims之后,如果要进行权限的鉴别需要添加以下配置
      "RouteClaimsRequirement": {
         "UserType": "registered"
      }
      */
      "RouteClaimsRequirement": {},
      "AddQueriesToRequest": {},
      "RequestIdKey": "",
      /*
      缓存
      Ocelot可以对下游请求结果进行缓存 ,目前缓存的功能还不是很强大。它主要是依赖于CacheManager 来实现的,我们只需要在路由下添加以下配置即可
      Region是对缓存进行的一个分区,我们可以调用Ocelot的 administration API来移除某个区下面的缓存 
      */
      "FileCacheOptions": {
        "TtlSeconds": 0,
        "Region": "somename"
      },
      "ReRouteIsCaseSensitive": false,
      "ServiceName": "",

      /*
      服务质量与熔断:熔断的意思是停止将请求转发到下游服务。当下游服务已经出现故障的时候再请求也是功而返,并且增加下游服务器和API网关的负担。这个功能是用的Pollly来实现的,我们只需要为路由做一些简单配置即可
      ExceptionsAllowedBeforeBreaking 允许多少个异常请求
      DurationOfBreak 熔断的时间,单位为秒
      TimeoutValue 如果下游请求的处理时间超过多少则自如将请求设置为超时
      */
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 0,
        "DurationOfBreak": 0,
        "TimeoutValue": 0
      },
      /*
      LeastConnection – 将请求发往最空闲的那个服务器
      RoundRobin – 轮流发送
      NoLoadBalance – 总是发往第一个请求或者是服务发现
      */
      "LoadBalancer": "", //将决定负载均衡的算法
      /*
      限流
      对请求进行限流可以防止下游服务器因为访问过载而崩溃,
      非常优雅的实现,我们只需要在路由下加一些简单的配置即可以完成
      ClientWihteList 白名单
      EnableRateLimiting 是否启用限流
      Period 统计时间段:1s, 5m, 1h, 1d
      PeroidTimeSpan 多少秒之后客户端可以重试
      Limit 在统计时间段内允许的最大请求数量
      */
      "RateLimitOptions": {
        "ClientWhitelist": [],
        "EnableRateLimiting": false,
        "Period": "",
        "PeriodTimespan": 0,
        "Limit": 0
      },
      /*
      认证
      如果我们需要对下游API进行认证以及鉴权服务的,则首先Ocelot 网关这里需要添加认证服务。这和我们给一个单独的API或者ASP.NET Core Mvc添加认证服务没有什么区别。
      JWT  Token
      public void ConfigureServices(IServiceCollection services)
      {
    var authenticationProviderKey = "ProviderKey";

    services.AddAuthentication()
        .AddJwtBearer(authenticationProviderKey, x =>
        {
        });
}
        然后在ReRoutes的路由模板中的AuthenticationOptions进行配置,只需要我们的AuthenticationProviderKey一致即可。
        "AuthenticationOptions": {
            "AuthenticationProviderKey": "ProviderKey",
            "AllowedScopes": []
        }
    }]

   

添加Identity Server的认证也是一样

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";
    var options = o =>
        {
            o.Authority = "identityserver4的地址";
            o.ApiName = "api"; 
            o.SupportedTokens = SupportedTokens.Both;
            o.ApiSecret = "secret";
        };

    services.AddAuthentication()
        .AddIdentityServerAuthentication(authenticationProviderKey, options);

    services.AddOcelot();
}
      */
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "",
        "AllowedScopes": []
      },
      /*


      */
      "HttpHandlerOptions": {
        "AllowAutoRedirect": true,
        "UseCookieContainer": true,
        "UseTracing": true
      },
      "UseServiceDiscovery": false,
      "Key": "keys1"
    }
  ],
  "Aggregates": [
    {
      "ReRouteKeys": [
        "keys1",
        "keys2"
      ],
      "UpstreamPathTemplate": "/"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5002"

  }
}

猜你喜欢

转载自www.cnblogs.com/liyouming/p/8856722.html
今日推荐