<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## :-: springcloud 整合TX-LCN實現分布式事務 ## 前言 ``` SpringCloud分布式架構給我們帶來開發上的便利,同時增加了我們對事務管理的難度,微服務的遍地開花,本地事務已經無法滿足分布式的要求,由此分布式事務問題誕生。 分布式事務被稱為世界性的難題。 ``` 本文記錄整合TX-LCN分布式事務框架管理分布式事務,用的版本是5.0.2.RELEASE ## TX-LCN ### 簡單介紹 TX-LCN分布式事務框架,LCN并不生產事務,LCN只是本地事務的協調工,LCN是一個高性能的分布式事務框架,兼容dubbo、springcloud框架,支持RPC框架拓展,支持各種ORM框架、NoSQL、負載均衡、事務補償 ### 特性一覽 1、一致性,通過TxManager協調控制與事務補償機制確保數據一致性 2、易用性,僅需要在業務方法上添加@TxTransaction注解即可 3、高可用,項目模塊不僅可高可用部署,事務協調器也可集群化部署 4、擴展性,支持各種RPC框架擴展,支持通訊協議與事務模式擴展 本文實現的是基于spring-cloud-alibaba整合tx-lcn實現分布式事務。 ~~~ springcloud :Hoxton.RELEASE spring-cloud-alibaba:2.2.0.RELEASE spring-boot:2.3.2.RELEASE openfeign:2.2.0.RELEASE nacos:1.0.0 ~~~ 原理 ![](https://box.kancloud.cn/caa18718890ded7863b21ab93dcc18ca_820x805.png) ## 實施步驟1 【搭建tx-manage 管理后臺服務】 創建數據庫、表 1. 創建MySQL數據庫, 名稱為:hj\_mall\_tx 2. 創建數據表:t\_tx\_exception ``` 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 NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` 下載官網提供的最新版的TM項目,修改配置文件(PS:由于官網的下載地址打不開,我們去GitHub上面下載例子:[https://github.com/codingapi/txlcn-demo](https://github.com/codingapi/txlcn-demo)),參考txlcn-demo-tm工程,在我們之前的項目下面創建一個springboot項目叫txlcn-tm **改造項目:使用遠程nacos配置中心配置項目 且分為多環境 改造過程略......tx-manage項目配置文件如下** ``` spring.application.name=hmall-tx-manager server.port=9009 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://***************.mysql.rds.aliyuncs.com:3306/hj_mall_tx spring.datasource.username=**** spring.datasource.password=**** 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=9108 # 心跳檢測時間(ms) tx-lcn.manager.heart-time=15000 # 分布式事務執行總時間 tx-lcn.manager.dtx-time=3000000 #參數延遲刪除時間單位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 #redis 主機 spring.redis.host=*********** #redis 端口 spring.redis.port=6379 #redis 密碼 spring.redis.password=********* # 開啟日志,默認為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} #事物執行時間 ``` ``` <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-tm</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!--alibaba:nacos配置中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> ``` ***** \*\*打包執行hmall-tx-manage.jar \*\* 瀏覽器打開http://127.0.0.1:9009 如下 ![](https://img.kancloud.cn/91/76/917680e2e12be4cde4d31e1a8fe0ebda_1228x547.png) ![](https://img.kancloud.cn/f4/fa/f4fa54b36b4fa8badc118d9825b80d25_1085x589.png) ## 實施步驟2【搭建2個 Tx-Client】 1、創建2Tx-Client個項目 作為事務消費者服務。 TC端參照官網一步步操作:[https://www.txlcn.org/zh-cn/docs/start.html](https://www.txlcn.org/zh-cn/docs/start.html) ![](https://img.kancloud.cn/9a/85/9a85ece71aebc4e6077555332ab75b87_263x315.png) 2、每個服務配置如下 ``` <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> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` ``` server: port: 6001 spring: # --------------cloud配置------------------ cloud: nacos: discovery: server-addr: 127.0.0.1:8848 #注冊服務控制中心 main: allow-bean-definition-overriding: true application: name: tx-lcn-A #------------數據庫鏈接----------------------------- datasource: url: ${datasource.url} username: ${datasource.username} password: ${datasource.password} driver-class-name: ${datasource.driver-class-name} mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml #實體掃描,多個package用逗號或者分號分隔 typeAliasesPackage: com.hjf.test.entity,com.hjf.base,MyOgnl configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 是否開啟自動駝峰命名規則(camel case)映射,即從經典數據庫列名 A_COLUMN(下劃線命名) 到經典 Java 屬性名 aColumn(駝峰命名) 的類似映射 map-underscore-to-camel-case: false global-config: db-config: id-type: auto datasource: url: jdbc:mysql://***********.mysql.rds.aliyuncs.com:3306/hj_mall?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: *** driver-class-name: com.mysql.jdbc.Driver tx-lcn: client: manager-address: 127.0.0.1:9108 springcloud: loadbalance: enabled: true logger: password: ******** username: root jdbc-url: jdbc:mysql://*****************.mysql.rds.aliyuncs.com:3306/hj_mall_tx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver enabled: true springcloud: loadbalance: enabled: true #開啟hystrix 熔斷 feign: hystrix: enabled: true # --------------負載均衡器配置------------------ ribbon: ReadTimeout: 60000 ConnectTimeout: 60000 ``` 3、在啟動類上使用?@EnableDistributedTransaction ~~~ //注冊服務到注冊中心去 @EnableDiscoveryClient @SpringBootApplication //開啟分布式事務 @EnableDistributedTransaction @EnableTransactionManagement @EnableFeignClients @ServletComponentScan(basePackages = "com.hjf") public class TxLanClient1Application { public static void main(String[] args) { SpringApplication.run(TxLanClient1Application.class, args); } } ~~~ 4、流程分析 txlcn-client2 使用openfeign 調用遠程txlcn-client1 添加數據庫服務, 然后在執行一次添加數據庫;正常情況下會添加2條數據 發生異常會回滾事務。添加0條數據。 ### **A:調用方代碼** ~~~ /** * @LcnTransaction//分布式事務 * @Transactional//本地事務 */ @LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事務注解 @Override public BaseResp addCarousel(MarketCarousel q) { String groupId = TracingContext.tracing().groupId(); String applicationId = Transactions.getApplicationId(); System.out.println(groupId); System.out.println(applicationId); marketCarouselMapper.insert(q); BaseResp r=feignService.add(q); if (StringUtils.isBlank(q.getTitle())){ throw new NullPointerException("拋出異常........"); } return BaseResp.SUCCESS; } ~~~ ### **B:遠程方法調用 feign通過nacos注冊中心** ~~~ /** * 遠程服務名稱 */ @FeignClient(name = "tx-lcn-A") public interface FeignService { @PostMapping("/cus/add") public BaseResp add(@RequestBody MarketCarousel q); } ~~~ ### **C:遠程方法** ~~~ @RestController @Slf4j @RequestMapping("/cus") public class CusController { @Resource BootService bootService; @RequestMapping(value = "add") public BaseResp addCarousel(@RequestBody MarketCarousel q){ BaseResp r=bootService.addCarousel(q); return r; } } ~~~ ~~~ /** * @LcnTransaction//分布式事務 * @Transactional //本地事務 */ @LcnTransaction(propagation = DTXPropagation.SUPPORTS) @Override public BaseResp addCarousel(MarketCarousel q) { String groupId = TracingContext.tracing().groupId(); String applicationId = Transactions.getApplicationId(); System.out.println(groupId); System.out.println(applicationId); marketCarouselMapper.insert(q); return BaseResp.SUCCESS; } ~~~ ### **D:測試** 1、正常 ![](https://img.kancloud.cn/bf/3a/bf3a8411968b59c55ba4145d51e0e804_606x472.png) ![](https://img.kancloud.cn/a7/08/a708d8ec36137bec17a322d9bb3e8540_744x212.png) 2 異常:事務回滾 ![](https://img.kancloud.cn/3b/82/3b82540c6aecd52dbb2b5681b9ac9ca5_593x409.png) ![](https://img.kancloud.cn/fd/4d/fd4de377fbe793ce3aeb295bc03526ef_689x221.png) ![](https://img.kancloud.cn/97/44/9744e565a93a55586e5228df66d0f113_1225x253.png) ![](https://img.kancloud.cn/30/90/3090bdb140c52c12329ae4286d4c107a_987x506.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看