【发布时间】:2021-09-05 14:57:48
【问题描述】:
我最近添加了 spring @Retryable 来处理 502 错误,而不是来自目标服务器的适当响应。 (带改造2)。
以下只是伪代码,但原始代码以类似的方式处理异常。
class BadGatewayException : RuntimeException
@Retryable(include = [BadGatewayException::class])
class A {
private fun handle(block: () -> Call<T>): T {
try {
val response = val response = block().execute()
...
if (response.code() == 502) {
throw BadGatewayException("The server suffers temporary connection problems.")
}
...
} catch(e: Exception) {
throw RuntimeException("a system error has occurred")
}
}
}
我预计@Retryable 不会重试,因为与 502 一起出现的BadGatewayException 将立即被包裹在RuntimeException 中,然后被抛出。但是,当它被测试时,它似乎遵循了这一步
- 尝试从改造请求中获得响应
- 502 发生
-
BadGatewayException抛出 - 重试(默认为 3) - 这里 BadGatewayException 以某种方式被捕获
-
RuntimeException抛出
关键是,@Retryable 应该以这种方式拦截任何异常吗?还是我在这里遗漏了什么?
【问题讨论】:
标签: spring-boot retrofit2 spring-retry