springboot2+spring-retry配置及使用

参考

https://blog.csdn.net/qq877728715/article/details/110039456
https://my.oschina.net/u/4293290/blog/3423913
https://blog.csdn.net/huanongying123/article/details/104417712

1 配置依赖

<dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.3.0.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.3.RELEASE</version>
</dependency>

2 启动类开启retry

启动类上添加@EnableRetry注解。

@EnableRetry
@SpringBootApplication
@MapperScan({"com.test.springbootplus.**.mapper"})
public class RetryTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RetryTestApplication .class, args);
    }

}

3 使用

service层方法类上使用注解,设置retry策略。相关注解如下所示。

@Retryable和@Backoff

注解参数说明
value抛出指定异常才会重试
include和value一样,默认为空,当exclude也为空时,默认所有异常
exclude指定异常不重试,默认空,当include也为空时,所有异常都重试
maxAttempts重试次数,默认3
backoff重试补偿机制,默认使用@Backoff。@Backoff:value:每次重试延迟毫秒数,默认为1000L,和delay一样,在指数级情况下用作初始值delay:重试的间隔时间multiplier:delay时间的间隔倍数,(指定延迟倍数)默认为0,表示固定暂停1秒后进行重试。maxDelay:重试次数之间的最大时间间隔,默认为0,如果小于delay的设置,则默认为30000L

@Recover

用于@Retryable重试失败后处理方法,此注解注释的方法参数一定要是@Retryable抛出的异常(即@Retryable的value值),返回类型也需与@Retryable的方法保持一致,否则无法识别。

使用样例

当抛出Exception异常时,在返回响应之前最多重试3次,每次间隔时间1秒、2秒、3.5秒(本应是4秒,但最大间隔是3.5秒)。重试三次后执行@Recover注解所在方法。

    @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 3500))
    public void testRetry() throws Exception {
        log.info("---------  进入  ------------");
        // 这里省略具体的业务逻辑
    }

    @Recover
    public void recover(Exception e) {
        log.error("------------  retry重试后回调方法执行 --------------");
    }
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Seata 是阿里巴巴开源的一款分布式事务解决方案,它提供了高性能、易扩展、易集成的分布式事务解决方案,能够帮助我们解决分布式事务问题。 在 Spring BootSpring Cloud Alibaba 中集成 Seata 需要做以下几步操作: 1. 引入 Seata 的依赖 在 pom.xml 文件中引入 seata-all 的依赖: ```xml <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>1.4.2</version> </dependency> ``` 2. 配置 Seata 的注册中心 在 application.properties 或 application.yml 文件中配置 Seata 的注册中心地址: ```yaml seata: registry: type: nacos nacos: server-addr: localhost:8848 ``` 其中,type 表示注册中心的类型,这里设置为 nacos;nacos 表示 Nacos 注册中心的配置信息。 3. 配置 Seata 的事务组名称 在 application.properties 或 application.yml 文件中配置 Seata 的事务组名称: ```yaml seata: tx-service-group: my_tx_group ``` 其中,tx-service-group 表示事务组名称,可以任意设置。 4. 配置 Seata 的数据源代理 在 application.properties 或 application.yml 文件中配置 Seata 的数据源代理: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 seata: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/seata?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 enable-auto-data-source-proxy: true ``` 其中,datasource 表示业务数据源的配置信息;seata.datasource 表示 Seata 数据源的配置信息;enable-auto-data-source-proxy 表示是否启用数据源代理。 5. 配置 Seata 的代理数据源 在 application.properties 或 application.yml 文件中配置 Seata 的代理数据源: ```yaml spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 seata: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/seata?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 enable-auto-data-source-proxy: true ``` 其中,type 表示数据源的类型,这里使用的是阿里巴巴的 Druid 数据源。 6. 配置 Seata 的 AT 模式 在 application.properties 或 application.yml 文件中配置 Seata 的 AT 模式: ```yaml seata: service: vgroup-mapping: my_tx_group: default group-mapping: default: fescar-seata-example default-transaction-timeout: 300000 tx-service-group: my_tx_group mode: "AT" config: # AT 模式下的数据源类型 client.db-type: "mysql" server: undo: log serialization: "json" log table: "undo_log" datasource: datasource: "seataDataSource" db-type: "mysql" ``` 其中,service.vgroup-mapping 表示事务组映射关系;service.group-mapping 表示事务组和注册中心的映射关系;default-transaction-timeout 表示默认的事务超时时间;tx-service-group 表示事务组名称;mode 表示 Seata 的模式,这里使用的是 AT 模式;config.client.db-type 表示 AT 模式下的数据源类型;config.server.undo.log serialization 表示序列化方式;config.server.undo.log table 表示 undo log 表名;config.server.undo.datasource.datasource 表示 undo log 数据源名称;config.server.undo.datasource.db-type 表示 undo log 数据源类型。 7. 配置 Seata 的 TC 模式 在 application.properties 或 application.yml 文件中配置 Seata 的 TC 模式: ```yaml seata: service: vgroup-mapping: my_tx_group: default group-mapping: default: fescar-seata-example default-transaction-timeout: 300000 tx-service-group: my_tx_group mode: "TC" config: server: port: 8091 max-commit-retry-timeout: 120000 max-rollback-retry-timeout: 120000 ``` 其中,server.port 表示 TC 模式下的端口号;config.server.max-commit-retry-timeout 表示最大提交重试时间;config.server.max-rollback-retry-timeout 表示最大回滚重试时间。 以上就是在 Spring BootSpring Cloud Alibaba 中集成 Seata 的详细配置步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值