終於跑通分布式事務框架tcc-transaction的示例項目


1、背景

前段時間在看項目代碼的時候,發現有些接口的流程比較長,在各個服務里面都有通過數據庫事務保證數據的一致性,但是在上游的controller層並沒有對一致性做保證。

網上查了下,還沒找到基於Go開源的比較成熟的分布式事務框架。

於是,准備看看之前隔壁部門大佬寫的tcc-transaction,這是一個基於tcc思想實現的分布式事務框架。

tcc分別代碼Try,Confirm和Cancel。

 

Try: 嘗試執行業務

  • 完成所有業務檢查(一致性)
  • 預留必須業務資源(准隔離性)

 

Confirm: 確認執行業務

  • 真正執行業務
  • 不作任何業務檢查
  • 只使用Try階段預留的業務資源
  • Confirm操作滿足冪等性

 

Cancel: 取消執行業務

  • 釋放Try階段預留的業務資源
  • Cancel操作滿足冪等性
  •  

要了解其實現原理,第一步就是跑通項目自帶的示例,即tcc-transaction-tutorial-sample部分的代碼。

今天主要介紹在跑通tcc-transaction-tutorial-sample過程中遇到的各種坑。

 

 

2、依賴環境

  • Java
  • Maven
  • Git
  • MySQL
  • Redis
  • Zookeeper
  • Intellij IDEA

 

源碼地址:https://github.com/changmingxie/tcc-transaction

我自己Fork了一份(配置改動已提交):https://github.com/DMinerJackie/tcc-transaction

 

3、踩坑歷程

踩坑准備

第一步:克隆代碼

使用"git clone https://github.com/DMinerJackie/tcc-transaction"命令下載代碼

 

第二步:導入代碼並執行數據庫腳本

代碼導入Intellij IDEA。

執行tcc-transaction-http-sample/src/main/dbscripts 下的數據庫腳本。

 

第三步:修改配置文件

主要修改的是數據庫配置參數。拿tcc-transaction-dubbo-sample舉例,需要修改的文件有

tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-dubbo-sample/tcc-transaction-dubbo-capital/src/main/resources/tccjdbc.properties

tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-dubbo-sample/tcc-transaction-dubbo-redpacket/src/main/resources/tccjdbc.properties

tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-dubbo-sample/tcc-transaction-dubbo-order/src/main/resources/tccjdbc.properties

三個文件修改后對應配置如下

# 根據具體的MySQL版本使用驅動名稱
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
# 換成你連接數據庫的地址
tcc.jdbc.url=jdbc:mysql://127.0.0.1:3306/TCC?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
# 換成你需要配置數據庫的用戶名
jdbc.username=root
# 換成你需要配置數據庫的密碼
jdbc.password=rootroot

c3p0.initialPoolSize=10
c3p0.minPoolSize=10
c3p0.maxPoolSize=30
c3p0.acquireIncrement=3
c3p0.maxIdleTime=1800
c3p0.checkoutTimeout=30000 

同時修改tcc-transaction-sample-capital、tcc-transaction-sample-redpacket和tcc-transaction-sample-order三個項目中jdbc.proerties文件的數據庫連接,修改后配置如下

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/TCC_CAP?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
tcc.jdbc.url=jdbc:mysql://127.0.0.1:3306/TCC?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=rootroot

c3p0.initialPoolSize=10
c3p0.minPoolSize=10
c3p0.maxPoolSize=30
c3p0.acquireIncrement=3
c3p0.maxIdleTime=1800
c3p0.checkoutTimeout=30000

  

第四步:啟動項目

結合項目的README.md文件以及網上的文章了解到如果要跑通示例項目,需要分別啟動三個項目。

tcc-transaction提供了兩個版本:

  • 基於dubbo通訊的示例版本
  • 基於http通訊的示例版本

這兩個版本對於的三個項目分別是

  • tcc-transaction-dubbo-capital(賬戶資產服務)、 tcc-transaction-dubbo-redpacket(紅包服務)、 tcc-transaction-dubbo-order(交易訂單服務)
  • tcc-transaction-http-capital(賬戶資產服務)、 tcc-transaction-http-redpacket(紅包服務)、 tcc-transaction-http-order(交易訂單服務)

其實這兩個版本我都跑過,最終成功跑通的只有基於dubbo通訊的示例版本(http版本在最后confirm的時候最是失敗,導致最終訂單狀態為unkown)。

 

以基於dubbo通訊的示例為例

tcc-transaction-dubbo-capital的啟動配置如下

tcc-transaction-dubbo-redpacket的啟動配置如下

tcc-transaction-dubbo-order的啟動配置如下

 

坑1:連接不上zk

啟動tcc-transaction-dubbo-capital項目,報錯信息如下

