Microservices: Seata AT distributed transactions and configuration methods (Part 1)

Table of contents

Preface Introduction:

1. Install seata-at 

-> 1.1 Look at the version first, search globally

 -> 1.2 Release Notes · alibaba/spring-cloud-alibaba Wiki

 -> 1.3 select seata-at version

 -> 1.4 After downloading, create the file according to the following figure

---> 1.4.0 First create the namespace seata in nacos  

 ---> 1.4.1 registry.conf modification

---> 1.4.2 file.conf modification

 -> 1.4.3 Modify the config.txt file

---> 1.4.4 database create seata and undo_log table

The sql of seata is as follows: 

The sql of undo_log is as follows

---> 1.4.5 seata-push.sh/.bat modification

====> windows version: use git bash to operate

 ====> linux version: ./seata-push.sh

 -> 1.5 After the configuration is complete, run seata -at 

---> 1.5.1 windows execute seata-server.bat 

---> 1.5.2 linux Execute seata-server.sh

2. The following notice: Seata-at is introduced in springcloud to realize distributed transactions

3. Context preview: The modification method of AT encountering an exception


Preface Introduction:

The advantages of Seata AT (distributed transaction solution) include:

1. High availability: Seata AT uses a distributed architecture to achieve high availability. At the same time, Seata AT also supports multiple storage and registration centers, which can be selected based on different needs.

2. High performance: Seata AT adopts efficient algorithms and optimization strategies in transaction control, which can achieve high performance.

3. Easy to integrate: Seata AT provides a wealth of client API and documentation, making it easy to integrate into existing applications.

4. Support multiple scenarios: Seata AT is applicable to multiple scenarios, including microservices, distributed transactions, data consistency, etc.

5. Open source community support: Seata AT is an open source project, and there is an active community maintaining and supporting it, and users can get sufficient technical support and community resources.

For distributed transactions, I will not write those test codes in the project, I will search a lot, and I will make up later when I have time

Here only write the installation configuration and use

Real scene testing, server deployment, etc., the basic pits have been filled in. If you encounter any problems, please point them out in the comment area

1. Install seata-at 

-> 1.1 Look at the version first, search globally

 -> 1.2  Release Notes · alibaba/spring-cloud-alibaba Wiki

Each version of Spring Cloud Alibaba and the corresponding version of each component it adapts to is shown in the following table (note that Spring Cloud Dubbo has been removed from the trunk since 2021.0.1.0 and will no longer evolve with the trunk):

2022.0.0.0-RC2

1.8.6

2.2.1

4.9.4

~

1.7.0-native-rc2

2021.0.5.0

1.8.6

2.2.0

4.9.4

~

1.6.1

2.2.10-RC1

1.8.6

2.2.0

4.9.4

~

1.6.1

2022.0.0.0-RC1

1.8.6

2.2.1-RC

4.9.4

~

1.6.1

2.2.9.RELEASE

1.8.5

2.1.0

4.9.4

~

1.5.2

2021.0.4.0

1.8.5

2.0.4

4.9.4

~

1.5.2

2.2.8.RELEASE

1.8.4

2.1.0

4.9.3

~

1.5.1

2021.0.1.0

1.8.3

1.4.2

4.9.2

~

1.4.2

2.2.7.RELEASE

1.8.1

2.0.3

4.6.1

2.7.13

1.3.0

2.2.6.RELEASE

1.8.1

1.4.2

4.4.0

2.7.8

1.3.0

2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE

1.8.0

1.4.1

4.4.0

2.7.8

1.3.0

2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE

1.8.0

1.3.3

4.4.0

2.7.8

1.3.0

2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE

1.7.1

1.2.1

4.4.0

2.7.6

1.2.0

2.2.0.RELEASE

1.7.1

1.1.4

4.4.0

2.7.4.1

1.0.0

2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE

1.7.0

1.1.4

4.4.0

2.7.3

0.9.0

2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE

1.6.3

1.1.1

4.4.0

2.7.3

0.7.1

 -> 1.3 select seata-at version

I am here seata-at:1.3.0 version

Download address (official website resources):  Release v1.3.0 seata/seata

Download address (pzy resource): Set free:  https://download.csdn.net/download/pingzhuyan/87803297 

[Need to review for a while] 

 -> 1.4 After downloading, create the file according to the following figure

---> 1.4.0 First create the namespace seata in nacos  

 ---> 1.4.1 registry.conf modification

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "172.26.2.*:8848"
    group = "SEATA_GROUP"
    namespace = "3afe4eb9-0550-4271-96c1-c005ac0ea174"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = 0
    password = ""
    cluster = "default"
    timeout = 0
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

  nacos {
    serverAddr = "172.26.2.*:8848"
    namespace = "3afe4eb9-0550-4271-96c1-c005ac0ea174"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

---> 1.4.2 file.conf modification


## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"

  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://172.26.2.*:3306/seata"
    user = "root"
    password = "root"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }

  ## redis store property
  redis {
    host = "127.0.0.1"
    port = "6379"
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    queryLimit = 100
  }

}

 -> 1.4.3 Modify the config.txt file

