千家信息网

spring cloud的consulRetryInterceptor是什么

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇文章为大家展示了spring cloud的consulRetryInterceptor是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。序主要研究一下s
千家信息网最后更新 2026年11月03日spring cloud的consulRetryInterceptor是什么

本篇文章为大家展示了spring cloud的consulRetryInterceptor是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

序

主要研究一下spring cloud的consulRetryInterceptor

consulRetryInterceptor

spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/ConsulAutoConfiguration.java

@Configuration@EnableConfigurationProperties@ConditionalOnConsulEnabledpublic class ConsulAutoConfiguration {        //......        @ConditionalOnClass({ Retryable.class, Aspect.class, AopAutoConfiguration.class })        @Configuration        @EnableRetry(proxyTargetClass = true)        @Import(AopAutoConfiguration.class)        @EnableConfigurationProperties(RetryProperties.class)        protected static class RetryConfiguration {                @Bean(name = "consulRetryInterceptor")                @ConditionalOnMissingBean(name = "consulRetryInterceptor")                public RetryOperationsInterceptor consulRetryInterceptor(                                RetryProperties properties) {                        return RetryInterceptorBuilder.stateless()                                        .backOffOptions(properties.getInitialInterval(),                                                        properties.getMultiplier(), properties.getMaxInterval())                                        .maxAttempts(properties.getMaxAttempts()).build();                }        }        //......}
  • RetryConfiguration注册了consulRetryInterceptor,它基于RetryProperties创建了RetryOperationsInterceptor

RetryProperties

spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/RetryProperties.java

@ConfigurationProperties("spring.cloud.consul.retry")public class RetryProperties {        /** Initial retry interval in milliseconds. */        private long initialInterval = 1000;        /** Multiplier for next interval. */        private double multiplier = 1.1;        /** Maximum interval for backoff. */        private long maxInterval = 2000;        /** Maximum number of attempts. */        private int maxAttempts = 6;        public RetryProperties() {        }        public long getInitialInterval() {                return this.initialInterval;        }        public void setInitialInterval(long initialInterval) {                this.initialInterval = initialInterval;        }        public double getMultiplier() {                return this.multiplier;        }        public void setMultiplier(double multiplier) {                this.multiplier = multiplier;        }        public long getMaxInterval() {                return this.maxInterval;        }        public void setMaxInterval(long maxInterval) {                this.maxInterval = maxInterval;        }        public int getMaxAttempts() {                return this.maxAttempts;        }        public void setMaxAttempts(int maxAttempts) {                this.maxAttempts = maxAttempts;        }        @Override        public String toString() {                return new ToStringCreator(this).append("initialInterval", this.initialInterval)                                .append("multiplier", this.multiplier)                                .append("maxInterval", this.maxInterval)                                .append("maxAttempts", this.maxAttempts).toString();        }}
  • RetryProperties定义了initialInterval、multiplier、maxInterval、maxAttempts属性

AopAutoConfiguration

spring-boot-autoconfigure-2.1.6.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/aop/AopAutoConfiguration.java

@Configuration@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class, AnnotatedElement.class })@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)public class AopAutoConfiguration {        @Configuration        @EnableAspectJAutoProxy(proxyTargetClass = false)        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",                        matchIfMissing = false)        public static class JdkDynamicAutoProxyConfiguration {        }        @Configuration        @EnableAspectJAutoProxy(proxyTargetClass = true)        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",                        matchIfMissing = true)        public static class CglibAutoProxyConfiguration {        }}
  • AopAutoConfiguration根据spring.aop.proxy-target-class来注入不同的代理方式,默认是cglib代理

RetryOperationsInterceptor

spring-retry-1.2.4.RELEASE-sources.jar!/org/springframework/retry/interceptor/RetryOperationsInterceptor.java

