Seata service version 1.3.0 build (You said you hate your mother’s philistine smoothness, but you didn’t know that she also used to brew wine with spring flowers and brew tea with spring water; you said you hated your father’s sophistication and hypocrisy, but you didn’t know that he was also full of stars and unrestrained.)

Directory

1. Preparations

1.1 Database related

1.2 seata configuration related

2. The microservice project connects to seata


1. Preparation

(1) First, you need to create a database as shown in the figure below to simulate a distributed architecture:

The database name should preferably be consistent with the one shown in the diagram, so as to avoid some redundant modification operations

(2) Download the code for the desired project

Link: https://pan.baidu.com/s/1OFh1p0BJxfnzKYQgWtx4cA
Extraction code: xpnk

Unzip seata_parent.zip

And use idea to open the project (it is better to delete the .idea folder, because the maven path is different)

The project structure is as follows:

(3) Import the sql files in the project into the previously created database

If it is version 8.0 of mysql, just delete all COLLATE = utf8mb4_0900_ai_ci fields in the sql file to run successfully.

The table structure and data are shown in the figure:

(1) Separately decompress the downloaded seata compressed package

(2) Copy the script directory in seata-1.3.0 to seata-server-1.3.0/seata/

(3) Modify the file.conf file

(4) Import the mysql.sql file into the seata database

Steps: Open the \seata\script\server\db directory in seata-server-1.3.0, and run the sql file directly in seata.

(5) Move the jar package of your corresponding mysql version into the upper directory lib, here is version 5

(6) Modify the configuration center and registration center

Open the registry.conf file

The modification is as follows:

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"
  }
  
}

Nacos is used here, so other registration and configuration centers can be deleted directly, which looks cleaner.

(7) Modify the configuration and select some configuration information registered to nacos

Edit the config.txt file

The content is as follows:

The default is zhengzhou, which is convenient for seata management

(8) Put the modified configuration above into the configuration center

Be sure to start the local nacos first, otherwise you cannot register

Find the nacos directory

Open a cmd window and use the command:

.\
acos-config.sh

Or double-click directly, here is using git is the control panel, it is recommended to download a git first

After running, check the nacos console

Found that the registration has been successful

(9) Double-click the bin directory to start seata

Or use the cmd console command

.\seata-server.bat

Start successfully

View nacos service list

Indicates that the configuration has been successful.

2. Microservice project connection seata

(1) Create an undo_log table in each microservice database

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';

This table is used to record the log of data rollback. Regardless of success or failure, the data will be deleted eventually and the table will be cleared.

(2) The parent project of the microservice project introduces dependencies

 <dependencies>
        <!--seata must ensure that it matches the version of the seata service-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>
    </dependencies>

(3) The yml configuration file of each microservice opens the content of the comment

(4) Modify the transaction annotation to @GlobalTransactional in the OrderServiceImpl class under the package com.ykq.service.impl

(4) Start all microservices

It is found that each microservice has successfully connected to seata

(5) Start the test

Browser access: http://localhost:8001/order/addOrder?userId=1 & amp;productId=1 & amp;count=10 & amp;money=200

If you want to see the rollback operation of the transaction, you can add a division by 0 exception in the OrderServiceImpl class to simulate the failure of the microservice call link, resulting in the problem of distributed transactions.

Normally:

In exceptional circumstances:

Restart the order service after modification

Check the database after visiting again, and found that the rollback was successful, and there was no distributed transaction problem

Check the seata console to see detailed information