spring-boot - 如何将 CircuitBreaker 与 TimeLimiter 和 Bulkhead 结合使用?
问题描述
我有一个通过 REST 调用依赖项的服务。服务和依赖是微服务架构的一部分,所以我想使用弹性模式。我的目标是:
- 当依赖关系陷入困境时,有一个断路器来保护它
- 限制呼叫可以运行的时间。该服务具有 SLA,并且必须在特定时间内回答。在超时时,我们使用回退值。
- 限制对依赖项的并发调用数。通常调用率很低,响应速度很快,但我们希望保护依赖关系免受服务内部的突发和排队请求。
以下是我当前的代码。它可以工作,但理想情况下我想使用TimeLimiter
andBulkhead
类,因为它们似乎是为了一起工作而构建的。
我怎样才能写得更好?
@Component
class FooService(@Autowired val circuitBreakerRegistry: CircuitBreakerRegistry)
{
...
// State machine to take load off the dependency when slow or unresponsive
private val circuitBreaker = circuitBreakerRegistry
.circuitBreaker("fooService")
// Limit parallel requests to dependency
private var semaphore = Semaphore(maxParallelRequests)
// The protected function
private suspend fun makeHttpCall(customerId: String): Boolean {
val client = webClientProvider.getCachedWebClient(baseUrl)
val response = client
.head()
.uri("/the/request/url")
.awaitExchange()
return when (val status = response.rawStatusCode()) {
200 -> true
204 -> false
else -> throw Exception(
"Foo service responded with invalid status code: $status"
)
}
}
// Main function
suspend fun isFoo(someId: String): Boolean {
try {
return circuitBreaker.executeSuspendFunction {
semaphore.withPermit {
try {
withTimeout(timeoutMs) {
makeHttpCall(someId)
}
} catch (e: TimeoutCancellationException) {
// This exception has to be converted because
// the circuit-breaker ignores CancellationException
throw Exception("Call to foo service timed out")
}
}
}
} catch (e: CallNotPermittedException) {
logger.error { "Call to foo blocked by circuit breaker" }
} catch (e: Exception) {
logger.error { "Exception while calling foo service: ${e.message}" }
}
// Fallback
return true
}
}
理想情况下,我想写一些类似于Flows的文档描述的东西:
// Main function
suspend fun isFoo(someId: String): Boolean {
return monoOf(makeHttpCall(someId))
.bulkhead(bulkhead)
.timeLimiter(timeLimiter)
.circuitBreaker(circuitBreaker)
}
解决方案
您也可以使用 Resilience4j 的 Bulkhead 代替您自己的 Semaphore 和 Resilience4j 的 TimeLimiter。您可以将 CircuitBreaker 与bulkhead.executeSuspendFunction
和叠加timelimiter.executeSuspendFunction
。
推荐阅读
- c# - “this”这个词是什么意思,“static”是什么意思?
- php - 允许上传原始文件名
- c# - 在 int List 中插入值然后对列表进行排序的最快方法
- python-3.x - Watson-NLU 在循环中给出下游问题(500),但不是单独在句子上
- javascript - 检测可编辑 div 中动态创建的跨度上的退格键
- android - 如何更改后台运行的应用程序图标?
- javascript - Javascript从url打开txt并输出为HTML
- android - 使用 google smart lock 进行 oauth 登录方法
- c# - 处理 Access 数据库时处理缓慢
- reactjs - 在 Facebook 评论中反应使用