Spring Cloud Alibaba [Understand distributed things, scenarios of distributed transactions, what is two-phase commit, XA scheme, Seata scheme, business description, download and start Seata service] (10)

 

Table of contents

Distributed transaction processing_Understanding distributed transactions

Distributed transaction processing_Scenes generated by distributed transactions

Distributed transaction solution_what is two-phase commit

Distributed transaction solution_XA solution

Distributed transaction solution_Seata solution

Seata provides XA mode to realize distributed transactions_Business Description

Seata provides XA mode to realize distributed transaction_Download and start Seata service


 

Distributed transaction processing_Understanding distributed transactions

foreword 

With the rapid development of the Internet, the software system has changed from the original single application to distributed application. The following figure describes the evolution from single application to microservice.

Note: A distributed system will split an application system into multiple services that can be deployed independently. Therefore, remote collaboration between services is required to complete transaction operations. In this distributed system environment, transactions completed by remote collaboration between different services through the network are called distributed transactions. 

If there is no distributed transaction 

In a series of microservice systems, if there is no distributed transaction, what will happen? Let us take the transaction business commonly used in the Internet as an example:

 

Explanation: The above figure contains two independent microservices of inventory and order, and each microservice maintains its own database. In the business logic of the trading system, before an order is placed, a product needs to call the inventory service to deduct the inventory, and then call the order service to create an order record. 

Under normal circumstances, the two databases are updated successfully, and the data on both sides maintains consistency.

 

However, under abnormal circumstances, it is possible that the inventory deduction is completed, but the subsequent order record fails to be inserted for some reason. At this time, the data on both sides loses its due consistency. 

 

Problem: At this time, it is necessary to ensure data consistency. The consistency of a single data source is guaranteed by stand-alone transactions, and the consistency of multiple data sources is guaranteed by distributed transactions. 

What is a distributed transaction 

It means that a large operation is composed of different small operations, and these small operations are distributed on different servers. Distributed transactions need to ensure that these small operations either all succeed or all fail. In essence, distributed transactions are to ensure the data consistency of different databases.

Real-time effect feedback

1. The main function of distributed things is _____.

A Solve the problem of data single point of failure

B guarantees the data consistency of different databases 

C to solve performance problems

All of the above are wrong

Distributed transaction processing_Scenes generated by distributed transactions

Across JVM processes 

After we split the single project into distributed and microservice projects, the various services cooperate to complete business operations through remote REST or RPC calls.

Typical scenario: Order microservice and inventory microservice in the mall system. When users place an order, they will access the order microservice. When order records are generated, the order microservice will call the inventory microservice to deduct inventory. Each microservice is deployed in different JVM processes. At this time, distributed transaction problems caused by cross-JVM processes will arise. 

across database instances 

When a single system accesses multiple database instances, that is, distributed transactions will be generated when accessing across data sources.

 

Typical scenario: For example, the order database and transaction database in our system are placed in different database instances. When a user initiates a refund, the user's order database and transaction database will be operated at the same time, the refund operation will be performed in the transaction database, and the status of the order will be changed to refunded in the order database. Since the data is distributed in different database instances, it is necessary to operate the data in the database through different database connection sessions. At this time, distributed transactions are generated. 

Multiple service databases 

Multiple microservices access the same database.

Typical scenario: For example, distributed transactions will also be generated when order microservices and inventory microservices access the same database. The reason is that when multiple microservices access the same database, they essentially operate the database through different database sessions. At this time, distributed transactions will occur. 

 Real-time effect feedback

1. The following belongs to the generation scene of distributed things is _____.

A across JVM processes

B cross-database instance

C multiple service databases

D above are correct

Distributed transaction solution_what is two-phase commit

Two-phase commit is also called 2PC. 2PC is a very classic strong consistent and centralized atomic commit protocol. The centralization mentioned here means that there are two types of nodes in the protocol: one is the centralized coordinator node (coordinator) and N participant nodes (partcipant).

Two phases: first phase: voting phase and second phase: submission/execution phase. 

2pc example 

A organizes B, C and D to climb a mountain: if everyone agrees to climb a mountain, the event will be held; if one person disagrees, the event will be cancelled.

First A will be the coordinator of the activity, and B, C and D will be the participants of the activity.

 

specific process:

Phase 1:   

①A sends emails to B, C, and D, proposing to go hiking next Wednesday, and asks if they agree. Then at this time A needs to wait for the mails of B, C and D.   

②B, C and D check their schedules respectively. B and C find that they have no activity arrangements on that day, so they send an email to tell A that they agree to go climbing next Wednesday. For some reason, D didn't check his mail during the day. Then A, B and C all need to wait at this time. In the evening, D found A's email, and then checked the schedule, and found that there were other arrangements on Wednesday, so D replied to A that the event would be cancelled.

Phase 2:   

① At this time, A has received emails from all the participants of the event, and A finds that D will not be able to climb the mountain next Wednesday. Then A will send an email to notify B, C and D that the mountain climbing activity will be canceled next Wednesday.   

②At this time, B and C reply to A "It's a pity", and D replies to A "I'm sorry". At this point the transaction is terminated. 

