In distributed systems, in order to ensure strong consistency of data distributed transactions, when calling the RPC interface or sending MQ, you will take a retry operation to prevent the network jitter request timeout. The most common way to retry is MQ, but if you do not introduce MQ in your project, it will be inconvenient. This article mainly introduces how to use Spring Retry to implement retry operations.
1. Add maven dependencies
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency>
2. Add retry configuration in startup
@SpringBootApplication @EnableRetry public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 3. Write Service
@Service public class RemoteService { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Retryable(value= {BusinessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 2)) public void call() throws Exception { logger.info("do something..."); throw new BusinessException("RPC call exception"); } @Recover public void recover(BusinessException e) { logger.info(" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4. Write Controller
@RestController @RequestMapping("/test") public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Autowired private RemoteService remoteService; @RequestMapping("/test") public String login() throws Exception { remoteService.call(); return String.valueOf("11"); } 5. Visit http://localhost:8080/test/test
6. Test log
2017-07-25 19:28:07 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:12 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.call(RemoteService.java:19)] do something...
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:24)] ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2017-07-25 19:28:22 [INFO]-[http-nio-53602-exec-1]-[com.test.retry.service.RemoteService.recover(RemoteService.java:25)] RPC call exception
7. Related configuration instructions
@EnableRetry Can you try again ? When the proxyTargetClass property is true, use CGLIB proxy. Standard JAVA annotations are used by default. In spring Boot, this parameter can be written in the program entrance.
@Retryable The method of labeling this annotation will be retryed when an exception occurs
value: Specify the exception class to be processed
include: The specified exception class handled is the same as the value, and the default is empty. When the exclude is also empty, all exceptions are defaulted to be all
exclude: Specify that exceptions are not processed, default is empty, when include is also empty, default all exceptions
maxAttempts: Maximum number of retries. Default 3 times
backoff: Retry the waiting policy. Use @Backoff annotation by default
@Backoff Retry the waiting policy <br />When the parameters are not set, FixedBackOffPolicy is used by default (specify the waiting time), and retry waits for 1000ms
Set delay, use FixedBackOffPolicy (specify the waiting time), and try the waiting time to be filled in again
When setting delay and maxDealy, try again and wait for the homogeneous distribution between these two values
Set delay, maxDealy, and multiplier, use ExponentialBackOffPolicy (implementation of exponential retry interval), multiplier specifies the delay multiple, such as delay=5000l, multiplier=2, the first retry is 5 seconds, the second time is 10 seconds, and the third time is 20 seconds...
@Recover is used for @Retryable's failed retry method. The method parameters of this annotation must be an exception thrown by @Retryable, otherwise it will not be recognized. Log processing can be performed in this method.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.