[译]Ocelot - Configuration

原文html

这里有一个配置的样例。配置主要有两个部分。一个是ReRoutes数组,另外一个是GlobalConfiguration。ReRoute告诉Ocelot怎么处理上游的请求。Global configuration能让咱们覆盖一些ReRoute的一些配置。git

{
    "ReRoutes": [],
    "GlobalConfiguration": {}
}

这里是一个ReRoutes的配置样例(你不须要设置下面全部的配置):github

{
          "DownstreamPathTemplate": "/",
          "UpstreamPathTemplate": "/",
          "UpstreamHttpMethod": [
              "Get"
          ],
          "AddHeadersToRequest": {},
          "AddClaimsToRequest": {},
          "RouteClaimsRequirement": {},
          "AddQueriesToRequest": {},
          "RequestIdKey": "",
          "FileCacheOptions": {
              "TtlSeconds": 0,
              "Region": ""
          },
          "ReRouteIsCaseSensitive": false,
          "ServiceName": "",
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
              {
                  "Host": "localhost",
                  "Port": 51876,
              }
          ],
          "QoSOptions": {
              "ExceptionsAllowedBeforeBreaking": 0,
              "DurationOfBreak": 0,
              "TimeoutValue": 0
          },
          "LoadBalancer": "",
          "RateLimitOptions": {
              "ClientWhitelist": [],
              "EnableRateLimiting": false,
              "Period": "",
              "PeriodTimespan": 0,
              "Limit": 0
          },
          "AuthenticationOptions": {
              "AuthenticationProviderKey": "",
              "AllowedScopes": []
          },
          "HttpHandlerOptions": {
              "AllowAutoRedirect": true,
              "UseCookieContainer": true,
              "UseTracing": true
          },
          "UseServiceDiscovery": false,
          "DangerousAcceptAnyServerCertificateValidator": false
}

Multiple environments

Ocelot支持如configuration.dev.json, configuration.test.json等,这样的配置文件。json

.ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                .AddJsonFile("appsettings.json", true, true)
                .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                .AddJsonFile("ocelot.json")
                .AddJsonFile($"configuration.{hostingContext.HostingEnvironment.EnvironmentName}.json")
                .AddEnvironmentVariables();
        })

Merging configuration files

能够使用AddOcelot()来替换AddJsonFile("ocelot.json")数组

.ConfigureAppConfiguration((hostingContext, config) =>
    {
        config
            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", true, true)
            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
            .AddOcelot()
            .AddEnvironmentVariables();
    })

这种状况下Ocelot会查找匹配 (?i)ocelot.([a-zA-Z0-9]*).json的文件,而后合并他们。若是你想设置GlobalConfiguration属性,那么必需要有一个ocelot.global.json文件。缓存

Ocelot合并配置文件的方式,就是去加载它们,而后循环遍历这些文件,添加ReRoutes,AggregateReRoutes, and if the file is called ocelot.global.json add the GlobalConfiguration aswell as any ReRoutes or AggregateReRoutes。Ocelot会将合并的文件保存为ocelot.json,在ocelot运行的时候使用这个文件。服务器

在合并前不会作任何验证。若是出现了问题,建议检查ocelot.json文件。cookie

Store configuration in consul

若是在注册ocelot服务的时候添加了下面的代码,ocelot会将配置信息存储在consul的键值存储中,而且读取也会从consul的键值存储中读取。app

services
   .AddOcelot()
   .AddStoreOcelotConfigurationInConsul();

同时,你须要将下面的代码添加到ocelot.json中。这样Ocelot才能找到Consul和它进行交互,从Consul中存储&加载配置。ide

"GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
        "Host": "localhost",
        "Port": 9500
    }
}

Reload JSON config on change

Ocelot支持当配置文件发生修改后从新加载配置。下面代码使得当ocelot.json手动更新后从新加载配置文件。

config.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);

Configuration Key

若是你使用了Consul来读取&存储配置,你可能须要用一个key来标识配置文件,这样就能够有多个配置了。只须要设置ServiceDiscoveryProviderConfigurationKey就能为配置指定一个key了:

"GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
        "Host": "localhost",
        "Port": 9500,
        "ConfigurationKey": "Oceolot_A"
    }
}

这样Ocelot在经过Consul加载配置文件的时候会查找key为Oceolot_A的配置。

若是没有设置ConfigurationKey, ocelot会使用InternalConfiguration做为key。

Follow Redirects / Use CookieContainer

使用ReRoute中的HttpHandlerOptions来设置HttpHandler的行为:

  • AllowAutoRedirect用来标识请求是否应该redirection responses。设置true会自动从下游资源redirection response。默认为:false
  • UseCookieContainer用来标识是否使用CookieContainer存储服务器cookie,而且在发送请求的时候带上这些cookie。默认为:false。若是你使用了CookieContainer, Ocelot会为每一个下游服务缓存HttpClient。这意味全部对DownstreamService的请求都会共享一样的cookie。建议不要使用CookieContainer。

SSL Errors

若是你想忽略SSL警告|错误,你须要这样配置ReRoute:

"DangerousAcceptAnyServerCertificateValidator": false
相关文章
相关标签/搜索