2PC stage processing flow

 For example, order service A needs to call payment service B to pay. If the payment is successful, the shopping order will be processed as pending delivery; otherwise, the shopping order will be processed as failed.

Phase 1: Voting Phase

The first phase is divided into three steps:

1. Transaction query: The coordinator sends a transaction preprocessing request to all participants, called Prepare, and begins to wait for the response of each participant.

2. Execute local transactions: Each participant node executes local transaction operations, but does not actually submit the local transaction of the database after the execution is completed, but first reports to the coordinator: "My side can handle it/I can't handle it here". .

3. Each participant feeds back the response to the coordinator's query: if the participant successfully executes the transaction operation, it will feed back a Yes response to the coordinator, indicating that the transaction can be executed. If no participant successfully executes the transaction, it will feed back a No response to the coordinator, indicating that the transaction cannot be executed. After the first phase is executed, there are two possibilities. 1. All return Yes. 2. One or more returns No. 

Phase 2: Commit/Execute Phase (Success Flow) 

Success condition: All participants return Yes.

The second phase of the exception process is also divided into two steps

Send a rollback request

The coordinator sends a RoollBack request to all participant nodes.

transaction rollback

After the participant receives the RoollBack request, it will roll back the local transaction. 

2PC disadvantages 

1. Performance issues

Whether it is in the first phase or in the second phase, all participant resources and coordinator resources are locked. Only when all nodes are ready, the transaction coordinator will notify the global commit, and the participants will release the resources after the local transaction commit. Such a process will be relatively long and have a greater impact on performance.

2. Single node failure

Due to the importance of the coordinator, once the coordinator fails. Participants will be blocked forever. Especially in the second stage, if the coordinator fails, all participants are still in the state of locking transaction resources, and cannot continue to complete transaction operations.

Real-time effect feedback

1. The following is a disadvantage of the distributed solution 2PC is _____.

A performance problem

B Single point of failure problem

C The coordinator is down, and the participants are normal

D above are correct

2. Two-phase commit is also called 2PC, and 2PC is a very classic ____.

A centralized atomic commit protocol

B Strong consensus commit protocol

C strong consistent, centralized atomic commit protocol

D above are all wrong 

Distributed transaction solution_XA solution

What is DTP 

The traditional 2PC solution is implemented at the database level. For example, Oracle and MySQL both support the 2PC protocol. In order to unify standards and reduce unnecessary docking costs in the industry, it is necessary to formulate standardized processing models and interface standards. The Open Group, an international open standard organization, defines a distributed transaction processing model DTP (Distributed Transaction Processing Reference Model ) .

DTP model defines roles 

1. AP (Application Program): the application program, which can be understood as a program that uses DTP distributed transactions.

2. RM (Resource Manager): Resource Manager, which can be understood as a transaction participant, generally refers to a database instance, the database is controlled through the Resource Manager, and the Resource Manager controls branch transactions.

3. TM (Transaction Manager): The transaction manager is responsible for coordinating and managing transactions. The transaction manager controls global transactions, manages the transaction life cycle, and coordinates various RMs. A global transaction refers to a distributed transaction processing environment in which multiple databases need to be operated to complete a job together. This job is a global transaction.

 Note: The DTP model defines the interface specification for communication between TM and RM called XA, which is simply understood as the 2PC interface protocol provided by the database, and the realization of 2PC based on the XA protocol of the database is also called the XA scheme.

 

Implementation process:

1. The application holds two data sources, the user library and the point library.

2. The application notifies the user library RM to add a new user through TM, and at the same time notifies the point library RM to add points for the user. RM has not submitted anything at this time, and the user and point resources are locked at this time.

3. When TM receives the reply, as long as one party fails, it will initiate a rollback transaction to other RMs respectively. After the rollback is completed, the resources will be released. 4. The TM receives the execution reply and all of them are successful. At this time, it submits the transaction to all RMs. After the submission is completed, the resource lock is released. 

Real-time effect feedback

1. The traditional solution of 2PC in distributed things is realized at the ____ level.

A application layer

B service layer

C database

All of the above are wrong

2. The following ones that do not belong to the role of the DTP model are ____.

A AP

B CP 

C RM

D TM

Distributed transaction solution_Seata solution

What is Seata 

Seata is an open source distributed transaction solution dedicated to providing high-performance and easy-to-use distributed transaction services. Seata provides users with AT, TCC, SAGA and XA transaction modes to create a one-stop distributed solution for users.

 

Seata Overall Framework 

Relationship diagram of global transaction and branch transaction

Similar to the traditional 2PC model, Seata defines three components to protocol the processing of distributed transactions 

specific process: 

Transaction Coordinator (TC): The transaction coordinator is an independent middleware that needs to be independently deployed and operated. It maintains the running status of the global transaction, receives TM instructions to initiate the commit and rollback of the global transaction, and is responsible for communicating with the RM to coordinate the commit or rollback of each branch transaction.

Transaction Manager (TM): The transaction manager, TM needs to be embedded in the application program, it is responsible for opening a global transaction, and finally initiates a global commit or global rollback instruction to the TC.

