gpt4 book ai didi

spring - Feign 客户端和 Spring 重试

转载 作者:行者123 更新时间:2023-12-04 10:23:02 28 4
gpt4 key购买 nike

我有一个使用 Spring Cloud Feign 客户端调用外部服务的 Restful 服务

@FeignClient(name = "external-service", configuration = FeignClientConfig.class)
public interface ServiceClient {

@RequestMapping(value = "/test/payments", method = RequestMethod.POST)
public void addPayment(@Valid @RequestBody AddPaymentRequest addPaymentRequest);

@RequestMapping(value = "/test/payments/{paymentId}", method = RequestMethod.PUT)
public ChangePaymentStatusResponse updatePaymentStatus(@PathVariable("paymentId") String paymentId,
@Valid @RequestBody PaymentStatusUpdateRequest paymentStatusUpdateRequest);

}

在过去 3 个月中,我在日志文件中注意到以下失败 3-4 次:

json.ERROR_RESPONSE_BODY:Connection refused executing POST http://external-service/external/payments json.message:Send Payment Add Payment Failure For other reason: {ERROR_RESPONSE_BODY=Connection refused executing POST http://external-service/external/payments, EVENT=ADD_PAYMENT_FAILURE, TRANSACTION_ID=XXXXXXX} {} json.EVENT:ADD_PAYMENT_FAILURE json.stack_trace:feign.RetryableException: Connection refused executing POST http://external-service/external/payments at feign.FeignException.errorExecuting(FeignException.java:67) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)



是否可以在 Feign 客户端上添加 Spring Retry。
我想注释的 addPayment操作
@Retryable(value = {feign.RetryableException.class }, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier=2))

但这是不可能的,我还有什么其他选择?

最佳答案

您可以添加 RetryerFeignClientConfig

@Configuration
public class FeignClientConfig {

@Bean
public Retryer retryer() {
return new Custom();
}

}

class Custom implements Retryer {

private final int maxAttempts;
private final long backoff;
int attempt;

public Custom() {
this(2000, 3);
}

public Custom(long backoff, int maxAttempts) {
this.backoff = backoff;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}

public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}

try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}

@Override
public Retryer clone() {
return new Custom(backoff, maxAttempts);
}
}

更新了示例 Retryer基于 Retryer.Default 的示例配置.

关于spring - Feign 客户端和 Spring 重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151448/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com