Welcome to the public number [sharedCode] committed to mainstream middleware source code analysis, personal website: www.shared-code.com/

preface

Because the project needs to use distributed transactions, in order to solve this problem, we investigated some distributed transaction frameworks, hoping that developers can solve the invocation problem between internal systems with a single annotation. Seata was chosen

Official documents:

Seata. IO/useful – cn/docs /…

Github.com/seata/seata…

The installation

Official Installation Guide

Seata. IO/useful – cn/docs /…

Seata has three roles: TC, TM, and RM. TC (Server) is deployed as an independent Server, and TM and RM (Client) are integrated by service systems.

Resource Catalog Introduction

Click to view

  • client

Save client SQL scripts and configure parameters

  • config-center

Config. TXT (including server and client, formerly known as nacos-config. TXT) is a general parameter file

  • server

Server-side database scripts and container configurations

Matters needing attention

  • seata-spring-boot-starter
The built-in GlobalTransactionScanner automatic initialization function. If this function is implemented externally, see SeataAutoConfiguration to ensure that automatic proxy for data sources is enabled by default. You can configure seata. Enable-auto-data-source-proxy: false Disables seataCopy the code
  • spring-cloud-alibaba-seata

2.1.0 embedded seata-all 0.7.1, 2.1.1 embedded Seata-All 0.9.0, 2.2.0 embedded Seata-spring-boot-starter 1.0.0

Starter solutions compatible with 2.1.0 and 2.1.1: @ SpringBootApplication annotations within exclude off spring - the cloud - alibaba - within seata com, alibaba. Cloud. Seata. GlobalTransactionAutoConfigurationCopy the code

Start the Server

There are two storage modes on the Server side (Store. mode) : File and DB (Redis and raft will be introduced later). The file mode does not need to be changed. Note: The file mode is the single-machine mode, and the global transaction session information is read and written in the memory and the local file root.data is persisted, which provides high performance. Db mode is high availability mode, global transaction session information is shared through DB, corresponding performance is poor.

Step 1: Start the package

  • Click on the download
  • Official nail group (group id: 23171167,1 group 5000 full, 2 groups), qq group (group id: 254657148) group file sharing download

Step 2: Create a table

Global transaction session information consists of three parts: global transaction > branch transaction > global lock, corresponding to global_table, branch_table, and lock_table

Step 3: Modify store.mode

Seata –>resources–>file.conf –> store. Mode =”db”

Step 4: Modify the database connection

Start package: seata–>conf–>file.conf, modify properties of store.db. Source: root directory –>seata-server–>resources–>file.conf, modify store.db related properties.

Step 5: Start

  • Source launch: Execute the main method of server.java
  • Command start: seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test
-h: IP registered with the registry -p: Server RPC listening port -m: storage mode of global transaction session information, file and DB, with priority for reading startup parameters -n: Server node. If there are multiple servers, separate the nodes to generate transactionids of different ranges to avoid conflicts. -e: For the multi-environment configuration, see http://seata.io/en-us/docs/ops/multi-configuration-isolation.htmlCopy the code
  • Click to see the Docker deployment

Note: It is recommended to allocate 2 GB of heap memory and 1 GB of out-of-heap memory

Business system integration Client

Step 1: Add seATA dependencies (single option recommended)

  • Rely on seata – all
  • It relies on Seata-spring-boot-starter, supports yML and properties configurations (which can be deleted. Conf), and relies on Seata-all internally
  • Relying on Spring-Cloud-Alibaba-SeATA, seATA is integrated internally and XID passing is implemented

Step 2: undo_log create a table and configure parameters

  • View parameter configuration

Step 3: Data source agent (automatic and manual configuration is not supported)

  • Seata supports automatic proxy data sources starting with version 0.9.0
1.1.0: seata - all cancel the configuration properties, instead annotation @ EnableAutoDataSourceProxy open, and you can choose the JDK proxy or additional proxy 1.0.0: Client. Support. Spring. The datasource. Autoproxy = true 0.9.0: support. Spring. The datasource. Autoproxy = trueCopy the code
  • For manual configuration, see the following example
@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
	return new DataSourceProxy(druidDataSource);
}
Copy the code

Step 4: Initialize GlobalTransactionScanner

  • manual
@Bean
 public GlobalTransactionScanner globalTransactionScanner(a) {
   String applicationName = this.applicationContext.getEnvironment().getProperty("spring.application.name");
           String txServiceGroup = this.seataProperties.getTxServiceGroup();
           if (StringUtils.isEmpty(txServiceGroup)) {
               txServiceGroup = applicationName + "-fescar-service-group";
               this.seataProperties.setTxServiceGroup(txServiceGroup);
           }
   
           return new GlobalTransactionScanner(applicationName, txServiceGroup);
  }
Copy the code
  • Automatically introduce jars such as Seata-spring-boot-starter and Spring-Cloud-Alibaba-Seata

Step 5: Implement xID delivery across services

  • Manually refer to the various RPC implementation modules in the integration folder of the source code
  • Automated springCloud users can introduce Spring-Cloud-Alibaba-seata with xID passing already implemented internally