其他分享
首页 > 其他分享> > Spring Cloud + TX-LCN分布式事务框架 亲测

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

作者:互联网

1. tx服务

pom.xml 相关 Maven包

    <dependencies>
        <dependency>
            <groupId>com.codingapi.txlcn</groupId>
            <artifactId>txlcn-tm</artifactId>
	    <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

基本配置

/** 环境文件 1 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=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
# 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=


/** 环境文件 2 bootstrap.yml*/

server:
  port: 7970

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

启动文件

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

}

TM监控页面

http://localhost:7970 密码 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>

基本配置

/** 环境文件 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);
    }

标签:TX,tx,Spring,lcn,datasource,spring,LCN,logger,name
来源: https://blog.csdn.net/qcl108/article/details/100008107