tx-lcn分布式事务项目貌似不再流行了,稍稍记录一下
服务端 pom文件添加依赖注意: 前置条件,搭建好mysql数据库、redis数据库
因为是spring cloud项目 可以自己选择 分布式类型 我这里用的nacos
注意 这里只能使用applcation.properties格式
启动类添加注解org.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntime org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-enginecom.codingapi.txlcn txlcn-tm5.0.2.RELEASE
添加 EnableTransactionManagerServer 注解
@SpringBootApplication
@EnableTransactionManagerServer
public class DemoTxApplication {
public static void main(String[] args) {
SpringApplication.run(DemoTxApplication.class, args);
System.out.println("tx 启动完成======》 ");
}
}
配置properties配置文件
这里只能使用 properties 格式
# 应用名称 spring.application.name=demo-tx # 应用服务 WEB 访问端口 server.port=7970 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:12005/tx-manager?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=abc123456 spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=abc123456 spring.redis.database=10 tx-lcn.manager.admin-key=abc123456 tx-lcn.manager.host=127.0.0.1 # 开放端口提供给别的服务连接 tx-lcn.manager.port=8070 # 心跳检测时间(ms) tx-lcn.manager.heart-time=15000 # 分布式事务执行总时间 tx-lcn.manager.dtx-time=30000 #参数延迟删除时间单位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 # mybatis 的配置 这里我没设置 请自行添加情况添加 #mybatis.conf
客户端 pom依赖
注意 cloud跟boot版本对应,不然启动会报错误,建议创建项目使用阿里云连接( https://start.aliyun.com/ )创建
启动类添加注解org.projectlombok lombokorg.springframework.boot spring-boot-starter-webcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloud spring-cloud-starter-openfeignmysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-enginecom.codingapi.txlcn txlcn-tc5.0.2.RELEASE com.codingapi.txlcn txlcn-txmsg-netty5.0.2.RELEASE com.baomidou mybatis-plus-boot-starter3.5.2 org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-dependencies${spring-boot.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies${spring-cloud-alibaba.version} pom import
添加 EnableDistributedTransaction 注解
@SpringBootApplication
@MapperScan("com.example.serviceb.mapper")
@EnableDistributedTransaction
public class DemoServicebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoServicebApplication.class, args);
System.out.println("service 启动完成=========》");
}
}
yml配置文
spring:
application:
name: demo-serviceb
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: http://127.0.0.1:8848
namespace: public
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
name: defaultDataSource
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: 123456
server:
jetty:
threads:
acceptors: 4
selectors: 8
port: 8002
servlet:
context-path: /
tx-lcn:
client:
# 输入服务端设置的地址
manager-address: 127.0.0.1:8070
feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 300000
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml



