Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Guide to the use of open source distributed transaction middleware Seata

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Introduction

Seata is Alibaba's open source distributed transaction middleware, a distributed transaction solution with high performance and easy-to-use micro-service architecture.

Original intention

No intrusion to the business: that is, to reduce the intrusion to the business caused by the distributed transaction problems caused by the microservice in the technical architecture.

High performance: reduce the performance consumption caused by distributed transaction solutions

Distributed transaction definition

A distributed transaction is a global transaction, which consists of a batch of branch transactions, which are usually only local transactions.

Design

There are two distributed transaction implementation schemes in Seata, AT and TCC.

AT

AT pattern is based on the evolution of XA transactions, the core is no intrusion to the business, is an improved two-phase commit, which needs database support.

Basic components:

Transaction coordinator (TC): transaction coordinator, which maintains the running state of global transactions and is responsible for coordinating and driving the commit or rollback of global transactions.

Transaction Manager (TM): controls the boundaries of a global transaction, is responsible for opening a global transaction, and ultimately initiates a global commit or global rollback resolution.

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

Processing flow:

TM requires TC to start a new global transaction. TC generates a XID that represents a global transaction.

XID propagates through the call chain of microservices.

RM registers the local transaction as a branch of the corresponding global transaction from XID to TC.

TM requires TC to commit or roll back the corresponding global transaction for XID.

TC drives all branch transactions under the corresponding global transaction of XID to complete branch commit or rollback.

TCC

Seata requires each interface to implement prepare, commit, and rollback.

As with AT mode, this section intercepts all calls to the TCC interface at run time. Each time the Try interface is called, the aspect registers a branch transaction with TC before executing the original RPC call. When the request link call is completed, the TC calls back to the correct participant through the resource ID of the branch transaction to execute the Confirm or Cancel method of the corresponding TCC resource.

Preliminary operation Try: complete all business checks and reserve the necessary business resources.

Confirm operation Confirm: the business logic that is actually executed, does not do any business check, and only uses the business resources reserved during the Try phase. Therefore, as long as the Try operation is successful, the Confirm must be successful. In addition, Confirm operations need to be idempotent to ensure that a distributed transaction can and can only succeed once.

Cancel the operation Cancel: release the business resources reserved during the Try phase. Similarly, Cancel operations need to be idempotent.

Seata Server installation

1. Download the latest version of Seata Sever

Https://github.com/seata/seata/releases

two。 Extract and start Seata server

Unzip seata-server-xxx.zip

Cd distribution

Sh. / bin/seata-server.sh 8091 file

Example

Scene:

Transfer a piece of data from the proxy table in the database zeroa to the proxy table in the database zerob.

Module:

Zero-discovery-server: registry

Zero-gateway-server: service Gateway

Zero-consumer: service consumers

Zero-provider-a: service provider A

Zero-provider-b: service provider B

Architecture and version:

Spring-cloud:Finchley.BUILD-SNAPSHOT

Spring-cloud-starter-netflix-eureka-server:2.0.4.BUILD-SNAPSHOT

Spring-cloud-starter-netflix-eureka-client:2.0.4.BUILD-SNAPSHOT

Spring-cloud-starter-gateway:2.0.4.BUILD-SNAPSHOT

Spring-cloud-starter-openfeign:2.0.0.RELEASE

Spring-boot:2.0.0.RELEASE

Spring-boot-starter-data-jpa:2.0.0.RELEASE

Spring-cloud-alibaba-seata:0.9.1.BUILD-SNAPSHOT

Seata-all:0.6.1

Mysql-connector-java:8.0.11

Druid-spring-boot-starter:1.1.18

Mysql:5.7

Seata-server-0.6.1

Achieve:

Zero-gateway-server

Configure application.yml

Zero-provider-a:

Configure application.yml

File.conf

Primary configuration application name and seata server address

Vgroup_mapping.$ {spring.application.name}-fescar-service-group= "default"

Default.grouplist = "127.0.0.1pur8091"

Registry.conf

Write Entity

Write Repository

Write Service

Write code Controller

DataSource

Zero-provider-b

Configure the same zero-provider-a project and write the corresponding business logic.

An exception is thrown when the add business is handled.

Zero-consumer

Configure the same zero-provider-a project and write the corresponding business logic.

FeignClient

Feignclient

Service

Controller

test

Start Seata Server

Start Mysql and initialize (each library creates a undo_log table)

DROP SCHEMA IF EXISTS zeroa

CREATE SCHEMA zeroa

USE zeroa

CREATE TABLE `undo_ log` (

`id` bigint (20) NOT NULL AUTO_INCREMENT

`branch_ id` bigint (20) NOT NULL

`xid` varchar (100) NOT NULL

`context` varchar (128) NOT NULL

`rollback_ info` longblob NOT NULL

`log_ status` int (11) NOT NULL

`log_ created` datetime NOT NULL

`log_ modified` datetime NOT NULL

`ext` varchar (100) DEFAULT NULL

PRIMARY KEY (`id`)

UNIQUE KEY `ux_undo_ log` (`xid`, `branch_ id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

DROP SCHEMA IF EXISTS zerob

CREATE SCHEMA zerob

USE zerob

CREATE TABLE `undo_ log` (

`id` bigint (20) NOT NULL AUTO_INCREMENT

`branch_ id` bigint (20) NOT NULL

`xid` varchar (100) NOT NULL

`context` varchar (128) NOT NULL

`rollback_ info` longblob NOT NULL

`log_ status` int (11) NOT NULL

`log_ created` datetime NOT NULL

`log_ modified` datetime NOT NULL

`ext` varchar (100) DEFAULT NULL

PRIMARY KEY (`id`)

UNIQUE KEY `ux_undo_ log` (`xid`, `branch_ id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

Start

Zero-discovery-server

Zero-gateway-server

Zero-provider-a

Zero-provider-b

Zero-consumer

Add data

Normal execution of transactions

The data can be viewed in two tables (data for id=2 is deleted from library an and a piece of data is added to library b).

Perform a transaction rollback

View the database data (no change).

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report