seata distributed transaction of spring cloud Alibaba
The construction of seata distributed transactions is scattered on the Internet, and there are always problems. Today, a chapter on building cases is published. This chapter is based on springcloud + Nacos + seate1 2 + MySQL build
1. First download seate1 Version 2 code and seate server http://seata.io/zh-cn/blog/download.html Try to be consistent with the version guided in this article, because each version will have its own differences
2. Download seate-1.2 Zip unzip, open through idea It mainly depends on the configuration in script
3. Unzip the seat server, enter the conf directory, and modify the file Conf and registry Conf these two files
- registry.conf: Specifies the registry. I use nacos here. Configuration is required in both registry and config: as shown in the figure: (I deleted many useless configurations here)
registry { # file ,nacos ,eureka,redis,zk,consul,etcd3,sofa type = "nacos" nacos { application = "seata-server" serverAddr = "http://127.0.0.1:8848" namespace = "" cluster = "default" username = "nacos" password = "nacos" } file { name = "file.conf" } } config { # file,nacos ,apollo,zk,consul,etcd3 type = "nacos" nacos { serverAddr = "http://127.0.0.1:8848" namespace = "c3a1098e-196f-462e-99e1-6697708a0ff3" group = "SEATA_GROUP" username = "nacos" password = "nacos" } file { name = "file.conf" } }
Note the configuration of namespace = "c3a1098e-196f-462e-99e1-6697708a0ff3". This configuration refers to the parameters for configuring the seat customer service side in nacos. If there is no configuration, it can not be written. If it is written, it needs to correspond to the following configuration, and the corresponding ones are indicated in italics
4. Modify file Conf file
service { vgroupMapping.my_test_tx_group = "default" default.grouplist = "127.0.0.1:8091" #degrade, current not support enableDegrade = false #disable seata disableGlobalTransaction = false } store { mode = "db" db { datasource = "druid" dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://192.168.1.1:3306/seata" user = "root" password = "123123" minConn = 5 maxConn = 30 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } } client { rm { asyncCommitBufferLimit = 10000 lock { retryInterval = 10 retryTimes = 30 retryPolicyBranchRollbackOnConflict = true } reportRetryCount = 5 tableMetaCheckEnable = false reportSuccessEnable = false sagaBranchRegisterEnable = false } tm { commitRetryCount = 5 rollbackRetryCount = 5 } undo { dataValidation = true logSerialization = "jackson" logTable = "undo_log" } log { exceptionRate = 100 } }
Here, just pay attention to the following points:
- vgroupMapping. my_ test_ tx_ My in group = "default"_ test_ tx_ Group will correspond to the following parameters. I will display them in bold. Vgroupmapping should not be written as vgroup_mapping
- For database connection, the data source uses seata first
- global_table,branch_table,lock_ The three tables are table names. Three tables will be created later and stored in the seata data source for seata records
5. Execute sql
- First add the data source seata in mysql, and then enter script / server / db / MySQL sql, there are three tables. Execute sql in the database. The three tables here correspond to the previous server configuration
- After execution, enter script / client / at / db / MySQL SQL, there is a table undo_log, which data source uses distributed transactions, add the table to that data source.
After that, the server is set up. You can start bin / Seata server Bat try
6: Add configuration parameters to nacos
Enter idea, open seata-1.2.0 just downloaded, and enter script \ config center \ config Txt file, there are many configurations here. I'll post the key contents
service.vgroupMapping.my_test_tx_group=default service.default.grouplist=127.0.0.1:8091 store.mode=db store.db.datasource=druid store.db.dbType=mysql store.db.driverClassName=com.mysql.jdbc.Driver store.db.url=jdbc:mysql://192.168.1.1:3306/seata?useUnicode=true store.db.user=root store.db.password=123123 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
It mainly modifies the mysql connection address and password. And what I said earlier_ test_ tx_ Group. This must be the same as the server configuration
After modification, open script \ config center \ Nacos \ Nacos config SH, mainly modify the following contents
Note that if you set the namespace in the front, you need to add it here. If you don't fill in the front, it can be ignored
7. Modification completed. Start nacos first, then run script \ config center \ nacos \ nacos config Sh (open the folder and double-click it). I downloaded git here, so I can execute it directly. After execution, I can see it in the configuration list in nacos
8. Add spring configuration (for those services that use distributed transactions, add configuration on those services):
Can be added to application YML can also be added to nacos. I added it directly to nacos
seata: enabled: true application-id: ${spring.application.name} tx-service-group: my_test_tx_group enable-auto-data-source-proxy: true use-jdk-proxy: false excludes-for-auto-proxying: firstClassNameForExclude,secondClassNameForExclude client: rm: async-commit-buffer-limit: 1000 report-retry-count: 5 table-meta-check-enable: false report-success-enable: false saga-branch-register-enable: false lock: retry-interval: 10 retry-times: 30 retry-policy-branch-rollback-on-conflict: true tm: commit-retry-count: 5 rollback-retry-count: 5 undo: data-validation: true log-serialization: jackson log-table: undo_log log: exceptionRate: 100 service: vgroup-mapping: my_test_tx_group: default grouplist: default: 127.0.0.1:8091 enable-degrade: false disable-global-transaction: false config: type: nacos nacos: serverAddr: http://localhost:8848 group: SEATA_GROUP namespace: c3a1098e-196f-462e-99e1-6697708a0ff3 userName: nacos password: nacos registry: type: nacos nacos: application: seata-server serverAddr: http://localhost:8848 namespace: cluster: default userName: nacos password: nacos
- You should also pay attention to the aforementioned namespace: c3a1098e-196f-462e-99e1-6697708a0ff3, which can be omitted if there is no one
- my_test_tx_group should correspond to the previous one
9. Add maven dependency
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.2.2.RELEASE</version> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
Pay attention to the version here. I use the 1.2 version. I use the 1.2.0 jar package for the introduction. If the version is wrong, there will be problems!
10: The last step: add spring configuration, as mentioned just now. When distributed transactions are used, add them there. In bootstrap Add in YML
spring: cloud: alibaba: seata: tx-service-group: my_test_tx_group
Note: Here's my_test_tx_group and previous correspondence
Add @ GlobalTransactional where distributed transactions are needed
What I said I should pay attention to are the pits I stepped on.....
OK, that's it!