seata springcloud Integration Tutorial and Pits Encountered

  • 2021-10-24 22:55:51
  • OfStack

SEATA Summary

seata is a distributed transaction manager from alibaba, which has the characteristics of small intrusion and simple implementation. We can use seata to implement distributed transaction management,

Is a necessary component of microservice. It can realize transaction management between microservices and multiple data sources.

seata has applications in Ali and many companies, so we can use it with confidence.

Dependency


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

Table building

The database of client service in AT mode needs to build a table undo_log
Otherwise, an error is reported

java. sql. SQLSyntaxErrorException: Table 'psr_enterprise_control_test. undo_log' doesn 't exist

Official GIT script file


-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

Configure

application.yml


seata:
  enabled: true
  enable-auto-data-source-proxy: true
  tx-service-group: my_test_tx_group #  And seata.service.vgroup-mapping1 To 
  registry:
    type: nacos #  And seata Same registry 
    nacos:
      application: seata-server
      server-addr: ${PSR_NACOS:localhost:8848}
      namespace: test
      group: application
      cluster: default
  config:
    type: nacos #  And seata Same configuration center 
    nacos:
      server-addr: ${PSR_NACOS:localhost:8848}
      group: seata
      namespace: test
  service:
    vgroup-mapping:
      my_test_tx_group: default #  Transaction group name 
    disable-global-transaction: false
  client:
    rm:
      report-success-enable: false

Enable global transactions

@GlobalTransactional


Related articles: