TX-LCN官网: http://www.txlcn.org/zh-cn/docs/start.html
官网中有源码以及demo的git地址,同时也给了清晰的文档说明,有需要去官网获取就可以了.
本文只结合自己springcloud项目+lcn框架实现分布式事务
1.TM的配置
pom.xml文件
<?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>com.lcn</groupId> <artifactId>txlcn-tm</artifactId> <version>1.0</version> <name>txlcn-tm</name> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tm</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: txlcn-tm datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/demo?nullCatalogMeansCurrent=true&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false username: root password: root redis: host: localhost port: 6379 server: port: 7970 tx-lcn: manager: host: 127.0.0.1
启动类:
@SpringBootApplication @ComponentScan(basePackages = {"com.*"}) @EnableTransactionManagerServer public class TxlcnTmApplication { public static void main(String[] args) { SpringApplication.run(TxlcnTmApplication.class, args); } }
启动tm服务,输入网址: localhost:7970,登录TxManager的系统平台
扫描二维码关注公众号,回复:
6413260 查看本文章
2.TC配置
需要分布式事务控制的服务,使其成为一个TxClient客户端
pom.xml
//引入jar包依赖 <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tc</artifactId> </dependency> <dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-txmsg-netty</artifactId> </dependency>
application.xml
//在配置文件中指定8070端口,8070是tm默认开启的和tc通信的端口 tx-lcn: client: manager-address: localhost:8070
启动类: 注解@EnableDistributedTransaction
@SpringBootApplication @ComponentScan(basePackages = {"com.*"}) @EnableEurekaClient @EnableFeignClients @EnableDistributedTransaction public class TxdemoServerApplication { public static void main(String[] args) { SpringApplication.run(TxdemoServerApplication.class, args); } }
@LcnTransaction注解控制分布式事务
@LcnTransaction public void demoA(){ demoB.add(user); throw new RuntimeException(); demoC.deleteById(id); }
public class demoB{
@LcnTransaction
public void add(User user){
demoBMapper.insert(user);
}
}
public class demoC{
@LcnTransaction
public void deleteById(Long id){
demoCMapper.deleteById(id);
}
}
注意:(1)lcn框架会创建一个事务组,在执行完之前不会提交事务,最后根据结果进行提交或者回滚,但是即使回滚数据库中也会占用id
(2)如果同时加入了@Transactional开启本地事务,需要配置好优先级顺序,否则会被本地事务提交,测试不加本地事务是ok的,本地也会被lcn管理
---同事做的,主要是为了自己记忆