随着互联化的蔓延,各种项目都逐渐向分布式服务做转换。如今微服务已经普遍存在,本地事务已经无法满足分布式的要求,由此分布式事务问题诞生。 分布式事务被称为世界性的难题,目前分布式事务存在两大理论依据:CAP定律 BASE理论。
准备工作
jdk1.8、mysql、redis、idea
1、数据库脚本
tx-manager脚本
CREATE DATABASE IF NOT EXISTS `tx-manager` DEFAULT CHARACTER SET = utf8;
Use `tx-manager`;
CREATE TABLE IF NOT EXISTS `t_tx_exception`(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`transaction_state` tinyint(4) NULL DEFAULT NULL,
`registrar` tinyint(4) NULL DEFAULT NULL,
`remark` varchar(4096) NULL DEFAULT NULL,
`ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解决 1已解决',
`create_time` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
tx_logger脚本
CREATE DATABASE IF NOT EXISTS `tx_logger` DEFAULT CHARACTER SET = utf8;
Use `tx_logger`;
CREATE TABLE IF NOT EXISTS `t_logger`(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`group_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`unit_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`tag` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`content` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`create_time` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`app_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 19 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
2、相关pom文件
父pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>tx-lcn-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>txlcn-tm</module>
<module>txlcn-client-demo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Txlcn-tm的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tx-lcn-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>txlcn-tm</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
3、配置文件
spring.application.name=TX-Manager
server.port=7970
######
# ## 你可以在 https://txlcn.org/zh-cn/docs/setting/manager.html 看到所有配置
### 官网好像挂好久啦 哈哈 — —
spring.profiles.active=dev
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.180:3306/tx-manager?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.username=【你的数据库账号】
spring.datasource.password=【你的数据库密码】
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
tx-lcn.logger.enabled=true
tx-lcn.logger.driver-class-name=com.mysql.cj.jdbc.Driver
tx-lcn.logger.jdbc-url=jdbc:mysql://192.168.1.180:3306/tx_logger?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
tx-lcn.logger.username=【你的数据库账号】
tx-lcn.logger.password=【你的数据库密码】
# redis信息
spring.redis.host=【redis地址】
spring.redis.port=6379
spring.redis.password=123456
# TM后台登录密码,默认值 codingapi
tx-lcn.manager.admin-key=shuhai@123
tx-lcn.manager.host=127.0.0.1
tx-lcn.manager.port=8070
# 分布式事务执行总时间(ms). 默认为8000
tx-lcn.manager.dtx-time=15000
4、启动类
@SpringBootApplication
@EnableTransactionManagerServer
public class TransactionManagerApp {
public static void main(String[] args) {
SpringApplication.run(TransactionManagerApp.class, args);
}
}
5、打开TM的后台登录
地址:http://localhost:7970/
**密码:**shuhai@123
代码开源
代码已经托管到我的GitHub上啦希望能帮助到有需要的小伙伴啦
GitHub:https://github.com/JarvanBest/tx-lcn-demo