[sample-dubbo-capital]2019-08-31 17:48:05,312 INFO [org.apache.zookeeper.ZooKeeper] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.I0Itec.zkclient.ZkClient@32c7bb63
[sample-dubbo-capital]2019-08-31 17:48:05,334 INFO [org.apache.zookeeper.ClientCnxn] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[sample-dubbo-capital]2019-08-31 17:48:05,344 WARN [org.apache.zookeeper.ClientCnxn] Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
[sample-dubbo-capital]2019-08-31 17:48:06,456 INFO [org.apache.zookeeper.ClientCnxn] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[sample-dubbo-capital]2019-08-31 17:48:06,459 WARN [org.apache.zookeeper.ClientCnxn] Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
[sample-dubbo-capital]2019-08-31 17:48:07,566 INFO [org.apache.zookeeper.ClientCnxn] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[sample-dubbo-capital]2019-08-31 17:48:07,567 WARN [org.apache.zookeeper.ClientCnxn] Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused

  

從報錯信息,一眼就看出是連不上zk即zookeeper。

這個很好理解,因為本地沒有安裝zk,於是安裝並通過"./zkServer.sh start"啟動zk

 

 

坑2:redis連不上

啟動tcc-transaction-dubbo-order報錯部分信息如下:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.mengyun.tcctransaction.sample.dubbo.order.service.PlaceOrderServiceImpl org.mengyun.tcctransaction.sample.dubbo.order.web.controller.OrderController.placeOrderService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placeOrderServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.mengyun.tcctransaction.sample.dubbo.order.service.PaymentServiceImpl org.mengyun.tcctransaction.sample.dubbo.order.service.PlaceOrderServiceImpl.paymentService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'paymentServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.mengyun.tcctransaction.sample.dubbo.capital.api.CapitalTradeOrderService org.mengyun.tcctransaction.sample.dubbo.order.service.PaymentServiceImpl.capitalTradeOrderService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'captialTradeOrderService': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compensableTransactionAspect' defined in class path resource [tcc-transaction.xml]: Cannot resolve reference to bean 'transactionConfigurator' while setting bean property 'transactionConfigurator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionConfigurator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.mengyun.tcctransaction.TransactionRepository org.mengyun.tcctransaction.spring.support.SpringTransactionConfigurator.transactionRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionRepository' defined in file [/Users/jackie/workspace/tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-dubbo-sample/tcc-transaction-dubbo-order/target/tcc-transaction-dubbo-order-1.2.6/WEB-INF/classes/config/spring/local/appcontext-service-tcc.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'jedisPool' threw exception; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:526)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)
... 60 more

  

 

這個和上面的原因類似,本地沒有安裝redis,導致無法拿到redis連接。

於是安裝redis,並使用"redis-server"啟動redis。

 

坑3:Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

三個項目都啟動后,可以看到一個商品鏈接列表頁,但是在點擊鏈接后無法跳轉,並且報錯如下

Type Exception Report
Message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
### The error may exist in URL [jar:file:/Users/jackie/workspace/tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-http-sample/tcc-transaction-http-order/target/tcc-transaction-http-order-1.2.6/WEB-INF/lib/tcc-transaction-sample-order-1.2.6.jar!/config/sqlmap/main/sample-product.xml]
### The error may involve org.mengyun.tcctransaction.sample.order.infrastructure.dao.ProductDao.findByShopId
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)

Root Cause
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
### The error may exist in URL [jar:file:/Users/jackie/workspace/tcc-transaction/tcc-transaction-tutorial-sample/tcc-transaction-http-sample/tcc-transaction-http-order/target/tcc-transaction-http-order-1.2.6/WEB-INF/lib/tcc-transaction-sample-order-1.2.6.jar!/config/sqlmap/main/sample-product.xml]

  

 

根據錯誤信息,排查是MySQL版本和數據庫驅動版本不匹配。

本地的MySQL版本是"8.0.11 MySQL Community Server - GPL",但是tcc-transaction中對應的驅動版本是

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.33</version>
</dependency>

 

改為

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>

同時針對高版本,需要在連接的jdbc-url后面加上useSSL=false

 

坑4:Loading class `com.mysql.jdbc.Driver'. This is deprecated

啟動tcc-transaction-dubbo-redpacket時,在日志中看到一個警告"Loading class `com.mysql.jdbc.Driver'. This is deprecated"。

通過搜索,發現是因為數據庫驅動com.mysql.jdbc.Driver'已經被棄用了,需要使用com.mysql.cj.jdbc.Driver,於是修改jdbc.proerties的配置(具體配置見上面),啟動正常。

 

踩完上面的坑后,啟動三個項目,完整走完流程,實現了一個基於分布式事務的商品購買行為,具體過程如下圖所示

 

4、 總結

運行示例項目的過程不算太順利,主要有一下幾個原因吧

  • 本地環境配置和項目提供的不一致,導致走了很多彎路,比如MySQL的版本。
  • 缺少詳細的跑示例項目的文檔說明。
  • 網上提供的資料比較粗略,也比較陳舊,文中能跑起來的步驟說明已經不適用現在的代碼了。

 

所以,在踩完這么多坑總結下,避免后面的人走同樣的彎路。

 

 

如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕,您的“推薦”將是我最大的寫作動力!如果您想持續關注我的文章,請掃描二維碼,關注JackieZheng的微信公眾號,我會將我的文章推送給您,並和您一起分享我日常閱讀過的優質文章。


免責聲明!

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



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