安装TM需要依赖的中间件: JRE1.8+, Mysql5.6+, Redis3.2+(4.0的也是也可以)
如果需要手动编译源码, 还需要Git, Maven, JDK1.8+(我没有使用Git使用的cmd)
CREATE TABLE `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;
如果执行sql失败,把
`create_time` datetime(0) NULL DEFAULT NULL
修改为
`create_time` datetime NULL DEFAULT NULL
下载tx-lcn-5.0.2.RELEASE.zip
https://pan.baidu.com/s/15pfrBFs9DAVycGs-npP0GQ
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
spring.application.name=TransactionManager
server.port=7970
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
#redis主机地址
spring.redis.host=127.0.0.1
#redis主机端口
spring.redis.port=6379
mvn clean package -Dmaven.test.skip=true
txlcn-tm-5.0.2.RELEASE.jar
txlcn-tm-5.0.2.RELEASE.jar
(必须先启动redis)java -jar txlcn-tm-5.0.2.RELEASE.jar
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tcartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-txmsg-nettyartifactId>
<version>5.0.2.RELEASEversion>
dependency>
在主类上使用@EnableDistributedTransaction
@SpringBootApplication
@EnableDistributedTransaction
public class DemoAApplication {
public static void main(String[] args) {
SpringApplication.run(DemoDubboClientApplication.class, args);
}
}
@Service
public class ServiceA {
@Autowired
private ValueDao valueDao; //本地db操作
@Autowired
private ServiceB serviceB;//远程B模块业务
@LcnTransaction //分布式事务注解
@Transactional //本地事务注解
public String execute(String value) throws BusinessException {
// step1. call remote service B
String result = serviceB.rpc(value); // (1)
// step2. local store operate. DTX commit if save success, rollback if not.
valueDao.save(value); // (2)
valueDao.saveBackup(value); // (3)
return result + " > " + "ok-A";
}
}
@Service
public class ServiceB {
@Autowired
private ValueDao valueDao; //本地db操作
@LcnTransaction //分布式事务注解
@Transactional //本地事务注解
public String rpc(String value) throws BusinessException {
valueDao.save(value); // (4)
valueDao.saveBackup(value); // (5)
return "ok-B";
}
}
# 默认之配置为TM的本机默认端口
tx-lcn.client.manager-address=127.0.0.1:8070
官网:https://www.txlcn.org
github:https://github.com/codingapi/tx-lcn
留言区:https://github.com/codingapi/tx-lcn/issues/7
视频教程:https://www.bilibili.com/video/BV1cb411U7g6?from=search&seid=5411458233554449964
框架开发者bilibili账号:https://space.bilibili.com/386239614?spm_id_from=333.788.b_765f7570696e666f.2