Java Spring Cloud + TX-LCN分布式事务框架 亲测

28 篇文章 0 订阅
1 篇文章 0 订阅

Java Spring Cloud + TX-LCN分布式事务框架

1. TM服务 (统管分布式事物)

  • pom.xml 相关 Maven包
    <dependencies>
        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tm</artifactId>
	    <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
  • TM服务的 两个配置文件
  1. 环境文件 1 bootstrap.yml

server:
  port: 8391

eureka:
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 2 # 5秒钟发送一次心跳
    lease-expiration-duration-in-seconds: 4 # 10秒不发送就过期
  client:
    serviceUrl:
      defaultZone: http://localhost:9000/eureka/

spring:
  application:
    name: txlcn-tm-service

  1. 环境文件 2 application.properties

# Mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&serverTimezone=PRC&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
# TxManager Host Ip
tx-lcn.manager.host=127.0.0.1
# TxClient连接请求端口
tx-lcn.manager.port=8392
# 心跳检测时间(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
# TM后台登陆密码,默认值为 codingapi
tx-lcn.manager.admin-key=123456
logging.level.com.codingapi=debug
# 开启日志,默认为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
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=

  • 参数说明
    spring.jpa.hibernate.ddl-auto首次启动服务需设置成 create会自动生成三个表 hibernate_sequence, t_logger, t_tx_exception

  • TM启动文件

@SpringBootApplication
@EnableTransactionManagerServer
public class TxLcnTMApp {
    public static void main(String[] args) {
        SpringApplication.run(TxLcnTMApp.class, args);
    }

}
  • TM监控页面
    http://localhost:8391 密码 123456
    在这里插入图片描述

2. 业务服务

  • pom.xml 相关 Maven包
    <dependencies>
      <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>
    </dependencies>
  • 业务服务的 两个基本配置
    注意: 业务服务的两个基本环境文件是每个业务服务都需要配的, 除了端口不同其它基本一致
  1. 环境文件 1 bootstrap.yml

server:
  port: 8081

eureka:
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 2 # 5秒钟发送一次心跳
    lease-expiration-duration-in-seconds: 4 # 10秒不发送就过期
  client:
    serviceUrl:
      defaultZone: http://localhost:9000/eureka/

spring:
  application:
    name: order-service
    
  1. 环境文件 2 application.properties

# hystrix time out 
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=18000
ribbon.ConnectTimeout=16000
ribbon.ReadTimeout=16000
#Mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&serverTimezone=PRC&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=
##Mybatis
mybatis.mapper-locations=classpath:mapper/**/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true

# 是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
# 默认之配置为TM的本机默认端口
tx-lcn.client.manager-address=127.0.0.1:8070
# 开启日志,默认为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}

  • 业务启动文件
@EnableDistributedTransaction
@EnableTransactionManagement
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserApp {
    public static void main(String[] args) {
        SpringApplication.run(UserApp.class, args);
    }

}
  • 需事物的方法上方都需加上两个注释 @LcnTransaction和 @Transactional
    @LcnTransaction
    @Transactional
    public void create(final String name, final Integer age, final String status, final String type) {
        eventClient.create(name, type);
        orderClient.create(name, status);
        userDao.create(name, age);
    }

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值