Ocelot允许用户访问claims
并把它们转换到头部,请求字符串参数和其他claims
中.这仅在用户通过身份验证后才可用。
用户通过身份验证之后,我们运行claims
转换中间件.这个中间件允许在授权中间件调用之前转换claims
.当用户身份验证之后,首先会调用claims
转换到头的中间件,然后调用claims
转换到查询字符串的中间件,最后调用claims
转换到downstream
路径中间件.
执行转换的语法对于每个处理都是相同的。在Route
配置中,使用特定名称AddClaimsToRequest
,AddHeadersToRequest
,AddQueriesToRequest
添加一个json字典。
注意,我不是一个编程专家,所以不知道这个语法是否好…
在词典中,这些条目指定了Ocelot应该如何转换! 字典的键将成为claim
,header
,query parameter
的键。对于ChangeDownstreamPathTemplate
,还必须在DownstreamPathTemplate
中指定key
,以便进行转换。
条目的值将被解析成转换的逻辑.首先指定了一个字典访问器,例如Claims[CustomerId]
.意思是我们想访问claims
并获取键为CustomerId
的claim
类型.然后一个大于号(>
)用于分隔.下一个条目是值或带索引器的值.如果指定了单个值,Ocelot将取该值并将其添加到变换中。如果该值有一个索引器,Ocelot将查找在另一个大于符号后面提供的分隔符。 然后,Ocelot会使用分隔符将值分开,并将所需的索引添加到转换中。
Claims
到 Claims
转换
下面是一个Claims到Claims转换的例子
"AddClaimsToRequest": {
"UserType": "Claims[sub] > value[0] > |",
"UserId": "Claims[sub] > value[1] > |"
}
这显示了Ocelot查看用户的sub
声明并将其转换为UserType
和UserId
声明的转换。 假设sub
声明看起来像这样“usertypevalue | useridvalue
”。
Claims到header
的转换
下面是一个Claims
到头转换的例子
"AddHeadersToRequest": {
"CustomerId": "Claims[sub] > value[1] > |"
}
这显示了Ocelot查看用户的sub
声明并将其转换为CustomerId
头的转换。 假设sub
声明看起来像这样“usertypevalue | useridvalue
”。
Claims 到查询字符串参数的转换
下面是一个Claims到查询字符串参数转换的例子
"AddQueriesToRequest": {
"LocationId": "Claims[LocationId] > value",
}
这显示了Ocelot查看用户的LocationId
声明并将其作为发往下游服务的查询字符串参数LocationId
的转换。
Claims 到Downstream
路径转换
以下是将claims
转换为下游路径自定义占位符的示例配置
"UpstreamPathTemplate": "/api/users/me/{everything}",
"DownstreamPathTemplate": "/api/users/{userId}/{everything}",
"ChangeDownstreamPathTemplate": {
"userId": "Claims[sub] > value[1] > |",
}
这显示了一个转换,其中Ocelot
着眼于用户userId
声明,并将该值替换为DownstreamPathTemplate
中指定的“ {userId}
”占位符。 考虑到ChangeDownstreamPathTemplate
中指定的密钥必须与DownstreamPathTemplate
中指定的占位符相同。
注意:如果ChangeDownstreamPathTemplate
中指定的键在DownstreamPathTemplate
中不作为占位符存在,则它将在运行时失败,并在响应中返回错误。
日志
目前,Ocelot使用标准的日志记录接口ILoggerFactory / ILogger<T>
。 在IOcelotLogger / IOcelotLoggerFactory
中提供了标准的asp.net core日志记录的一个实现。 因为Ocelot在日志中添加了一些额外的信息,如请求ID(如果已配置的话)。
这有个全局的错误处理程序,可以捕获所有异常并作为错误记录他们。
最后,如果日志记录设置为跟踪级别,Ocelot将记录开始,结束和任何抛出异常的中间件,这些异常可能非常有用。
不是使用标准框架的日志记录的原因是,我无法覆盖将IncludeScopes
设置为true
时记录的请求标识。
警告
如果您记录日志到控制台,您将获得糟糕的性能。 我遇到过很多关于Ocelot性能的问题,它始终记录调试级别的日志,并记录到控制台 😃 所以请确保您生产中记录了正确的东西 : )
跟踪
本页详细介绍如何使用Ocelot执行分布式跟踪。
OpenTracing
Ocelot提供程序从出色的OpenTracing C#项目中跟踪功能。 Ocelot集成的代码可以在这里找到。
下面的示例使用Jaeger C#客户端提供Ocelot中使用的跟踪程序。
services.AddSingleton<ITracer>(sp =>
{
var loggerFactory = sp.GetService<ILoggerFactory>();
Configuration config = new Configuration(context.HostingEnvironment.ApplicationName, loggerFactory);
var tracer = config.GetTracer();
GlobalTracer.Register(tracer);
return tracer;
});
services
.AddOcelot()
.AddOpenTracing();
然后在您的ocelot.json
中将以下内容添加到要跟踪的路由中。
"HttpHandlerOptions": {
"UseTracing": true
},
现在,当调用此路由时,Ocelot将向Jaeger发送跟踪信息。
Butterfly
Ocelot
提供商从出色的Butterfly
项目中跟踪功能。 Ocelot
集成的代码可以在这里找到。
为了使用跟踪,请阅读Butterfly
文档。
如果要跟踪路线,则在ocelot
中需要执行以下操作。
Install-Package Ocelot.Tracing.Butterfly
在您的ConfigureServices
方法中
services
.AddOcelot()
// this comes from Ocelot.Tracing.Butterfly package
.AddButterfly(option =>
{
//this is the url that the butterfly collector server is running on...
option.CollectorUrl = "http://localhost:9618";
option.Service = "Ocelot";
});
然后在ocelot.json
文件中,添加如下配置到你想要跟随的Route
中。
"HttpHandlerOptions": {
"UseTracing": true
},
现在,当这个Route
被调用的时候,Ocelot会发送跟踪信息到Butterfly
。