本文借鉴于:https://blog.csdn.net/qq_39057639/article/details/100436496
这篇博文。 原本想集成到项目中, 但因为集成 springboot版本 2.2.2.RELEASE 和springcloud
Hoxton.RELEASE
版本失败,再加上还需要 安装Consul 去使用,所以放弃了。 随后自己又集成了一个 eureka 的版本 ,无需 安装Consul
的一个版本。
https://download.csdn.net/download/fangchao2011/12100373
eureka-server: eureka 微服务注册中心。
txlcn-tm :LCN 管理端,用于管理服务事务。
test_a: 微服务 A
test_b: 微服务 B
调用 A 服务controller ,在impl实现层 调用 B 服务的接口保存数据,抛异常,然后再调用本服务保存数据。检测是否B服务是否有回滚。
http://localhost:8080/tx/pay?money=100&user=xiaoming
已经进入我们预期的异常!
test_a :
test_b:
然后看数据库B表(值没有改变,说明事务已经回滚成功!):
test_b_tx
lcn-demo(父节点):pom.xml
父节点的包主要做版本控制
<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.0modelVersion>
<groupId>org.txlcn.demogroupId>
<artifactId>lcn-demoartifactId>
<version>1.0.0version>
<packaging>pompackaging>
<name>lcn-demoname>
<modules>
<module>test_amodule>
<module>test_bmodule>
<module>txlcn-tmmodule>
modules>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<codingapi.txlcn.version>5.0.2.RELEASEcodingapi.txlcn.version>
<springcloud.version>Greenwich.RELEASEspringcloud.version>
<lombok.version>1.18.0lombok.version>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.2.RELEASEversion>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${springcloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tcartifactId>
<version>${codingapi.txlcn.version}version>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tmartifactId>
<version>${codingapi.txlcn.version}version>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-txmsg-nettyartifactId>
<version>${codingapi.txlcn.version}version>
dependency>
dependencies>
dependencyManagement>
project>
启动类加上 事务注解
@EnableTransactionManagerServer
application.properties:
spring.application.name=tx-manager
server.port=7970
# JDBC 数据库配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tx-manager?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#eureka 地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
eureka.instance.prefer-ip-address=true
# 数据库方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# 第一次运行可以设置为: create, 为TM创建持久化数据库表
spring.jpa.hibernate.ddl-auto=update
# TM监听IP. 默认为 127.0.0.1
tx-lcn.manager.host=127.0.0.1
# TM监听Socket端口. 默认为 ${server.port} - 100
tx-lcn.manager.port=8070
# 心跳检测时间(ms). 默认为 300000
tx-lcn.manager.heart-time=300000
# 分布式事务执行总时间(ms). 默认为36000
tx-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后台登陆密码,默认值为codingapi
tx-lcn.manager.admin-key=jack
# 分布式事务锁超时时间 默认为-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-url
tx-lcn.manager.ex-url-enabled=false
# 事务异常通知(任何http协议地址。未指定协议时,为TM提供内置功能接口)。默认是邮件通知
tx-lcn.manager.ex-url=/provider/email-to/***@**.com
# 开启日志,默认为false
tx-lcn.logger.enabled=true
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}
# redis 的设置信息. 线上请用Redis Cluster
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=7000
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=20000
pom.xml
关键包:
com.codingapi.txlcn
txlcn-tm
完整包:
<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>
<groupId>org.txlcn.demogroupId>
<artifactId>lcn-demoartifactId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>txlcn-tmartifactId>
<name>txlcn-tmname>
<dependencies>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tmartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
dependencies>
project>
启动类加上事务注解配置
@EnableDistributedTransaction
application.yml:
server:
port: 8080
spring:
application:
name: test-a
datasource:
url: jdbc:mysql://localhost:3306/txlcn-demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
# MySQL 8.x: com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#eureka 地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
pom.xml
关键包:
com.codingapi.txlcn
txlcn-tc
com.codingapi.txlcn
txlcn-txmsg-netty
完整包:
<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>
<groupId>org.txlcn.demogroupId>
<artifactId>lcn-demoartifactId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>test_aartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tcartifactId>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-txmsg-nettyartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.18version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependencies>
project>
application.yml:
server:
port: 8081
spring:
application:
name: test-b
datasource:
url: jdbc:mysql://localhost:3306/txlcn-demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
# MySQL 8.x: com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#eureka 地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
pom.xml
关键包:
com.codingapi.txlcn
txlcn-tc
com.codingapi.txlcn
txlcn-txmsg-netty
完整包:
<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>
<groupId>org.txlcn.demogroupId>
<artifactId>lcn-demoartifactId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>test_bartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-tcartifactId>
dependency>
<dependency>
<groupId>com.codingapi.txlcngroupId>
<artifactId>txlcn-txmsg-nettyartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.18version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependencies>
project>