[译]Ocelot - Configuration
这里有一个配置的样例。配置主要有两个部分。一个是ReRoutes数组,另一个是GlobalConfiguration。ReRoute告诉Ocelot怎么处理上游的请求。Global configuration能让我们覆盖一些ReRoute的一些配置。
{
"ReRoutes": [],
"GlobalConfiguration": {}
}
这里是一个ReRoutes的配置样例(你不需要设置下面所有的配置):
{
"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等,这样的配置文件。
.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文件。
Store configuration in consul
如果在注册ocelot服务的时候添加了下面的代码,ocelot会将配置信息存储在consul的键值存储中,并且读取也会从consul的键值存储中读取。
services
.AddOcelot()
.AddStoreOcelotConfigurationInConsul();
同时,你需要将下面的代码添加到ocelot.json中。这样Ocelot才能找到Consul和它进行交互,从Consul中存储&加载配置。
"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来标识配置文件,这样就可以有多个配置了。只需要设置ServiceDiscoveryProvider的ConfigurationKey就能为配置指定一个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
[译]Ocelot - Configuration的更多相关文章
- [译]Ocelot - Getting Started
原文 Ocelot专为.NET Core而设计. .NET Core 2.1 安装 首先需要创建一个netstandard2.0项目,然后再通过nuget安装. Install-Package Oce ...
- [译]Ocelot - Big Picture
原文 目录 Big Picture Getting Started Configuration Routing Request Aggregation Service Discovery Authen ...
- [译]Ocelot - Load Balancer
原文 可以对下游的服务进行负载均衡. 提供了下面几种负载均衡: LeastConnection - tracks which services are dealing with requests an ...
- [译]Ocelot - Quality of Service
原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...
- [译]Ocelot - Caching
原文 Ocelot支持基本的缓存,目前Ocelot的缓存是通过CacheManager project实现的. 下面的示例展示了如何启用缓存: s.AddOcelot() .AddCacheManag ...
- [译]Ocelot - Rate Limiting
原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...
- [译]Ocelot - Service Discovery
原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...
- [译]Ocelot - Request Aggregation
原文 Aggregate ReRoutes用来组合多个ReRoutes,将它们的响应结果映射到一个响应中返回给客户端. 为了使用Aggregate ReRoutes,你必须像下面的ocelot.jso ...
- [译]Ocelot - Routing
原文 Ocelot主要的功能就是将http请求转发到对应的下游服务上去. Ocelot将一个请求路由到另外一个路由的动作叫做ReRoute.为了能让Ocelot能正常工作,需要在配置中设置ReRout ...
随机推荐
- 【English 】20190319
BOKO鼻子['boʊkoʊ] pores毛孔['pɔ:z] cute漂亮可爱[kjut] DEKO-BOKO pores don't make a girl cute! ideal最理想的[aɪˈ ...
- Vue项目分环境打包的实现步骤
转:https://blog.csdn.net/xinzi11243094/article/details/80521878 方法一:亲测真的有效 在项目开发中,我们的项目一般分为开发版.测试版.Pr ...
- wangEditor的使用
wangEditor的使用 第一步,将其下载,并引入项目中. 第二步,引入js <script type="text/javascript" src="/plugi ...
- 强大的scrollReveal库,炫酷的页面缓入效果。
首先我问来看一下这个强大的插件能做出什么效果,下面是我找的一个网站: http://kepler.gl/#/, 接下来看看官网给出的效果:https://scrollrevealjs.org/. 是不 ...
- Gdi绘图
在使用VC开发项目过程中,界面是项目中的一个子模块.虽然界面并不那么重要,把握住核心功能就可以了,但界面美观与否直接关系到用户的体验, 因此我们也应该关注界面的处理. 我们可以在OnEraseBkgn ...
- Python—kmeans算法学习笔记
一. 什么是聚类 聚类简单的说就是要把一个文档集合根据文档的相似性把文档分成若干类,但是究竟分成多少类,这个要取决于文档集合里文档自身的性质.下面这个图就是一个简单的例子,我们可以把不同的文档聚合 ...
- Elastic Stack-Elasticsearch使用介绍(一)
一.前言 Elasticsearch对外提供RESTful API,下面的演示我们主要使用Postman,进行一系列的Demo演示,这款工具方便各位前端大大或者对接口调试的神器: 安装过于简单 ...
- 进程与线程的通信机制----Queue
进程运行时候变量是隔离的,线程间共享全局变量. 进程: from multiprocessing import Process from threading import Thread def get ...
- C语言之四舍五入
在C语言中,如果进行强制类型转换,它会将所需要取的位数直接提取出来,而其他位数的数字会被直接删除,不会对提取出来的位数有任何影响 所以如果我们需要提高精度,对所取的数进行四舍五入,需要给所需去的数的最 ...
- vue通过自定义指令 v-py 将名字转拼音
自定义指令 py: 1.新建 vue-py.js文件 import Vue from 'vue'; var chinesePointCode = { "a": [21834, 38 ...