The most critical configuration is (the location of the giant pit): service.vgroupMapping should pay attention to this 

Other locations only need to change the db database

transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3

# 此处可以修改为自己项目的分组
#service.vgroup_mapping.my_test_tx_group=default
## 1.0之后需要换成  vgroupMapping
## 可以写多个配置,只需要相关联的group统一就行


service.vgroupMapping.seata_group=default

service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=false
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000

#此处配置file.conf一致
store.mode=db

store.publicKey=
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100

store.db.datasource=druid
store.db.dbType=mysql
#配置数据库相关信息
##store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://172.26.4.*:3306/seata?useUnicode=true
store.db.user=root
store.db.password=*3
icKeyRetrieval=true
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

store.redis.mode=single
store.redis.single.host=127.0.0.1
store.redis.single.port=6379
store.redis.maxConn=10
store.redis.minConn=1
store.redis.maxTotal=100
store.redis.database=0
store.redis.password=
store.redis.queryLimit=100
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

---> 1.4.4 database create seata and undo_log table

The sql of seata is as follows: 

/*
Navicat MySQL Data Transfer
Source Server         : mysql
Source Server Version : 80020
Source Host           : localhost:3306
Source Database       : seata
Target Server Type    : MYSQL
Target Server Version : 80020
File Encoding         : 65001
Date: 2022-07-27 18:35:46
*/
 
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for `branch_table`
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table` (
  `branch_id` bigint NOT NULL,
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint DEFAULT NULL,
  `resource_group_id` varchar(32) DEFAULT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `branch_type` varchar(8) DEFAULT NULL,
  `status` tinyint DEFAULT NULL,
  `client_id` varchar(64) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime(6) DEFAULT NULL,
  `gmt_modified` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-- ----------------------------
-- Records of branch_table
-- ----------------------------
 
-- ----------------------------
-- Table structure for `global_table`
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table` (
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint DEFAULT NULL,
  `status` tinyint NOT NULL,
  `application_id` varchar(32) DEFAULT NULL,
  `transaction_service_group` varchar(32) DEFAULT NULL,
  `transaction_name` varchar(128) DEFAULT NULL,
  `timeout` int DEFAULT NULL,
  `begin_time` bigint DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`xid`),
  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-- ----------------------------
-- Records of global_table
-- ----------------------------
 
-- ----------------------------
-- Table structure for `lock_table`
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table` (
  `row_key` varchar(128) NOT NULL,
  `xid` varchar(96) DEFAULT NULL,
  `transaction_id` bigint DEFAULT NULL,
  `branch_id` bigint NOT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `table_name` varchar(32) DEFAULT NULL,
  `pk` varchar(36) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`row_key`),
  KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
-- ----------------------------
-- Records of lock_table
-- ----------------------------

The sql of undo_log is as follows

<Create in which library where distributed transactions are used>

-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log

-- drop table `undo_log`;
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;

---> 1.4.5 seata-push.sh/.bat modification

sh nacos-config.sh -h 172.26.2.* -p 8848 -g SEATA_GROUP -t 3afe4eb9-0550-4271-96c1-c005ac0ea174 -u nacos -w nacos
-h IP address (server intranet ip can be windows local You can use the remote public network ip);

-p specifies the port number of nacos;
-g specifies the configured group, note that it is the configured group <must be SEATA_GROUP> (introduced in the afternoon);
-t specifies the namespace id, the default id created on nacos;
-u -w specifies The username and password of nacos need to be specified only when nacos registration and configuration authentication are enabled.

====> windows version: use git bash to operate

You cannot use bat to copy files, as shown in the figure below, git bash here console to operate

[Significantly tidier hehe] 

 ====> linux version: ./seata-push.sh

If you can't copy the file with sh, operate it on the linux console (as shown in the figure)

 -> 1.5 After the configuration is complete, run seata -at 

---> 1.5.1 windows execute seata-server.bat 

---> 1.5.2 linux Execute seata-server.sh

Foreground start: ./seata-server.sh

Background start: nohup ./seata-server.sh >log.out 2>1 &


At this point, all configuration of seata-at is over

2. The following notice: Seata-at is introduced in springcloud to realize distributed transactions

 Configuration file xml basic code, etc.

Portal: =>  Microservice: Seata AT springCloud integrates distributed transactions in configuration mode (Part 2)

3. Context preview: The solution to the exception encountered by AT

Part of the source code analysis of the solution to the exception (most problems can be repaired)  

Portal: =>  Microservice: Seata AT Distributed transaction configuration exception resolution (quite comprehensive) (Part 2)

Guess you like

Origin blog.csdn.net/pingzhuyan/article/details/130781186