<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency>
2.配置类
只有启用@EnabelRetry才行,写在配置类或者启动类上都是可以的
package com.jun.web.config; import org.springframework.context.annotation.Configuration; import org.springframework.retry.annotation.EnableRetry; @Configuration @EnableRetry public class RetryConfig { }
3.服务类
一般将其加到服务类上就行,服务类一般写主要逻辑
package com.jun.web.annotation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; import java.time.LocalTime; @Service public class PayService { private Logger logger = LoggerFactory.getLogger(getClass()); private final int totalNum = 100000; @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 1.5)) public int minGoodsnum(int num) throws Exception { logger.info("减库存开始" + LocalTime.now()); try { int i = 1 / 0; } catch (Exception e) { logger.error("illegal"); } if (num <= 0) { throw new IllegalArgumentException("数量不对"); } logger.info("减库存执行结束" + LocalTime.now()); return totalNum - num; } @Recover public int recover(Exception e) { logger.warn("减库存失败!!!" + LocalTime.now()); //记日志到数据库 return totalNum; } }
package com.jun.web.annotation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PayController { @Autowired private PayService payService; @GetMapping("/retry") public String getNum() throws Exception { int i = payService.minGoodsnum(-1); System.out.println("===="+i); return "succeess"; } }
2019-08-23 10:27:46.702 DEBUG 16036 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : GET "/retry", parameters={} 2019-08-23 10:27:46.702 DEBUG 16036 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String com.jun.web.annotation.PayController.getNum() throws java.lang.Exception 2019-08-23 10:27:46.703 INFO 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : 减库存开始10:27:46.703 2019-08-23 10:27:46.703 ERROR 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : illegal 2019-08-23 10:27:48.703 INFO 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : 减库存开始10:27:48.703 2019-08-23 10:27:48.703 ERROR 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : illegal 2019-08-23 10:27:51.704 INFO 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : 减库存开始10:27:51.704 2019-08-23 10:27:51.704 ERROR 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : illegal 2019-08-23 10:27:51.704 WARN 16036 --- [nio-8080-exec-5] com.jun.web.annotation.PayService : 减库存失败!!!10:27:51.704 ====100000 2019-08-23 10:27:51.704 DEBUG 16036 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Using ‘text/html‘, given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json] 2019-08-23 10:27:51.704 DEBUG 16036 --- [nio-8080-exec-5] m.m.a.RequestResponseBodyMethodProcessor : Writing ["succeess"] 2019-08-23 10:27:51.705 DEBUG 16036 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 200 OK 2019-08-23 10:27:51.714 DEBUG 16036 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : GET "/favicon.ico", parameters={} 2019-08-23 10:27:51.715 DEBUG 16036 --- [nio-8080-exec-6] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []] 2019-08-23 10:27:51.718 DEBUG 16036 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed 200 OK
三:说明
1.@Retryable参数的意思说明
@Backoff
,@Backoff
的value默认为1000L,我们设置为2000L;multiplier(指定延迟倍数)默认为0,表示固定暂停1秒后进行重试,如果把multiplier设置为1.5,则第一次重试为2秒,第二次为3秒,第三次为4.5秒。
原文:https://www.cnblogs.com/shadowlovesunshine/p/14928686.html