Seata的使用简介

Seata的使用简介

一,引入依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>

二,seata官网下载压缩包
1,修改conf目录下的registry.conf文件

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "DEFAULT_GROUP"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }

2,修改file.conf文件
1.3.0版本没有service块,需要再老版本中复制

service {
  #vgroup->rgroup
  vgroupMapping.gulimall-seata-service-group = "default"
  #only support single node
  default.grouplist = "localhost:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

三,复制registry.conf和file.conf到项目的resource目录中

四,微服务项目配置
1,需要事务控制的微服务中增加配置数据源配置文件,内容如下:

@Configuration
public class SeataConfig {
    @Autowired
    DataSourceProperties dataSourceProperties;


    @Primary
    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(dataSourceProperties.getName())) {
            dataSource.setPoolName(dataSourceProperties.getName());
        }
        return new DataSourceProxy(dataSource);
    }
}

2,application.properties文件中增加配置,需要和file.conf中的 vgroupMapping.xx保持一致

spring.cloud.alibaba.seata.tx-service-group=gulimall-seata-service-group

五,启动seata-server,并在业务方法上使用@GlobalTransactional 注解即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值