package com.shkpr.service.alambizplugin.configuration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.retry.backoff.ExponentialBackOffPolicy; import org.springframework.retry.backoff.FixedBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.support.RetryTemplate; @Configuration @EnableAutoConfiguration public class RetryTemplateConfig { @Primary @Bean(name = "Normal3Retry") public RetryTemplate normal3Retry(){ RetryTemplate template = new RetryTemplate(); SimpleRetryPolicy policy = new SimpleRetryPolicy();//默认最大重试次数为3 ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(200L); backOffPolicy.setMaxInterval(200L); backOffPolicy.setMultiplier(2.0);//第一次间隔为T1=200ms,第二次为T2=2*T1=400ms,第三次为T3=2*T2=800ms template.setBackOffPolicy(backOffPolicy); template.setRetryPolicy(policy); return template; } @Bean(name = "FixedPeriod3Retry") public RetryTemplate fixedPeriod3Retry(){ RetryTemplate template = new RetryTemplate(); SimpleRetryPolicy policy = new SimpleRetryPolicy();//默认最大重试次数为3 FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy(); fixedBackOffPolicy.setBackOffPeriod(500L); //每次重试固定间隔500ms template.setBackOffPolicy(fixedBackOffPolicy); template.setRetryPolicy(policy); return template; } }