Seata study notes

Posted by Paingiver on Tue, 26 Oct 2021 12:03:06 +0200

Installation steps:

Download from the official website:
Official website

Database preparation

Deployment tutorial
Simply put, you need to build a seata database in the database, which stores some data that needs to be rolled back at that time
sql script
You also need to create an undo table in the database you need to roll back to roll back:
undo sql

Modification of configuration file

The profile you need to modify:

Modify the registered and stored respectively:
Very simple columns, such as the modification of file.conf:

The first line indicates what to store

Then select db to represent the database, and then modify those corresponding to db into their own data:

Like register.conf:
Note that there are two type s that need to be modified:


The above is the preparation before use

Start using

1: Start nacos and seata services

Because nacos is used for registration, you need to use nacos for matching
It's easy to start. Just open the bat file in the bin directory:
Pay attention to start nacos first

2: Establishment of microservices

There are three microservices and three databases used in this test:
Order user inventory
It's easy to place an order, generate an order, deduct the user's amount and reduce inventory

step

1: Change pom:

 <dependencies>
        <!--nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>seata-all</artifactId>
                    <groupId>io.seata</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>1.4.1</version> <!--You'd better change to the version you downloaded on the official website-->
        </dependency>
        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2: Change yaml: there's basically nothing to change

server:
  port: 2002

spring:
  application:
    name: seata-storage-service
  cloud:
    alibaba:
#      seata:
#        tx-service-group: fsp_tx_group group settings
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/seata_storage
    username: root
    password: 1234

logging:
  level:
    io:
      seata: info

mybatis:
  mapperLocations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

3: You need to copy the configuration file under resources

3: Testing

Write this annotation on the controller or serviceimple method of the transaction you need to roll back:

@GlobalTransactional


Explanation of some parameters:
Name: it's name
The rest of the explanation

Supplement: (continuously updated)

Overall process:


Explanation of tm tc rm: that's about it

Rollback principle


This is the first stage of operation. The most important is the before and after snapshots

This is the operation for which a rollback has occurred
The following can be used to explain:

As shown in the figure, I go through the first stage of the process before modifying the data
Save a before snapshot and store age=22
Then modify age =28
Then an after snapshot is created and age=28 is stored
This concludes the first phase

If a transaction rollback occurs
He first checked the database for age
If age=28, it is the same as the age comparison of after snapshot storage
Then set the age value stored in the before snapshot
If age= 28 and after is 28, that proves dirty reading
It will go to manual processing

Topics: Spring Cloud Microservices