1. 程式人生 > >Ocelot中文文件-請求Id和關聯Id

Ocelot中文文件-請求Id和關聯Id

原文: Ocelot中文文件-請求Id和關聯Id

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,