我在一个springbean中指定了一个@ Retryable注释,如下所示.
@EnableRetry
@Import({GrpcClientRetryConfig.class})
@Retryable(interceptor = "grpcClientRetryInterceptor", label = "ProfileGrpcClient")
public class ProfileGrpcClient {
public RateLimitConfig getRateLimitConfig()
@Component
@AllArgsConstructor
public class RateLimits {
private final ProfileGrpcClient client;
// Some method calls client.getRateLimitConfig()
}
拦截器定义为GrpcClientRetryConfig
@EnableRetry
public class GrpcClientRetryConfig {
@Bean
public RetryOperationsInterceptor grpcClientRetryInterceptor() {
val template = new RetryTemplate();
val backOffPolicy = new ExponentialBackOffPolicy();
// Set the exponential backoff parameter
val interceptor = new RetryOperationsInterceptor();
template.setRetryPolicy(new SimpleRetryPolicy());
template.setBackOffPolicy(backOffPolicy);
template.setListeners(new GrpcClientRetryListener[] {new GrpcClientRetryListener()});
interceptor.setRetryOperations(template);
return interceptor;
}
}
在GrpcClientRetryListener
中,我试图获得@Retryable
中指定的标签
@Slf4j
@Component
public class GrpcClientRetryListener extends MethodInvocationRetryListenerSupport {
@Override
public <T, E extends Throwable> void onSuccess(
RetryContext context, RetryCallback<T, E> callback, T result) {
if (callback instanceof MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
log.info(
"Retry Succeeded: {}, Count: {}",
getMethodName(methodInvocationRetryCallback),
context.getRetryCount());
}
super.onSuccess(context, callback, result);
}
@Override
public <T, E extends Throwable> void onError(
RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
if (callback instanceof MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
log.info(
"Retry Failing: {}, Count: {}",
getMethodName(methodInvocationRetryCallback),
context.getRetryCount());
}
super.onError(context, callback, throwable);
}
private <T, E extends Throwable> String getMethodName(
MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
return methodInvocationRetryCallback.getLabel();
}
}
但在重试期间,我看到得到的标签像public com.abc.proto.v2.common.ratelimit.RateLimits com.abc.grpcclients.ProfileGrpcClient.getRateLimitConfig()
.完整日志(log)如下
Retry Failing: public com.abc.proto.v2.common.ratelimit.RateLimits com.abc.grpcclients.ProfileGrpcClient.getRateLimitConfig(), Count: 5
有没有办法让我在MethodInvocationRetryListenerSupport
中得到@Retryable
中指定的label
?