Tx-LCN SpringCloud分佈式事務處理

TX-LCN官網http://www.txlcn.org/zh-cn/index.html

環境依賴:springboot2.0以上+springcloud+eureka+redis3.2+mysql5.6+feign

1.先新建一個springboot模塊
2.在pom文件中添加maven依賴:

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

3.在啓動類上添加註解
@EnableTransactionManagerServer

@SpringBootApplication
@EnableTransactionManagerServer
public class TransactionManagerApplication {

  public static void main(String[] args) {
      SpringApplication.run(TransactionManagerApplication.class, args);
  }

}

4.填寫配置
填寫配置前,先在mysql上創建一個數據庫,名稱爲: tx-manager
創建數據表:

CREATE TABLE `t_tx_exception`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `transaction_state` tinyint(4) NULL DEFAULT NULL,
  `registrar` tinyint(4) NULL DEFAULT NULL,
  `remark` varchar(4096) NULL DEFAULT  NULL,
  `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解決 1已解決',
  `create_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

根據你自己環境配置,主要是數據庫地址和註冊中心的地址

spring.application.name=tx-manager
server.port=7970

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#指定註冊中心地址
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=

5.到此你的tm已經配置完成了,啓動這個服務,打開瀏覽器訪問http://localhost:7970/admin/index.html#/login
你可以看到tx-manager的後臺管理,類似註冊中心那種,可以看到你有哪些服務需要做事務處理的。登錄的默認密碼爲codingapi
已註冊的TC就是你要做事務的客服端,這裏稱爲tc-client

已註冊的TC就是你要做事務的客服端,這裏稱爲tc-client
六、配置tc-client
就是你要做事務的哪些微服務
在你要做事務處理的每一個tc-client中添加如下maven依賴

<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>

然後在啓動類上加入註解
@EnableDistributedTransaction

@SpringBootApplication
@EnableDistributedTransaction
public class DemoAApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoDubboClientApplication.class, args);
    }

}

然後在你需要做的事務事務的方法上加入註解@LcnTransaction //分佈式事務註解
比如:這裏兩個服務,A服務給B服務轉錢,A服務減一百,B服務加一百。這個處理就需要做事務處理了,以保證數據的一致性。
那在A服務Service層上加入註解@LcnTransaction,A的服務層去調用B服務,在B服務中的service成也需要加上@LcnTransaction註解,在每個service方法上也要加上本地事務註解@Transaction
在你處理的整個業務的時候,如果需要事務處理,那你就得在再涉及到你這個業務所有服務的所對應的處理的service層加上註解。
最後說一下,在每一個tc-client配置文件中需要制定tx-manager的地址,因爲我用的是默認配置,所以沒有寫。若有修改tx-manager 在tc-client中添加如下配置
默認之配置爲TM的本機默認端口
tx-lcn.client.manager-address=127.0.0.1:8070
七、github地址:https://github.com/zhangjunli16811/tx-lcn-springcloud-transaction
覺得不錯的小夥伴可以去star一下哦。
如果通過feign 調用時,feign註解中加了fallbackFactory =xxx.class的話;在Fallback方法裏面加上這個
DTXUserControls.rollbackGroup(TracingContext.tracing().groupId());

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章