Я указал аннотацию @Retryable в bean-компоненте Spring, как показано ниже.
@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(). Полный журнал, как показано ниже
Retry Failing: public com.abc.proto.v2.common.ratelimit.RateLimits com.abc.grpcclients.ProfileGrpcClient.getRateLimitConfig(), Count: 5
Есть ли способ получить label, как указано в @Retryable, в MethodInvocationRetryListenerSupport?




См. @Retryable(interceptor) Javaдокументацию:
/**
* Retry interceptor bean name to be applied for retryable method. Is mutually
* exclusive with other attributes.
* @return the retry interceptor bean name
*/
String interceptor() default "";
Итак, если вы укажете interceptor, все остальные атрибуты аннотации будут игнорироваться.
Поскольку вы предоставляете свой собственный RetryOperationsInterceptor, просмотрите соответствующее свойство setLabel(String label).