public class RetryOperationsInterceptor implements MethodInterceptor {        private RetryOperations retryOperations = new RetryTemplate();        private MethodInvocationRecoverer recoverer;        private String label;        public void setLabel(String label) {                this.label = label;        }        public void setRetryOperations(RetryOperations retryTemplate) {                Assert.notNull(retryTemplate, "'retryOperations' cannot be null.");                this.retryOperations = retryTemplate;        }        public void setRecoverer(MethodInvocationRecoverer recoverer) {                this.recoverer = recoverer;        }        public Object invoke(final MethodInvocation invocation) throws Throwable {                String name;                if (StringUtils.hasText(label)) {                        name = label;                } else {                        name = invocation.getMethod().toGenericString();                }                final String label = name;                RetryCallback retryCallback = new RetryCallback() {                        public Object doWithRetry(RetryContext context) throws Exception {                                                                context.setAttribute(RetryContext.NAME, label);                                /*                                 * If we don't copy the invocation carefully it won't keep a reference to                                 * the other interceptors in the chain. We don't have a choice here but to                                 * specialise to ReflectiveMethodInvocation (but how often would another                                 * implementation come along?).                                 */                                if (invocation instanceof ProxyMethodInvocation) {                                        try {                                                return ((ProxyMethodInvocation) invocation).invocableClone().proceed();                                        }                                        catch (Exception e) {                                                throw e;                                        }                                        catch (Error e) {                                                throw e;                                        }                                        catch (Throwable e) {                                                throw new IllegalStateException(e);                                        }                                }                                else {                                        throw new IllegalStateException(                                                        "MethodInvocation of the wrong type detected - this should not happen with Spring AOP, " +                                                                        "so please raise an issue if you see this exception");                                }                        }                };                if (recoverer != null) {                        ItemRecovererCallback recoveryCallback = new ItemRecovererCallback(                                        invocation.getArguments(), recoverer);                        return this.retryOperations.execute(retryCallback, recoveryCallback);                }                return this.retryOperations.execute(retryCallback);        }        /**         * @author Dave Syer         *         */        private static final class ItemRecovererCallback implements RecoveryCallback {                private final Object[] args;                private final MethodInvocationRecoverer recoverer;                /**                 * @param args the item that failed.                 */                private ItemRecovererCallback(Object[] args, MethodInvocationRecoverer recoverer) {                        this.args = Arrays.asList(args).toArray();                        this.recoverer = recoverer;                }                public Object recover(RetryContext context) {                        return recoverer.recover(args, context.getLastThrowable());                }        }}
  • RetryOperationsInterceptor实现了aopalliance的MethodInterceptor;它将invocation包装为retryCallback,然后使用RetryTemplate实现重试

小结

  • RetryConfiguration注册了consulRetryInterceptor,它基于RetryProperties创建了RetryOperationsInterceptor

  • RetryProperties定义了initialInterval、multiplier、maxInterval、maxAttempts属性

  • RetryOperationsInterceptor实现了aopalliance的MethodInterceptor;它将invocation包装为retryCallback,然后使用RetryTemplate实现重试

doc

  • RetryProperties

上述内容就是spring cloud的consulRetryInterceptor是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

内容 属性 技能 知识 代理 包装 不同 简明 简明扼要 小结 就是 文章 方式 更多 篇文章 行业 资讯 资讯频道 频道 一亮 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 科技赋能下的互联网金融 互联网科技保险 没上市的科技互联网公司 北京互联网科技 互联网科技英文 北京互联网络科技有限公司 菲比寻常互联网科技 天下捷融互联网科技 互联网科技的未来发展 互联网科技咨询 中国最先进互联网科技公司 玖二柒互联网科技公司待遇 互联网科技与管理 千方科技属于互联网企业吗 江阴互联网智能科技产品进口 明优互联网科技有限公司 科技赋能视角下互联网保险发展 南京一网一红互联网科技有限公司 互联网科技平台产品运营 瑞安市蟠桃会互联网科技有限公司 有格互联网科技 美国互联网保险保险科技 金融科技创新监管互联网金融 金融科技与互联网金融 菲比寻常互联网科技 互联网科技汛 科技互联网海报 艺电能源互联网科技有限公司招聘 美国五大互联网科技公司市值多少 互联网的科技体现在哪里
0