Ocelot API 网关仅设置 TimeOut 值抛出错误

Posted

技术标签:

【中文标题】Ocelot API 网关仅设置 TimeOut 值抛出错误【英文标题】:Ocelot API gateway setting only TimeOut value throwing error 【发布时间】:2020-11-07 15:36:36 【问题描述】:

根据Ocelot文档Quality Of Service我们只能在Routes中提到TimeOutValue,比如

"QoSOptions": 
    "TimeoutValue":5000

但这会抛出异常

Ocelot.Requester.Middleware.HttpRequesterMiddleware: Debug: requestId: 800002b3-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: IHttpRequester returned an error, setting pipeline error
Ocelot.Responder.Middleware.ResponderMiddleware: Warning: requestId: 800002b3-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: Error Code: UnableToCompleteRequestError Message: Error making http request, exception: System.ArgumentException: The enumerable of policies to form the wrap must contain at least two policies. (Parameter 'policies')
   at Polly.Policy.WrapAsync(IAsyncPolicy[] policies)
   at Ocelot.Provider.Polly.PollyCircuitBreakingDelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Ocelot.Requester.HttpClientHttpRequester.GetResponse(HttpContext httpContext) errors found in ResponderMiddleware. Setting error response for request path:/api/v1/cust/clients, request method: GET
Ocelot.Errors.Middleware.ExceptionHandlerMiddleware: Debug: requestId: 800002b3-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: ocelot pipeline finished
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 200.2779ms 500 

我的路线配置是


  "Routes": [
    
      "DownstreamPathTemplate": "/api/version/cust/everything",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        
          "Host": "localhost",
          "Port": 5001
        
      ],
      "UpstreamPathTemplate": "/api/version/cust/everything",
      "UpstreamHttpMethod": [],
      "QoSOptions": 
        "TimeoutValue": 900000
      
    
  ],
  "GlobalConfiguration": 
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration"
  


我的 .Net Core 版本是 3.1,Ocelet 版本是 16.0.1

【问题讨论】:

看起来这可能是 Polly 问题:github.com/App-vNext/Polly/issues/386 【参考方案1】:

只有一个策略(超时策略)可能会出现一些问题。

我必须添加断路器设置才能使这项工作:

 "QoSOptions": 
    "ExceptionsAllowedBeforeBreaking":99999,
    "DurationOfBreak":0,
    "TimeoutValue":5000
  

【讨论】:

以上是关于Ocelot API 网关仅设置 TimeOut 值抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

Ocelot实现API网关服务

Ocelot一款.NET下的API网关介绍

使用 API 网关 Ocelot 与 Nginx 的微服务

AKS 中的 Ocelot API 网关实现

Ocelot Api网关教程- QoS

API网关-Ocelot概述