能否将Spring Retry统计信息接入Spring Actuator?
Spring Retry统计接入Spring Actuator的实现方案
当然可行,而且RetryStatistics正是实现这个需求的核心基础类。通过Spring Retry内置的统计功能结合Spring Actuator的自定义端点,就能轻松暴露重试的各类指标。
核心前提依赖
确保项目中已引入以下依赖(以Maven为例):
<dependencies> <!-- Spring Retry --> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <!-- Spring Boot Actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Spring AOP(Retry依赖AOP实现代理) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies>
步骤1:启用RetryTemplate的统计功能
默认RetryTemplate未开启统计,需要在配置中手动启用,并添加自定义监听来捕获超时异常:
@Configuration @EnableRetry public class RetryConfig { @Bean public RetryTemplate retryTemplate(RetryTimeoutListener timeoutListener) { RetryTemplate retryTemplate = new RetryTemplate(); // 配置重试策略(示例:最多重试3次) SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(3); // 配置超时策略(示例:单次尝试超时5秒) TimeoutRetryPolicy timeoutPolicy = new TimeoutRetryPolicy(); timeoutPolicy.setTimeout(5000); // 组合重试+超时策略 CompositeRetryPolicy compositePolicy = new CompositeRetryPolicy(); compositePolicy.setPolicies(new RetryPolicy[]{retryPolicy, timeoutPolicy}); retryTemplate.setRetryPolicy(compositePolicy); // 启用统计功能 retryTemplate.setStatisticsEnabled(true); // 注册超时统计监听 retryTemplate.registerListener(timeoutListener); return retryTemplate; } @Bean public RetryTimeoutListener retryTimeoutListener() { return new RetryTimeoutListener(); } // 自定义监听类:统计超时次数 static class RetryTimeoutListener implements RetryListener { private final AtomicLong timeoutCount = new AtomicLong(0); @Override public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) { return true; } @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {} @Override public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { // 捕获超时异常并计数 if (throwable instanceof TimeoutException) { timeoutCount.incrementAndGet(); } } public long getTimeoutCount() { return timeoutCount.get(); } } }
步骤2:自定义Actuator端点暴露统计数据
创建一个自定义端点,注入RetryTemplate和超时监听,将RetryStatistics的指标转换为易读的格式暴露:
@Endpoint(id = "retryStats") @Component public class RetryStatisticsEndpoint { private final RetryTemplate retryTemplate; private final RetryTimeoutListener timeoutListener; public RetryStatisticsEndpoint(RetryTemplate retryTemplate, RetryTimeoutListener timeoutListener) { this.retryTemplate = retryTemplate; this.timeoutListener = timeoutListener; } @ReadOperation public Map<String, Object> getRetryMetrics() { RetryStatistics stats = retryTemplate.getRetryStatistics(); Map<String, Object> metrics = new HashMap<>(); // 从RetryStatistics提取核心指标 metrics.put("total_attempts", stats.getStartedCount()); // 总发起重试请求数 metrics.put("successful_attempts", stats.getCompletedCount()); // 成功完成的请求数 metrics.put("failed_attempts", stats.getAbortedCount()); // 最终失败终止的请求数 metrics.put("total_retry_attempts", stats.getRetryCount()); // 累计重试的次数(非总请求数) // 从自定义监听提取超时次数 metrics.put("timeout_count", timeoutListener.getTimeoutCount()); return metrics; } }
步骤3:配置Actuator暴露端点
在application.yml中配置允许暴露自定义端点:
management: endpoints: web: exposure: include: retryStats, health, info
验证访问
启动项目后,访问http://localhost:8080/actuator/retryStats即可看到格式化的重试统计数据:
{ "total_attempts": 10, "successful_attempts": 7, "failed_attempts": 3, "total_retry_attempts": 5, "timeout_count": 2 }
关键说明
RetryStatistics是Spring Retry内置的统计载体,启用统计后由RetryTemplate自动维护核心指标,但它没有直接提供超时次数统计,因此需要通过RetryListener捕获超时异常来补充。- 如果不需要统计超时,仅依赖
RetryStatistics就能满足尝试、成功、失败次数的需求。
内容的提问来源于stack exchange,提问作者Isaev Maxim
相关产品推荐
相关产品推荐

