一、TM配置
pom.xml
文件中添加依赖:
<dependency>
<groupId>com.codingapi.txlcn</groupId>
<artifactId>txlcn-tm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
修改application.properties
文件:
tx-lcn.manager.port=8070
这个配置,用于配置LCN的监听端口,在下面配置客户端服务里需要用到这个端口号
server.port=7970
spring.application.name=tx-manager
#mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
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
#TM后台登陆密码,默认值为codingapi
tx-lcn.manager.admin-key=123456
logging.level.com.codingapi=debug
#开启日志,默认为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}
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
修改启动类,添加@EnableTransactionManagerServer
注解:
@EnableTransactionManagerServer
@SpringBootApplication
public class TxManagerApplication {
public static void main(String[] args) {
SpringApplication.run(TxManagerApplication.class, args);
}
}
应用启动后,访问http://localhost:7970/
,密码更改为123456,进入TxManager系统后台界面
二、TC配置
pom.xml
文件中添加依赖:
<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>
修改application.properties
文件:
tx-lcn.client.manager-address=127.0.0.1:8070
#是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
#是否开启日志记录,当开启以后需要配置对应logger的数据库连接配置信息
tx-lcn.logger.enabled=true
tx-lcn.logger.driver-class-name=com.mysql.cj.jdbc.Driver
tx-lcn.logger.jdbc-url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
tx-lcn.logger.username=root
tx-lcn.logger.password=123456
修改启动类,添加@EnableDistributedTransaction
注解:
@EnableDiscoveryClient
//开启分布式事务
@EnableDistributedTransaction
@SpringBootApplication
public class BankProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BankProviderApplication.class, args);
}
}
修改业务类,配置事务发起方:
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Autowired
private BankProviderClient bankProviderClient;
@Override
@LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事务注解
@Transactional
public Boolean transferAccounts(int money, String userFrom, String userTo) {
Account myAccount = new Account();
myAccount.setMoney(money);
myAccount.setUser(userFrom);
accountMapper.subMoney(myAccount);
bankProviderClient.addMoney(money, userTo);//调用bank-provider服务
int i = 1 / 0;
return true;
}
}
DTXPropagation源码:
public enum DTXPropagation {
/**
* 当前没有分布式事务,就创建。当前有分布式事务,就加入
*/
REQUIRED,
/**
* 当前没有分布式事务,非分布式事务运行。当前有分布式事务,就加入
*/
SUPPORTS;
所以一般事务发起方使用REQUIRED
,事务参与方使用SUPPORTS
配置事务参与方:
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
//事务的参与方
@LcnTransaction(propagation = DTXPropagation.SUPPORTS)
@Transactional
public Boolean addMoney(int money, String user) {
Account account = new Account();
account.setMoney(money);
account.setUser(user);
accountMapper.addMoney(account);
return true;
}
}
LCN 5.0.2版本修改tx-client不配置tx-manager地址,改从redis主动拉取
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
修改application.properties
文件,添加redis相关配置:
#是否启动LCN负载均衡策略(优化选项,开启与否,功能不受影响)
tx-lcn.ribbon.loadbalancer.dtx.enabled=true
#是否开启日志记录,当开启以后需要配置对应logger的数据库连接配置信息
tx-lcn.logger.enabled=true
tx-lcn.logger.driver-class-name=${spring.datasource.driver-class-name}
tx-lcn.logger.jdbc-url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
tx-lcn.logger.username=root
tx-lcn.logger.password=123456
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
SpringCloud整合TX-LCN 5.0.2 Demo的代码已经上传到GitHub上了,地址:https://github.com/hxt970311/tx-lcn-demo