TxClient使用
- 配置LCN的maven包
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-client</artifactId>
<version>{lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>{lcn.last.version}</version>
</dependency>
- 再根据项目的rpc方式不同选择对应springcloud/dubbo/motan的扩展支持
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>{lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-dubbo</artifactId>
<version>{lcn.last.version}</version>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-motan</artifactId>
<version>{lcn.last.version}</version>
</dependency>
配置tx-manager的url地址。有两种方式
- 方式一: 使用默认方式是添加tx.properties文件,内容如下
#txmanager地址
url=http://127.0.0.1:8899/tx/manager/
- 方式二: 自定义url的配置
编写配置文件到application.properties文件下keytm.manager.url如tm.manager.url=http://127.0.0.1:8899/tx/manager/。
复写读取配置文件的类TxManagerTxUrlService。
import com.codingapi.tx.config.service.TxManagerTxUrlService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class TxManagerTxUrlServiceImpl implements TxManagerTxUrlService{
@Value("${tm.manager.url}")
private String url;
@Override
public String getTxUrl() {
System.out.println("load tm.manager.url ");
return url;
}
}
业务开发人员声明全局事务
- 在使用LCN分布式事务时,只需要将事务的开始方法添加
@TxTransaction(isStart=true)
注解即可,在参与方添加@TxTransaction
或者实现ITxTransaction
接口即可。
@TxTransaction(isStart = true)
@Transactional
public int save() {
//保存一部分业务数据
int rs1 = testMapper.save("mybatis-hello-1");
//调用事务参与方的restfull接口
int rs2 = demo2Client.save();
在参与方的子事务方法上加上 @TxTransaction
@TxTransaction
@Transactional
public int save() {
//事务参与方的数据保存
int rs = testMapper.save("mybatis-hello-2");
TxManager启动说明
- 启用redis服务
在application.properties配置文件中,配置redis服务连接信息
##redis 集群环境配置
##redis cluster
spring.redis.cluster.nodes={ip&port}
spring.redis.cluster.commandTimeout=5000
- 启用eureka服务发现服务
在application.properties配置文件中,配置eureka服务器信息
#eureka 地址
eureka.client.service-url.defaultZone=http://**/eureka
eureka.instance.prefer-ip-address=true
- 视使用情况,配置application.properties文件中的其它参数
#######################################txmanager-start#################################################
#服务端口
server.port=8899
#tx-manager不得修改
spring.application.name=tx-manager
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/static/
#######################################txmanager-end#################################################
#zookeeper地址
#spring.cloud.zookeeper.connect-string=127.0.0.1:2181
#eureka 地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
#######################################redis-start#################################################
#redis 配置文件,根据情况选择集群或者单机模式
##redis 集群环境配置
##redis cluster
#spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003
#spring.redis.cluster.commandTimeout=5000
##redis 单点环境配置
#redis
#redis主机地址
spring.redis.host=127.0.0.1
#redis主机端口
spring.redis.port=6379
#redis链接密码
spring.redis.password=
spring.redis.pool.maxActive=10
spring.redis.pool.maxWait=-1
spring.redis.pool.maxIdle=5
spring.redis.pool.minIdle=0
spring.redis.timeout=0
#####################################redis-end###################################################
#######################################LCN-start#################################################
#业务模块与TxManager之间通讯的最大等待时间(单位:秒)
#通讯时间是指:发起方与响应方之间完成一次的通讯时间。
#该字段代表的是Tx-Client模块与TxManager模块之间的最大通讯时间,超过该时间未响应本次请求失败。
tm.transaction.netty.delaytime = 5
#业务模块与TxManager之间通讯的心跳时间(单位:秒)
tm.transaction.netty.hearttime = 15
#存储到redis下的数据最大保存时间(单位:秒)
#该字段仅代表的事务模块数据的最大保存时间,补偿数据会永久保存。
tm.redis.savemaxtime=30
#socket server Socket对外服务端口
#TxManager的LCN协议的端口
tm.socket.port=9999
#最大socket连接数
#TxManager最大允许的建立连接数量
tm.socket.maxconnection=100
#事务自动补偿 (true:开启,false:关闭)
# 说明:
# 开启自动补偿以后,必须要配置 tm.compensate.notifyUrl 地址,仅当tm.compensate.notifyUrl 在请求补偿确认时返回success或者SUCCESS时,才会执行自动补偿,否则不会自动补偿。
# 关闭自动补偿,当出现数据时也会 tm.compensate.notifyUrl 地址。
# 当tm.compensate.notifyUrl 无效时,不影响TxManager运行,仅会影响自动补偿。
tm.compensate.auto=false
#事务补偿记录回调地址(rest api 地址,post json格式)
#请求补偿是在开启自动补偿时才会请求的地址。请求分为两种:1.补偿决策,2.补偿结果通知,可通过通过action参数区分compensate为补偿请求、notify为补偿通知。
#*注意当请求补偿决策时,需要补偿服务返回"SUCCESS"字符串以后才可以执行自动补偿。
#请求补偿结果通知则只需要接受通知即可。
#请求补偿的样例数据格式见代码
#请求补偿的返回数据样例数据格式:
#SUCCESS
#请求补偿结果通知的样例数据格式:
#{"resState":true,"groupId":"TtQxTwJP","action":"notify"}
tm.compensate.notifyUrl=http://ip:port/path
#补偿失败,再次尝试间隔(秒),最大尝试次数3次,当超过3次即为补偿失败,失败的数据依旧还会存在TxManager下。
tm.compensate.tryTime=30
#各事务模块自动补偿的时间上限(毫秒)
#指的是模块执行自动超时的最大时间,该最大时间若过段会导致事务机制异常,该时间必须要模块之间通讯的最大超过时间。
#例如,若模块A与模块B,请求超时的最大时间是5秒,则建议改时间至少大于5秒。
tm.compensate.maxWaitTime=5000
#######################################LCN-end#################################################
- 启动tx-manager-4.x.x.jar。若从源码下则需要启动运行TxManagerApplication
然后访问http://{IP}:8899/index.html 可以展示tx-manager运行信息