HelloWorld
我们首先创建一个SpringBoot应用,引入Maven坐标
<dependencies>
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-starter</artifactId>
<version>{Latest Version}</version>
</dependency>
// 重试模块
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-core</artifactId>
<version>{Latest Version}</version>
</dependency>
</dependencies>
然后我们在SpringBoot的启动项上增加注解@EnableEasyRetry
@SpringBootApplication
@EnableEasyRetry(group = "easy_retry_demo_group")
public class EasyRetrySpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(EasyRetrySpringbootApplication.class, args);
}
}
这个启动类中写入的easy_retry_demo_group对应的是我们控制台中的组名称
![](/img/建立控制台上的组.png)
在控制台中我们点击 - 总在线机器豆腐块即可看到当前的客户端已经注册到了我们的机器列表中了
![](/img/查看机器列表.png)
随后我们来写一个最简单的Service服务应用Easy-Retry
@Component
public class LocalRetryService {
@Retryable(scene = "localRetry")
public void localRetry(){
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
}
大家可以看到这段代码中我们添加了一个注解@Retryable(scene = "localRetry"),在其中指定了参数值scene,这个scene对应着控制台中的场景,我们可以在这里理解为场景就是组下面的唯一标识。 那么接下来我们来测试一下这段代码
@Test
public void localRetryTest(){
localRetryService.localRetry();
}
当不指定重试次数时,默认会重试三次,所以我们可以看到控制台上供给打印了四次"local retry 方法开始执行"后才抛出ArithmeticException异常信息。
![](/img/Easy-Retry启动案例.png)
上次更新: 2024/04/07, 10:27:31