我正在使用 Play 的 AhcWSClient
发出 HTTP 请求(我没有使用 Play 框架的其余部分):
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val wsClient = AhcWSClient()
wsClient
.url(s"http://somerandomurl.com/somethingsomething")
.get()
.map { wsResponse =>
if (!(200 to 299).contains(wsResponse.status)) {
sys.error(s"Received unexpected status ${wsResponse.status} : ${wsResponse.body}")
}
println(s"OK, received ${wsResponse.body}")
}(system.dispatcher)
}
如果失败,我如何指示 WSClient 重试请求?
查看文档,它 seems to be possible使用 AsyncHttpClientConfig
使用 maxNumberOfRedirects
参数(指的是“请求失败时重试请求的最大次数”),但是怎么做呢?
更新:显然,在创建 AhcWSClient
时,我们得到了 five retries by default , 并且可以用不同的 maxRequestRetry
指定我们自己的 AhcWSClientConfig
。但是当我发出返回 502 状态代码的请求时,我没有看到多次尝试发生......我应该将什么算作“失败的请求”?
最佳答案
通常客户端应该只重试 idempotent 的方法:
The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE SHOULD NOT have side effects, and so are inherently idempotent.
关于scala - 重试与 Play 的 AhcWSClient 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37011458/