我有一个restful服务使用Spring虚拟客户端调用外部服务
@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拒绝执行http://external-service/external/payments json.message:Send付款加上付款失败的其他原因:{ERROR_RESPONSE_BODY=Connection拒绝执行http://external-service/external/payments,EVENT=ADD_PAYMENT_FAILURE,feign.FeignException.errorExecuting(FeignException.java:67) } {} json.EVENT:ADD_PAYMENT_FAILURE json.stack_trace:feign.RetryableException:连接拒绝在feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)执行POST http://external-service/external/payments
是否可以在假客户端上添加Spring重试。我想用什么来注释addPayment操作
@Retryable(value = {feign.RetryableException.class }, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier=2))但这是不可能的,我还有其他选择吗?
发布于 2017-11-07 06:55:53
您可以在Retryer中添加FeignClientConfig
@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。
发布于 2017-11-07 12:29:37
如果使用带状设置属性,则可以使用下面的属性进行重试:
myapp.ribbon.MaxAutoRetries=5
myapp.ribbon.MaxAutoRetriesNextServer=5
myapp.ribbon.OkToRetryOnAllOperations=true注意:"myapp“是您的服务id。
查看此Github实施以获取工作示例
发布于 2021-08-25 03:40:15
只是一个新的结构Default
@Configuration
public class FeignClientConfig {
@Bean
public Retryer retryer() {
return new Retryer.Default(100, 2000, 3);
}
}https://stackoverflow.com/questions/47151448
复制相似问题