Article directory
- set
- 1. Construction of seata server
-
- (1) Download the seata server
- (2) Decompression
- (3) Configure the storage method of seata
- (4) Create seata database and import related tables
- (5) Put the mysql driver jar into the lib directory of the seata service
- (6) seata’s registration center
- (7) Specify which configuration contents of seata are put into the nacos configuration center
- (8) Start the seata service
- 2. Connect the microservice to the seata server
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.
TC: Distributed transaction manager—understood as seata server
TM: Transaction initiator
RM: branch transaction. independent connection
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
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
A service transaction execution branch performs operations on the database
Service A starts calling service B remotely. At this timeThe XID will be propagated in the call chain of the microservice
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
Service B executes branch transactions and performs operations on the database
After the global transaction call chain is processed, TM initiates global transaction submission or rollback to TC according to whether there is an exception.
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
(3) Configure the storage method of seata
(4) Create seata database and import related tables
Table structure: Obtained from the seata source code and
place the script directory in the seata source code into the directory of the seata service
(5) Put the mysql driver jar into the lib directory of the seata service
(6) seata’s registration center
Because the microservice client can access the seata cluster through the registration center
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"
}
}
(7) Specify which configuration contents of seata are put into the nacos configuration center
Modify the content of config.txt
and import the configuration in config.txt above into the nacos configuration center through the nacos script.
Because it is a shell script, windows cannot operate it directly
:
Result:
(8) Start the seata service
Note: Open nacos first and then open the seata service.
If it happens in a flash, you need to create a logs directory in the seata directory. You
can open cmd and execute seata-server.bat to see if it is a log-related issue.
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';
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
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
- 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