1 :官网地址
http://www.txlcn.org/zh-cn/
部署tx-manager
新建SpringBoot项目
添加pom.xml依赖关系
? XML版本=' 1.0 '编码=' utf-8 '? project xmlns=' http://maven.Apache.org/POM/4.0.0 ' xmlns : xsi=' http://www.w3.org/2001/XML方案ins ttins maven-4.0.0.xsd ' model版本4.0.0/modelversionparentgroupidors groupidartifactidspring-boot-starter-parenend --- lookupparentfromrepository---/parentgroupidcom.manager/groupidartifactidtc-manager/artifactidversion0.0.1- namedescriptiondemoprojectforspringboot/descriptionpropertiesjava.1.8/Java.version spring-cloud.versionfinching propertiesdependenciesdependencygroupidredis.clients/groupidartifactidjedis 从属关系组组组织-云启动器- Netflix-eureka-client/artifact id/dependencydependencygroupidorg.spring fring groupidartifactidspring-boot-starter-test/artifactidscopetest/scope/从属关系模型groupidcom.coding API.to ding artifactidversion5.0.2. release/version/dependency/dependenciesdependencymanagementdependenciesdependencygroupidorg groupidartifactidspring-cloud-dependencymancies/artifactidverver versiontypepom/typescopepom ependencymanagementbuildpluginsplugingroupidorg.spring framework.boot/groupidartifactidsprinion
in></plugins></build></project>其中
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId></dependency>依赖用来连接redis客户端工具。如果没有,则无法正常使用redis。
修改application.properties配置文件
server.port=7970spring.application.name=TransactionManager
eureka.client.service-url.defaultZone=http://admin:123456@localhost:9000/eureka
eureka.instance.appname=transaction-manager
eureka.instance.prefer-ip-address=true
#JDBC 数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?qcdzjy=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#数据库方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
#spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=update
tx-lcn.manager.host=127.0.0.1
TM监听Socket端口. 默认为 ${server.port} - 100tx-lcn.manager.port=8070
心跳检测时间(ms). 默认为 300000tx-lcn.manager.heart-time=300000
分布式事务执行总时间(ms). 默认为36000tx-lcn.manager.dtx-time=8000
参数延迟删除时间单位ms 默认为dtx-time值tx-lcn.message.netty.attr-delay-time=${tx-lcn.manager.dtx-time}
事务处理并发等级. 默认为机器逻辑核心数5倍tx-lcn.manager.concurrent-level=160
TM后台登陆密码,默认值为codingapitx-lcn.manager.admin-key=codingapi
分布式事务锁超时时间 默认为-1,当-1时会用tx-lcn.manager.dtx-time的时间tx-lcn.manager.dtx-lock-time=${tx-lcn.manager.dtx-time}
雪花算法的sequence位长度,默认为12位.tx-lcn.manager.seq-len=12
异常回调开关。开启时请制定ex-urltx-lcn.manager.ex-url-enabled=false
事务异常通知(任何http协议地址。未指定协议时,为TM提供内置功能接口)。默认是邮件通知tx-lcn.manager.ex-url=/provider/email-to/*@.com
开启日志,默认为falsetx-lcn.logger.driver-class-name=KaTeX parse error: Expected 'EOF', got '&' at position 61: …logger.jdbc-url&̲#61;{spring.datasource.url}
tx-lcn.logger.username=KaTeX parse error: Expected 'EOF', got '&' at position 52: …logger.password&̲#61;{spring.datasource.password}
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
启动文件添加注解
package com.unicom.manager;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import com.codingapi.txlcn.tm.config.EnableTransactionManagerServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableTransactionManagerServer
public class DemoApplication {
}
浏览器访问:http://localhost:7970/admin/index.html 使用配置文件的密码登录即可(默认密码是codingapi)
3:配置tx-client项目
新建两个springcloud项目,并注册到eureka中
pom.xml添加tx-client的依赖
<dependency> <groupId>com.codingapi.txlcn</groupId> <artifactId>txlcn-tc</artifactId> <version>5.0.2.RELEASE</version></dependency><dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-txmsg-netty</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
配置文件添加tx-manager的注册地址
tx-lcn: client: manager-address: 127.0.0.1:8070修改配置启动类的注解
package com.wtmlzt.EurekaProvider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
@EnableEurekaClient
@SpringBootApplication
@EnableDistributedTransaction
@EnableTransactionManagement
public class EurekaProvider01 {
}
在服务消费方添加事务管理注解
@RequestMapping("/saveOrder")@LcnTransaction(propagation = DTXPropagation.SUPPORTS)public Integer saveOrder(@RequestBody OrderVo vo){logger.info("EurekaConsumer>>saveOrder>>vo:" + vo.toString()); OrderDao.saveOrderHistory(vo)int i = orderFeignService.saveOrder(vo); if(i==0){ throws new Exception(); }}同时在服务提供方也要添加事务管理的注解
@LcnTransactionpublic Integer saveOrder(OrderVo vo) {System.out.println("provider>>"+ vo.toString()); return orderDao.saveOrder(vo);}
它虽然也加了@LcnTransaction这个注解,但由于这里是调用其他服务的,因此这个注解中不能配置其他属性。
这个项目中dao层和Model层相关的代码如tc-client-1完全相同,小伙伴们按照自己喜欢的方式写就可以了。
到这里就大功告成了。小伙伴们可以自己修改一个数据库结构让其中一个数据插入不成功,看看成果。
注意:
1、数据库表结构必须为innoDB
2、在启动类上一定要加入@EnableDistributedTransaction
3、当调用其他服务时@LcnTransaction注解不能加入其他参数
4、被调用的服务@LcnTransaction注解一定要加入propagation = DTXPropagation.SUPPORTS这个配置