项目地址 :https://github.com/heng1234/springcl-lcn-demo
官网文档地址:https://www.txlcn.org/zh-cn/docs/start.html
创建数据表
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;
idea新建一个maven lcn项目
在刚刚建立好的项目右键new module建立springboot项目 tx-manager
pom.xml加入
com.codingapi.txlcn
txlcn-tm
5.0.2.RELEASE
启动类
加上注解
@EnableTransactionManagerServer
application.properties
#应用名称
spring.application.name=tx-manager
server.port=7970
#DataSource配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#redis 配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
#注册中心地址
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
# 注册名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
# 设置注册到服务的为ip
eureka.instance.prefer-ip-address=true
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false
mybatis.configuration.use-generated-keys=true
# tx-manager ip(client请求ip)
#tx-lcn.manager.host=127.0.0.1
# client 请求端口
#tx-lcn.manager.port=8070
tx-lcn.client.manager-address=127.0.0.1:8070
# 心跳检测时间 单位:ms
tx-lcn.manager.heart-time=12000
# 事务执行总时间
tx-lcn.manager.dtx-time=10000
# 参数延迟删除时间单位ms
tx-lcn.message.netty.attr-delay-time=10000
tx-lcn.manager.concurrent-level=128
# 开启日志
tx-lcn.logger.enabled=true
logging.level.com.codingapi=debug
tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name}
tx-lcn.logger.jdbc-url=${spring.datasource.url}
tx-lcn.logger.username=${spring.datasource.username}
tx-lcn.logger.password=${spring.datasource.password}
tx-lcn.manager.ex-url=/provider/email-to/[email protected]
tx-lcn.manager.ex-url-enabled=true
spring.jpa.hibernate.ddl-auto=update
tx-lcn.manager.admin-key=123456
新建2个client client_one client_two
pom.xml加入jar
com.codingapi.txlcn
txlcn-tc
5.0.2.RELEASE
com.codingapi.txlcn
txlcn-txmsg-netty
5.0.2.RELEASE
启动类加上注解
@EnableDistributedTransaction
然后再添加2个增加方法
client_one
/**
* 新增
*/
@Transactional
@LcnTransaction
public Integer saveUser(User user){
//user.setId(9L);
user.setEmail("[email protected]");
user.setPassword("lcn123456");
return userLcnMapper.insert(user);
}
client_two
@Transactional
@LcnTransaction
public Integer saveUser(){
User user = new User();
user.setEmail("[email protected]");
user.setAge(17);
user.setName("lcn");
int res = 1/0;
return userMapper.insert(user);
}
feign
pom里面也加入jar
com.codingapi.txlcn
txlcn-tc
5.0.2.RELEASE
com.codingapi.txlcn
txlcn-txmsg-netty
5.0.2.RELEASE
启动类加上注解
@EnableDistributedTransaction
代码
@Autowired
private Client2 client2;
@Autowired
private Client3 client3;
@RequestMapping("testLcn")
@LcnTransaction
public String testLcn(){
Map map = new HashMap<>();
Integer res = (Integer) client2.getMethod("tuser/saveUser",map);
Integer res1 = (Integer) client3.getMethod("userTwo/insertUser",map);
return res+"-"+res1;
}
访问controller地址http://localhost:8765/feign/testLcn
浏览器出现
表示回滚成功
tx-manager访问地址
http://localhost:7970/admin/index.html#/builder
具体看项目地址 :https://github.com/heng1234/springcl-lcn-demo