1.有个 spring retry 的重试,但是这个似乎没有办法实现超时之后重试 2.我们使用的是 org.springframework.web.reactive.function.client.WebClient ,我知道有个 retryWhen 的东西,但是由于封装了 sdk 所以我不好改它 所以大佬们有什么其它好的实现呢!!!!!
1
EddieWang OP 也有使用 Mono.defer 似乎好像也不太行?
|
2
dqzcwxb 2023-03-29 19:26:22 +08:00
completablefuture timedGet()可以设置超时时间,超时后会抛出 TimeoutException 异常你捕获该异常进行重试就可以了
|
3
kwh 2023-03-29 20:17:58 +08:00
resilience4j ???
|
4
hn16838220 2023-03-29 21:26:46 +08:00
我记得 guava 的 retry 是可以写条件判断是否重试的
|
5
why1001 2023-03-29 22:41:36 +08:00
可以看看 project reactor 文档,webflux 也是在这上边实现的,里边有个 timeout()还有其它的可以实现。
|
6
Helsing 2023-03-29 22:45:33 +08:00 via iPhone
用 reactor 、rxjava 这些框架比较好实现
|
7
shinyruo2020 2023-03-29 22:51:56 +08:00
for 循环 + countDownLatch.await 指定超时时间
|
8
bthulu 2023-03-30 10:32:31 +08:00
You can retry requests using the Java 11 HTTP client by composing the returned future with re-schedules in a loop. Here’s an example:
public static <T> CompletableFuture<T> retry(Supplier<CompletableFuture<T>> action, int maxRetries) { return action.get().handle((result, throwable) -> { if (throwable != null && maxRetries > 0) { return retry(action, maxRetries - 1); } else if (throwable != null) { throw new RuntimeException(throwable); } else { return result; } }).thenCompose(Function.identity()); } This method takes a Supplier that returns a CompletableFuture, and an integer maxRetries that specifies the maximum number of retries. It returns a new CompletableFuture that retries the action if it fails with an exception. Here’s how you can use this method to retry an HTTP request: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://example.com")) .build(); CompletableFuture<HttpResponse<String>> response = retry(() -> client.sendAsync(request, HttpResponse.BodyHandlers.ofString()), 3); This code retries the HTTP request up to three times if it fails with an exception. I hope this helps! Let me know if you have any other questions. |
9
yazinnnn 2023-03-30 10:33:34 +08:00
Mono.defer(() -> {
System.out.println(LocalDateTime.now()); val integer = Random.Default.nextInt(100); System.out.println(integer); if (integer > 80) { return Mono.just(100); } else { return Mono.never(); } }) .timeout(Duration.ofSeconds(1)) .retry(10) defer 就行 |
10
liuhuan475 2023-03-30 10:41:58 +08:00
楼上的回答都有同样的问题,就是应用挂掉重试就不会进行了
|