SpringBoot 实战教程(3,mysql 集群和主从原理
} else {
// 正常返回时的结果
result.append(",result=" + attempt.getResult());
}
System.out.println(result.toString());
}
}
public static final Retryer retryer = RetryerBuilder.newBuilder()
// 设置抛出异常重试
.retryIfException()
// 设置重试等待时间为固定 5 秒重试一次
.withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
// 设置尝试次数为 3 次
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
// 设置监听类
.withRetryListener(new MyRetryListener())
.build();
public static boolean run() {
int a = 1 / 0;
return false;
}
public static void main(String[] args) {
try {
retryer.call(() -> TestRetry.run());
} catch (Exception e) {
LoggerUtil.error("retry three time still error.", e);
}
}
}
2.1 maven 依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
2.2 示例代码
Application 启动类上加上 @EnableRetry 的注解
@EnableRetry
public class Application {
...
}
import com.zgd.demo.thread.Application;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class MyBaseTest {
/*
N777777777777MMNN88777N
N777777777777MNZZZ7777O
DZN7777O77777777777777
7777M:::::M7777777++IZZZZM
M777$:::::N777777*M7777M +++++ZZZDN
7 :N MNN$$ $$8 8D8I
*/
// Constant matcher factory methods
@Before
public void init() {
log.info("----------------测试开始---------------");
}
@After
public voi
d after() {
log.info("----------------测试结束---------------");
}
}
import com.zgd.demo.thread.retry.RetryDemoTask;
import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.ExhaustedRetryException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
@Service
@Slf4j
public class SpringRetryDemo {
/*
N777777777777MMNN88777N
N777777777777MNZZZ7777O
DZN7777O77777777777777
7777M:::::M7777777++IZZZZM
M777$:::::N777777*M7777M +++++ZZZDN
7 :N MNN$$ $$8 8D8I
*/
// Constant matcher factory methods
/**
重试所调用方法
@param param
@return
*/
@Retryable(value = {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 2000L,multiplier = 2))
public boolean call(String param){
return RetryDemoTask.retryTask(param);
}
/**
达到最大重试次数,或抛出了一个没有指定进行重试的异常
recover 机制
@param e 异常
*/
@Recover
public boolean recover(Exception e,String param) {
log.error("达到最大重试次数,或抛出了一个没有指定进行重试的异常:",e);
return false;
}
}
package com.zgd.demo.thread.retry;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.remoting.RemoteAccessException;
@Slf4j
public class RetryDemoTask {
/**
重试方法
@return
*/
public static boolean retryTask(String param) {
log.info("收到请求参数:{}",param);
int i = RandomUtils.nextInt(0,11);
log.info("随机生成的数:{}",i);
if (i == 0) {
log.info("为 0,抛出参数异常.");
throw new IllegalArgumentException("参数异常");
}else if (i == 1){
log.info("为 1,返回 true.");
return true;
}else if (i == 2){
log.info("为 2,返回 false.");
return false;
}else{
//为其他
log.info("大于 2,抛出自定义异常.");
throw new RemoteAccessException("大于 2,抛出远程访问异常");
}
}
}
import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class SpringRetryDemoTest extends MyBaseTest {
/*
N777777777777MMNN88777N
N777777777777MNZZZ7777O
DZN7777O77777777777777
7777M:::::M7777777++IZZZZM
M777$:::::N777777*M7777M +++++ZZZDN
评论