springCloud分布式事务实战改造合服务BlockMicroService支持分布式事务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springCloud分布式事务实战改造合服务BlockMicroService支持分布式事务相关的知识,希望对你有一定的参考价值。
在BlockMicroService 工程 中加入
(1)加入jar
<!-- springCloud 事务 关键点1 -->
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>transaction-springcloud</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId>
<version>${lcn.last.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
(2) 修改配置文件application.properties
连接 事务服务器
加上 关键点2
tm.manager.url=http://127.0.0.1:7000/tx/manager/
(3)添加2个代码文件
1 http请求服务TxManagerHttpRequestServiceImpl.java
package com.jh.service.impl;
import com.codingapi.tx.netty.service.TxManagerHttpRequestService;
import com.lorne.core.framework.utils.http.HttpUtils;
import org.springframework.stereotype.Service;
/**
* http请求 //关键点3
*
*/
@Service
public class TxManagerHttpRequestServiceImpl implements TxManagerHttpRequestService{
@Override
public String httpGet(String url) {
System.out.println("httpGet-start");
String res = HttpUtils.get(url);
System.out.println("httpGet-end");
return res;
}
@Override
public String httpPost(String url, String params) {
System.out.println("httpPost-start");
String res = HttpUtils.post(url,params);
System.out.println("httpPost-end");
return res;
}
}
2 获取url TxManagerTxUrlServiceImpl.java
package com.jh.service.impl;
import com.codingapi.tx.config.service.TxManagerTxUrlService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* 获取url
*/
@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;
}
}
(4) 改造整合服务
@TxTransaction(isStart=true) //关键 4
@Override
@Transactional
public int saveBlockTheme(Block block, Theme theme) {
int rs1 = blockDao.saveBlock("jwg1", "111");// 3 保存1
int rs2 = themeClient.saveTheme("jwg2", "111", 1);// 4 保存2
int v = 100/0; //故障
return rs1 + rs2;
}
以上是关于springCloud分布式事务实战改造合服务BlockMicroService支持分布式事务的主要内容,如果未能解决你的问题,请参考以下文章