【SpringCloud】08 Distributed transaction seata

set

Seata is an open source distributed transaction solution dedicated to providing high-performance and easy-to-use distributed transaction services under a microservice architecture.
Insert image description here
TC: Distributed transaction manager—understood as seata server

TM: Transaction initiator

RM: branch transaction. independent connection

  1. The TM[Captain:] of the A[Order] service applies to TC[seata] to start a global transaction, and TC will create a global transaction and return a unique XID

  2. The RM [connection database] of service A registers the branch transaction with TC and brings it into the jurisdiction of the global transaction corresponding to the XID

  3. A service transaction execution branch performs operations on the database

  4. Service A starts calling service B remotely. At this timeThe XID will be propagated in the call chain of the microservice

  5. The RM of service B registers the branch transaction with TC and brings it into the jurisdiction of the global transaction corresponding to the XID

  6. Service B executes branch transactions and performs operations on the database

  7. After the global transaction call chain is processed, TM initiates global transaction submission or rollback to TC according to whether there is an exception.

  8. TC coordinates all branch transactions under its jurisdiction and decides whether to roll back

1. Construction of seata server

(1) Download the seata server

https://github.com/seata/seata/releases/tag/v1.3.0

(2) Decompression

Insert image description here

(3) Configure the storage method of seata

Insert image description here
Insert image description here

(4) Create seata database and import related tables

Insert image description here

Table structure: Obtained from the seata source code and
Insert image description here
place the script directory in the seata source code into the directory of the seata service
Insert image description here
Insert image description here

(5) Put the mysql driver jar into the lib directory of the seata service

Insert image description here

(6) seata’s registration center

Because the microservice client can access the seata cluster through the registration center
Insert image description here
Insert image description here

registry {
    
    
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  nacos {
    
    
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
    
    
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"
  nacos {
    
    
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

Insert image description here

(7) Specify which configuration contents of seata are put into the nacos configuration center

Insert image description here
Modify the content of config.txt
Insert image description here
Insert image description here
and import the configuration in config.txt above into the nacos configuration center through the nacos script.
Insert image description here

Because it is a shell script, windows cannot operate it directly
: Insert image description here
Insert image description here
Result:
Insert image description here

(8) Start the seata service

Note: Open nacos first and then open the seata service.
Insert image description here

If it happens in a flash, you need to create a logs directory in the seata directory. You
Insert image description here
can open cmd and execute seata-server.bat to see if it is a log-related issue.

Insert image description here

2. Connect the microservice to the seata server

(1) Add a log table to each database

-- 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(20)   NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(100) 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';

Insert image description here

This table records the image before modification and the image after modification

(2) Introduce seata dependencies into each microservice

<!--引入seata的依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

(3) Modify the configuration of microservices

Insert image description here

spring:
  cloud:
    alibaba:
      seata:
        tx-service-group: guangzhou
seata:
  #注册中心的地址
  registry:
    type: nacos
    nacos:
      server-addr: localhost:8848
      username: nacos
      password: nacos
      group: SEATA_GROUP
      application: seata-server
  #配置中心的地址
  config:
    type: nacos
    nacos:
      server-addr: localhost:8848
      username: nacos
      password: nacos
      group: SEATA_GROUP

Other microservice configurations are the same as above.

The lower version of nacos does not support MySQL8. You need a higher version, 1.3.1 or above.

Add on the business method of the order microservice@GlobalTransactional
Insert image description here

  • Under normal circumstances, inventory will be deducted normally and all operations will be performed normally.
  • If an exception occurs, the transaction will be rolled back without any inventory deduction and returned to the previous state.

The tables created in the database will not leave records, because no matter whether the operation successfully commits the transaction or the operation fails and rolls back the transaction, the records will be cleared. It seems that there are no records, but in fact there are records. The time is very short, and they have not had time to look at them. was deleted

Guess you like

Origin blog.csdn.net/qq_60969145/article/details/128102061