扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
本篇文章为大家展示了spring cloud的consulRetryInterceptor是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
我们提供的服务有:成都网站设计、网站制作、外贸营销网站建设、微信公众号开发、网站优化、网站认证、水磨沟ssl等。为近1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的水磨沟网站制作公司
主要研究一下spring cloud的consulRetryInterceptor
spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/ConsulAutoConfiguration.java
@Configuration @EnableConfigurationProperties @ConditionalOnConsulEnabled public 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
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属性
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代理
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
RetryOperationsInterceptor实现了aopalliance的MethodInterceptor;它将invocation包装为retryCallback,然后使用RetryTemplate实现重试
RetryConfiguration注册了consulRetryInterceptor,它基于RetryProperties创建了RetryOperationsInterceptor
RetryProperties定义了initialInterval、multiplier、maxInterval、maxAttempts属性
RetryOperationsInterceptor实现了aopalliance的MethodInterceptor;它将invocation包装为retryCallback,然后使用RetryTemplate实现重试
RetryProperties
上述内容就是spring cloud的consulRetryInterceptor是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流