SpringCloud 集成分佈式事務LCN
A. LCN的版本4.1.0只支持到spring boot 1.0 版本 (所以使用4.1.0版本這裏要注意了,這裏不做講解)
B.LCN版本5.2.0 已經支持spring boot2.0以上版本
今天說下支持springcloud 2.0使用、先去官網下載最新5.0.2.RELEASE 源碼
下載地址:https://github.com/codingapi/tx-lcn/releases
把它的源碼包下載下來,解壓後,把裏面的txlcn-tm(這個是lcn的核心,即事務協調器)這個項目單獨拿出來導入idea,並修改yml文件
server:
port: 7970
spring:
application:
name: tx-manager
profiles: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
password: 123456
url: jdbc:mysql://192.168.13.64:3306/tx-manager?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull
username: root
jpa:
hibernate:
ddl-auto: update
# redis配置
redis:
host: 192.168.13.64
port: 6379
#mybatis:
# configuration:
# map-underscore-to-camel-case: true
# use-generated-keys: true
#eureka配置
eureka:
client:
serviceUrl:
defaultZone: http://192.168.13.64:5060/eureka/
instance:
instance-id: http://192.168.13.64:5060
prefer-ip-address: true
logging:
level:
com:
codingapi: debug
#tx-manager 配置
tx-lcn:
#client:
# manager-address: 127.0.0.1:8070
logger:
driver-class-name: ${spring.datasource.driver-class-name}
enabled: true
jdbc-url: ${spring.datasource.url}
password: ${spring.datasource.password}
username: ${spring.datasource.username}
manager:
admin-key: 123456 #後臺登陸密碼 登陸頁:http://127.0.0.1:7970/admin/index.html
concurrent-level: 128
dtx-time: 10000
ex-url-enabled: true
heart-time: 12000
#tx-manager ip(client請求ip) 即:應用層連接的ip地址、用與連接事務協調器
host: "" #這裏部署到docker容器中特別注意,只能""或者0.0.0.0,不然識別不出來具體地址,在本地測試中可以寫上本機的ip地址
port: 8070
message:
netty:
attr-delay-time: 10000
#tm:
# socket:
# port: 8070
2.創建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;
3. 在各自的項目中pom文件中 引用LCN 5.2.0版本架包
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tc</artifactId>
<version>5.0.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>*</artifactId>
</exclusion>
####這裏可能需要排除日誌文件,防止衝突###
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-txmsg-netty</artifactId>
<version>5.0.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
注意:有時候爲了防止springboot的jar包衝突,需要將裏面關於springboot的所有jar包排除掉。
4. 在各自的客戶端A、B應用程序中的sprintg文件中添加調用上面tx-manager配置的客戶端地址
#配置連接tx-manager服務,這裏的服務就是tx-manager客戶端tx-lcn.manager.host地址
tx-lcn:
client:
manager-address: 192.168.13.64:8070
上面的配置如果沒有配置,系統默認是127.0.0.1:8080,就是默認大於配置,我後來項目全部註釋了這個,所以取得源碼裏面默認地址, 也就是你的部署容器的地址、下面是客戶端和服務端地址的默認值
5. 在服務A、B的啓動類上添加註解@EnableDistributedTransaction啓動分佈式事務、在服務A中的需要分佈式事務方法上面添加@LcnTransaction,服務B中添加@TxcTransaction或 @LcnTransaction註解。
6. 依次啓動eureka註冊中心,txlcn-tm,服務A、B
注意:這裏遇到一個坑、在本地測試完了是OK的,看了它的源碼裏面默認ip地址127.0.0.1:8070 所以在部署到docker中應用鏈接不上tx-manager 中的8070端口地址,因爲配置具體的地址 tx-lcn.manager.host=192.168.13.64,所以找不到這個,看了其他的網上參考,必須把這個設置爲 “” 空雙引號或者0.0.0.0下面是我的具體tx-manager項目中的配置文件信息(就是上面的配置)
本地測試成功:
14:34:17.883 default [http-nio-9091-exec-3] DEBUG c.c.t.tc.aspect.weave.DTXLogicWeaver - <---- TxLcn start ---->
14:34:17.884 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.c.DefaultGlobalContext - Start TxContext[5931a5f842537]
14:34:17.884 default [http-nio-9091-exec-3] DEBUG c.c.txlcn.tc.core.DTXServiceExecutor - pre business code, unit type: lcn @group(5931a5f842537)
14:34:17.886 default [http-nio-9091-exec-3] DEBUG c.c.t.t.a.weave.DTXResourceWeaver - proxy a sql connection: com.codingapi.txlcn.tc.core.transaction.lcn.resource.LcnConnectionProxy@e21d245.
14:34:18.938 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"982967939019400","msg":{"action":"heartCheck","state":100}}
14:34:18.942 default [http-nio-9091-exec-3] DEBUG c.c.txlcn.tc.core.DTXServiceExecutor - business success @group(5931a5f842537)
14:34:18.942 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.t.l.c.LcnRunningTransaction - join group: [GroupId: 5931a5f842537,Method: public void cn.bainuo.gene.project.service.impl.ProjectServiceImpl.testTxManager(java.lang.String)]
14:34:18.942 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.t.TransactionControlTemplate - join group > transaction type: lcn @group(5931a5f842537)
14:34:18.943 default [http-nio-9091-exec-3] DEBUG c.c.t.txmsg.netty.bean.SocketManager - get channel, key:/192.168.33.194:8070
14:34:18.943 default [http-nio-9091-exec-3] DEBUG c.c.t.txmsg.netty.bean.SocketManager - await response
14:34:18.943 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdEncoder - send->{"key":"5931ab0da66537","msg":{"action":"joinGroup","data":{"groupId":"5931a5f842537","transactionState":1,"unitId":"e12ee93bd3c9545ed0361ba2940d86db","unitType":"lcn"},"groupId":"5931a5f842537","state":100},"remoteKey":"/192.168.33.194:8070"}
14:34:18.951 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"5931ab0da66537","msg":{"action":"joinGroup","groupId":"5931a5f842537","state":200},"remoteKey":"/192.168.33.194:62065"}
14:34:18.951 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - got response message[Netty Handler]
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.t.txmsg.netty.bean.NettyRpcCmd - got response. 5931ab0da66537
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.t.txmsg.netty.bean.SocketManager - response is: MessageDto(action=joinGroup, groupId=5931a5f842537, data=null, state=200)
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.t.t.netty.impl.NettyRpcClient - cmd request used time: 9 ms
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.txlcn.tc.txmsg.LoopMessenger - request action: joinGroup. TM[/192.168.33.194:8070]
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.t.TransactionControlTemplate - join group message over. @group(5931a5f842537)
14:34:18.952 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.checking.SimpleDTXChecking - start delay checking task @group(5931a5f842537)
14:34:18.953 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.t.TransactionControlTemplate - join group logic over @group(5931a5f842537)
14:34:18.953 default [http-nio-9091-exec-3] DEBUG c.c.t.t.c.c.DefaultGlobalContext - Destroy TxContext[5931a5f842537]
14:34:18.953 default [http-nio-9091-exec-3] DEBUG c.c.txlcn.tc.core.DTXLocalContext - clean thread local[DTXLocalContext]: DTXLocalContext(transactionType=lcn, groupId=5931a5f842537, unitId=e12ee93bd3c9545ed0361ba2940d86db, resource=null, destroy=true, inGroup=false, attachment=null, sysTransactionState=1, userTransactionState=-1, proxy=true, justNow=false, proxyTmp=false)
14:34:18.953 default [http-nio-9091-exec-3] DEBUG c.c.t.tc.aspect.weave.DTXLogicWeaver - <---- TxLcn end ---->
14:34:18.963 default [pool-2-thread-2] DEBUG c.c.t.t.c.a.AsyncH2DBAspectLogger - async save aspect log. result: true groupId: 5931a5f842537, used time: 10ms
14:34:19.060 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"5931ab36a4d537","msg":{"action":"notifyUnit","data":{"groupId":"5931a5f842537","state":1,"unitId":"e12ee93bd3c9545ed0361ba2940d86db","unitType":"lcn"},"groupId":"5931a5f842537","state":100},"remoteKey":"/192.168.33.194:62065"}
14:34:19.064 default [tc-rpc-service-0] DEBUG c.c.txlcn.tc.txmsg.ClientRpcAnswer - Receive Message: MessageDto(action=notifyUnit, groupId=5931a5f842537, data=NotifyUnitParams(groupId=5931a5f842537, unitId=e12ee93bd3c9545ed0361ba2940d86db, unitType=lcn, state=1), state=100)
14:34:19.066 default [tc-rpc-service-0] DEBUG com.codingapi.txlcn.txmsg.LCNCmdType - parsed txmsg cmd: notifyUnit
14:34:19.067 default [tc-rpc-service-0] DEBUG c.c.txlcn.tc.support.TxLcnBeanHelper - getRpcBeanName->rpc_lcn_notify-unit
14:34:19.067 default [tc-rpc-service-0] DEBUG c.c.t.t.c.t.TransactionCleanTemplate - clean transaction @group(5931a5f842537)
14:34:19.067 default [tc-rpc-service-0] DEBUG c.c.t.t.c.t.l.r.LcnConnectionProxy - commit transaction type[lcn] proxy connection:com.codingapi.txlcn.tc.core.transaction.lcn.resource.LcnConnectionProxy@e21d245.
14:34:19.071 default [tc-rpc-service-0] DEBUG c.c.t.t.c.t.l.r.LcnConnectionProxy - transaction type[lcn] proxy connection:com.codingapi.txlcn.tc.core.transaction.lcn.resource.LcnConnectionProxy@e21d245 closed.
14:34:19.071 default [tc-rpc-service-0] DEBUG c.c.t.t.c.checking.SimpleDTXChecking - cancel 5931a5f842537:e12ee93bd3c9545ed0361ba2940d86db checking. @group(5931a5f842537)
14:34:19.071 default [tc-rpc-service-0] DEBUG c.c.t.t.c.t.TransactionCleanTemplate - clean transaction over @group(5931a5f842537)
14:34:19.072 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdEncoder - send->{"key":"5931ab36a4d537","msg":{"action":"notifyUnit","data":true,"state":200},"remoteKey":"/192.168.33.194:8070"}
14:34:19.074 default [pool-2-thread-3] DEBUG c.c.t.t.c.a.AsyncH2DBAspectLogger - async clear aspect log. result:true, groupId: 5931a5f842537, used time: 2ms
14:34:31.074 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"982967939019400","msg":{"action":"heartCheck","state":100}}
14:34:43.080 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"982967939019400","msg":{"action":"heartCheck","state":100}}
14:34:55.085 default [nioEventLoopGroup-4-1] DEBUG c.c.t.t.netty.handler.RpcCmdDecoder - cmd->{"key":"982967939019400","msg":{"action":"heartCheck","state":100}}
https://www.cnblogs.com/cq-yangzhou/p/11453257.html