springcloud分布式事務TXLCN


新增一個model,pom文件引入依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.codingapi.txlcn</groupId>
    <artifactId>txlcn-tm</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>

 

 

配置文件中添加如下配置,其中數據庫及redis的賬號密碼自行更換:
spring.application.name=tx-manager
server.port=8101
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
  #指定注冊中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.instance.prefer-ip-address=true
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
  # 開啟日志
tx-lcn.logger.enabled=true
logging.level.com.codingapi=debug
  #redis 主機
spring.redis.host=127.0.0.1
  #redis 端口
spring.redis.port=6379
  #redis 密碼
spring.redis.password=
 
啟動類添加注解@EnableTransactionManagerServer

 

 

完成上面的操作事務就可以啟動了,記得先啟動注冊中心!
下面是修改需要添加分布式事務的模塊
 
pom添加依賴如下,最好添加在多個服務的父級模塊中,或common中
 
<dependency>
    <groupId>com.codingapi.txlcn</groupId>
    <artifactId>txlcn-tc</artifactId>
    <version>${txlcn.version}</version>
</dependency>
<dependency>
    <groupId>com.codingapi.txlcn</groupId>
    <artifactId>txlcn-txmsg-netty</artifactId>
    <version>${txlcn.version}</version>
</dependency>
 
啟動類添加注解@EnableDistributedTransaction
在每個需要事務的服務配置文件中添加
tx-lcn:
  client:
    manager-address: 127.0.0.1:8070
 
最后添加@LcnTransaction,這里注意,A調用B中,A和B都要添加注解,而且B要添加參數
txmanager和txlcient詳細的配置請去官網查看

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM