从我们的服务调用几个下游,我们需要提供断路器,,期限和舱壁特性到所有下游服务调用。
当前代码库:
@Override
@Bulkhead(name = "downstream1", type = Type.THREADPOOL, fallbackMethod = "downstream1Fallback")
@TimeLimiter(name = "downstream1", fallbackMethod = "downstream1Fallback")
@CircuitBreaker(name = "downstream1", fallbackMethod = "downstream1Fallback")
@Retry(name = "downstream1", fallbackMethod = "downstream1Fallback")
public CompletableFuture<String> downstream1(String a) {
return CompletableFuture.completedFuture(process(a)));
}
public CompletableFuture<String> downstream1Fallback(String a,
Exception e) {
return CompletableFuture.completedFuture(""); // empty string.
}
app.properties:
# timeouts
resilience4j.timelimiter.instances. downstream1.timeout-duration=30 #30 ms
# retry
resilience4j.retry.instances.downstream1.max-attempts=1
# bulkhead
resilience4j.thread-pool-bulkhead.instances.downstream1.core-thread-pool-size=30
resilience4j.thread-pool-bulkhead.instances.downstream1.max-thread-pool-size=50
resilience4j.thread-pool-bulkhead.instances.downstream1.queue-capacity=10
用例>:如果Bulkhead的线程池已满,或者下游超时,或者circuit-is-open,我不想在所有这些场景中重试,并在其他场景中重试。
重试应该只发生在服务器异常从下游。
根据官方文档:https://resilience4j.readme.io/docs/getting-started-3#aspect-order。这是顺序。
Retry (CircuitBreaker (RateLimiter (TimeLimiter (Bulkhead ())函数
我对docs的理解:
case 1: If thread-pool is full, fallback will be called with
exception type BulkheadFullException.class - no retry
: else new thread will be spawned to call downstream.
case 3: If spawned thread takes more time than what's set on
"resilience4j.timelimiter", fallback will be called with
exception type TimeoutException.class - no retry
case 4: If circuit is open, fallback will be called with
exception type CallNotPermittedException.class - no retry.
case 5: If thread-pool is not full and no timeout and circuit is
closed as well, and we got some other exception than
the above mentioned exceptions, than only retry to downstream
call will trigger using config values set on "resilience4j.retry".
这个关于弹性4j如何工作的流理解正确吗或者我需要在app.properties中更改/添加任何配置文件,以满足上面描述的用例。
您可以定义您想要使用retryExceptions
重试的异常,也可以定义您希望使用ignoreExceptions
忽略的异常。如果这两种解决方案都不适合您,那么您可以有一个谓词retryOnResultPredicate
,它评估是否应该重试结果。如果需要重试结果,Predicate必须返回true,否则必须返回false。
像下面。
import org.springframework.web.client.HttpClientErrorException;
import java.util.function.Predicate;
public class IgnoreErrorExceptionPredicate implements Predicate<Throwable> {
@Override
public boolean test(Throwable e) {
return e instanceof HttpClientErrorException || e.getCause() instanceof HttpClientErrorException;
}
}
并将重试属性中的谓词配置为
resilience4j.retry:
configs:
default:
maxAttempts: 3
waitDuration: 100
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.util.concurrent.TimeoutException
- java.io.IOException
ignoreExceptions:
- io.github.robwin.exception.BusinessException
retryOnResultPredicate:
- your.predicate.package.IgnoreErrorExceptionPredicate