Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is spring cloud's consulRetryInterceptor?

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the consulRetryInterceptor of spring cloud is. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Order

Mainly study the consulRetryInterceptor of spring cloud

ConsulRetryInterceptor

SpringMurphy cloudhouse consortium Murray 2.1.2.RELEASEMohamsources.jarwithorgAccording to springframeworkAccording to consulatory 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 registers consulRetryInterceptor, which creates RetryOperationsInterceptor based on RetryProperties

RetryProperties

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) .append () }}

RetryProperties defines initialInterval, multiplier, maxInterval, maxAttempts attributes

AopAutoConfiguration

SpringMuttbootMuoautovariant requre 2.1.6.RELEASEsarsources.jarAccording orgspringframeworkqorgspringframeworkandbootCPAopAutoConfiguration.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 injects different proxy methods according to spring.aop.proxy-target-class. The default is cglib proxy.

RetryOperationsInterceptor

SpringMerry retryMew 1.2.4.RELEASESES sources.jarAccording orgUnix springframeworkCharger retryUnix retryCept. 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 implements aopalliance's MethodInterceptor;, which wraps invocation as retryCallback, and then uses RetryTemplate to retry.

Summary

RetryConfiguration registers consulRetryInterceptor, which creates RetryOperationsInterceptor based on RetryProperties

RetryProperties defines initialInterval, multiplier, maxInterval, maxAttempts attributes

RetryOperationsInterceptor implements aopalliance's MethodInterceptor;, which wraps invocation as retryCallback, and then uses RetryTemplate to retry.

Doc

RetryProperties

The above content is what is the consulRetryInterceptor of spring cloud? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report