1. 程式人生 >實用技巧 >其它 >Seata的使用簡介

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 註解即可