微服务绕不开分布式事务。本章展示简单分布式案例。
背景:使用spring boot 1.5.17.RELEASE + shardingsphere 5.0.0-alpha + bitronix + druid 1.2.4 + mysql8
1. mysql 用户需要具备相应权限(允许执行 XA RECOVER语句),这里为用户分配所有权限
GRANT ALL ON *.* TO 'root'@'%'
参考(https://blog.csdn.net/mxskymx/article/details/88765072)
2. pom.xml
<properties>
<shardingsphere.version>5.0.0-alpha</shardingsphere.version>
<spring.boot.version>1.5.17.RELEASE</spring.boot.version>
<commons-dbcp2.version>2.2.0</commons-dbcp2.version>
<commons-pool.version>1.6</commons-pool.version>
<druid.version>1.2.4</druid.version>
<hutool.version>5.0.7</hutool.version>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<mysql.connector.version>8.0.17</mysql.connector.version>
</properties>
<dependencies>
<dependency>
<groupId>chuck-spring-cloud-common-entity</groupId>
<artifactId>chuck-spring-cloud-common-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>chuck-spring-cloud-comm-util</groupId>
<artifactId>chuck-spring-cloud-comm-util</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<!-- sql 解析 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-sql-parser-mysql</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<!-- 使用XA 事务时,需要引⼊此模块 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-xa-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<!-- 使用BASE事务时,需要引入此模块 <dependency> <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-base-seata-at</artifactId> <version>${shardingsphere.version}</version>
</dependency> -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-transaction-xa-bitronix</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.btm</groupId>
<artifactId>btm</artifa