Ocelot支持一个客户端以头的形式发送requestid。 如果设置了,一旦中间件管道中可用,Ocelot便会使用这个requestid进行日志记录。 Ocelot也会使用指定头将requireid转发给下游服务。

如果在日志配置中你设置IncludeScopes为true,你还可以在日志中获取asp.net core的请求id。

为了是用requestid,有两种选择。

全局

在ocelot.json的GlobalConfiguration配置块中如下设置。这样所有进入Ocelot的请求都会起作用。

 "GlobalConfiguration": {
  "RequestIdKey": "OcRequestId"
}

我建议使用GlobalConfiguration,除非你真的需要它是指定ReRoute的。

ReRoute

如果你想覆盖全局设置,在ocelot.json的特定ReRoute中添加如下设置。

"RequestIdKey": "OcRequestId"

一旦Ocelot识别出与ReRoute对象匹配的请求,它将根据ReRoute的配置来设置requestid。

这可能会导致一下困惑。如果你在GlobalConfiguration中设置了requestid,可能在ReRoute被匹配前是一个,匹配后是另一个,因为requestid的key会变。这是因为设计如此,而且这是我目前能想到的最好的解决方案了。在这种情况下OcelotLogger会在日志中记录当前requestid和上一个requestid。

下面的例子是debug级别下一个正常请求的日志记录。

dbug: Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0]
      requestId: asdf, previousRequestId: no previous request id, message: ocelot pipeline started,
dbug: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0]
      requestId: asdf, previousRequestId: no previous request id, message: upstream url path is {upstreamUrlPath},
dbug: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0]
      requestId: asdf, previousRequestId: no previous request id, message: downstream template is {downstreamRoute.Data.ReRoute.DownstreamPath},
dbug: Ocelot.RateLimit.Middleware.ClientRateLimitMiddleware[0]
      requestId: asdf, previousRequestId: no previous request id, message: EndpointRateLimiting is not enabled for Ocelot.Values.PathTemplate,
dbug: Ocelot.Authorisation.Middleware.AuthorisationMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: /posts/{postId} route does not require user to be authorised,
dbug: Ocelot.DownstreamUrlCreator.Middleware.DownstreamUrlCreatorMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: downstream url is {downstreamUrl.Data.Value},
dbug: Ocelot.Request.Middleware.HttpRequestBuilderMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: setting upstream request,
dbug: Ocelot.Requester.Middleware.HttpRequesterMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: setting http response message,
dbug: Ocelot.Responder.Middleware.ResponderMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: no pipeline errors, setting and returning completed response,
dbug: Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0]
      requestId: 1234, previousRequestId: asdf, message: ocelot pipeline finished,

Ocelot中文文档-请求Id和关联Id的更多相关文章

  1. Ocelot中文文档-请求聚合

    Ocelot允许您指定聚合多个普通ReRoutes的Aggregate ReRoutes(聚合路由),并将其响应映射到一个对象中.一般用于当您有一个客户端向服务器发出多个请求,而这些请求可以合并成一个 ...

  2. Ocelot中文文档-Route

    路由(Routing) Ocelot主要功能是接收即将发来的请求并转发它们至下游服务.与此同时,以另一个http请求的形式(在将来这可能是任何传输的机制) Ocelot将一个请求的路由描述为另一个路由 ...

  3. Ocelot中文文档入门

    入门 Ocelot仅适用于.NET Core,目前是根据netstandard2.0构建的,如果Ocelot适合您,这个文档可能会有用. .NET Core 2.1 安装NuGet包 使用nuget安 ...

  4. Ocelot中文文档-流量控制

    感谢@catcherwong 的文章激励我最终写出了这个文档 Ocelot支持上游的请求限制,以便您的下游服务不会过载. 此功能是由GitHub上的@geffzhang添加! 非常感谢. 好了,为了让 ...

  5. Ocelot中文文档-转换Headers

    Ocelot允许在请求下游服务之前和之后转换头部.目前Ocelot只支持查找和替换.这个功能在Github #190提出.我确定这个功能可以在各个方面发挥作用. 添加到请求 这个功能在GitHub # ...

  6. .NET Core开源API网关 – Ocelot中文文档

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...

  7. Ocelot中文文档-路由

    Ocelot的主要功能是接管进入的http请求并把它们转发给下游服务.目前是以另一个http请求的形式(将来可能是任何传输机制). Ocelot将路由一个请求到另一个请求描述为ReRoute.为了在O ...

  8. Ocelot中文文档-管理

    Ocelot支持在运行时通过一个认证的Http API修改配置.有两种方式对其验证, 使用Ocelot的内置IdentityServer(仅用于向管理API验证请求)或将管理API验证挂接到您自己的I ...

  9. Ocelot中文文档-跟踪

    Ocelot使用一个杰出的项目Butterfly 提供了跟踪功能. 为了使用跟踪,请阅读Butterfly的文档. 在Ocelot中如果你想跟踪一个ReRoute,你需要做如下事情: 在Configu ...

随机推荐

  1. 用SpriteBuilder简化"耕牛遍地走"的动画效果(二)

    首先使用SpriteBuilder新建一个项目,将之前下载的资源文件夹拖入SpriteBuilder的文件视图. 这里我们只需要一步操作就可以完成原文中在Texture Packer中的那么多操作:即 ...

  2. Linux多线程实践(7) --多线程排序对比

    屏障 int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restri ...

  3. java文件的基本操作示例

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println("请输入您的 ...

  4. json进阶(一)js读取解析JSON类型数据

    js读取解析JSON类型数据 一.什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式,同 ...

  5. PR 审批界面增加显示项方法

    PR 审批界面增加显示项 解决方法 Step 1:       进入审批界面: Step 2:       在上图中,点击左下角'About this Page'查看数据源 点击上图中'Expand ...

  6. 广义线性模型 R--glm函数

    R语言glm函数学习:  [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. glm函数介绍: glm(for ...

  7. Java-ServletOutputStream

    /** * Provides an output stream for sending binary data to the * client. A <code>ServletOutput ...

  8. 能量最小化初探,graphcuts能量最小化调用

    1.相对于能量函数来说,能量最小化的办法都有哪些? 梯度下降 模拟退火 图割 2.这个 跟最优化问题的求解,有什么联系跟区别呢? 基本上差不多,其实就是求出来了函数的一个最小值,我们看问题的时候不妨把 ...

  9. myBatis源码学习之SqlSession

    在上一篇文章中SqlSessionFactory介绍了生产SqlSession的工厂,SqlSession是一个接口其具体实现类为DefaultSqlSession,SqlSession接口主要定义了 ...

  10. Treemap 有序的hashmap。用于排序

    TreeMap:有固定顺序的hashmap.在需要排序的Map时候才用TreeMap. Map.在数组中我们是通过数组下标来对其内容索引的,键值对. HashMap HashMap 用哈希码快速定位一 ...