Resource Manager (RM): controls branch transactions, is responsible for branch registration, status reporting, and receives instructions from the transaction coordinator TC to drive the commit and rollback of branch (local) transactions.

Also take the example of Seata’s distributed transaction process for new user registration to send points

 

Implementation process:

1. The TM of the user service applies to the TC to start a global transaction. The global transaction is successfully created and a globally unique XID is generated.

2. The RM of the user service registers the branch transaction with the TC, and the branch transaction executes the new user logic in the user service, and brings it into the jurisdiction of the global transaction corresponding to the XID.

3. The user service executes the branch transaction and inserts a record into the user table.

4. When the logic is executed to remotely call the credit service (XID is propagated in the context of the microservice call link). The RM of the credit service registers the branch transaction with the TC, and the branch transaction executes the logic of increasing the credit and brings it into the jurisdiction of the global transaction corresponding to the XID.

5. The points service executes the branch transaction, inserts a record into the points record table, and returns to the user service after the execution is completed.

6. The execution of the user service branch transaction is completed.

7. The TM initiates a global commit or rollback resolution for the XID to the TC.

8. The TC schedules all branch transactions under the jurisdiction of the XID to complete the commit or rollback request. 

The difference between Seata's implementation of 2PC and traditional 2PC 

1. In terms of architecture level, the RM of the traditional 2PC solution is actually at the database layer. RM is essentially the database itself, implemented through the XA protocol, while Seata's RM is deployed on the side of the application as a middleware layer in the form of a jar package.

2. Performance level: In terms of two-phase commit, traditional 2PC does not release locks on transactional resources until Phase 2 is completed, no matter whether the decision in the second phase is commit or rollbcak. Seata's approach is to submit the local transaction in Phase 1, which saves the lock-holding time in Phase 2 and improves overall efficiency.

Real-time effect feedback

1. The following questions that belong to the XA scheme are _____.

A requires the local database to support the XA protocol

B performance is poor

C single point of failure problem

D above are correct

2. Seata mainly solves ____.

A XA mode

B TCC mode

C AT mode

D one-stop solution for distributed things 

Seata provides XA mode to realize distributed transactions_Business Description

Business description

This example implements distributed transactions through Seata middleware, simulating the transfer transaction process of two accounts. The two accounts are in two different banks (Zhang San is in bank1 and Li Si is in bank2), and bank1 and bank2 are two microservices. During the transaction, Zhang San transfers the specified amount to Li Si. The above transaction steps, either succeed together or fail together, must be an integral transaction. 

 

engineering environment

 

create database

bank1 library, including Zhang San's account 

CREATE DATABASE /*!32312 IF NOT
EXISTS*/`bank1` /*!40100 DEFAULT CHARACTER
SET utf8 */;
USE `bank1`;
/*Table structure for table `account_info`
*/
DROP TABLE IF EXISTS `account_info`;
CREATE TABLE `account_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `account_name` varchar(100) COLLATE
utf8_bin DEFAULT NULL COMMENT '户主姓名',
  `account_no` varchar(100) COLLATE utf8_bin
DEFAULT NULL COMMENT '银行卡号',
  `account_password` varchar(100) COLLATE
utf8_bin DEFAULT NULL COMMENT '帐户密码',
  `account_balance` double DEFAULT NULL
COMMENT '帐户余额',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT
CHARSET=utf8 COLLATE=utf8_bin
ROW_FORMAT=DYNAMIC;
/*Data for the table `account_info` */
insert  into
`account_info`(`id`,`account_name`,`account_
no`,`account_password`,`account_balance`)
values (2,'张三','1',NULL,1000);

bank2 library, including Lisi account

CREATE DATABASE /*!32312 IF NOT
EXISTS*/`bank2` /*!40100 DEFAULT CHARACTER
SET utf8 */;
USE `bank2`;
/*Table structure for table `account_info`
*/
DROP TABLE IF EXISTS `account_info`;
CREATE TABLE `account_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `account_name` varchar(100) COLLATE
utf8_bin DEFAULT NULL COMMENT '户主姓名',  
`account_no` varchar(100) COLLATE utf8_bin
DEFAULT NULL COMMENT '银行卡号',
  `account_password` varchar(100) COLLATE
utf8_bin DEFAULT NULL COMMENT '帐户密码',
  `account_balance` double DEFAULT NULL
COMMENT '帐户余额',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT
CHARSET=utf8 COLLATE=utf8_bin
ROW_FORMAT=DYNAMIC;
/*Data for the table `account_info` */
insert  into
`account_info`(`id`,`account_name`,`account_
no`,`account_password`,`account_balance`)
values (3,'李四的账户','2',NULL,0);

Seata provides XA mode to realize distributed transaction_Download and start Seata service

download seata server

Download address: https://github.com/seata/seata/releases 

 

Unzip and start 

tar -zxvf seata-server-1.4.2.tar.gz -C /usr/local/
#后台运行
nohup sh seata-server.sh -p 9999 -h 192.168.66.100 -m file &> seata.log &

Note: Among them, 9999 is the service port number; file is the startup mode, which means that the seata service will store information in the form of files.

test 

View startup log

cat seata.log

 

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/131869146