1、配置地址1: https://segmentfault.com/a/1190000016718525
配置地址2:https://segmentfault.com/a/1190000018973869
配置地址3: https://blog.csdn.net/oyh1203/article/details/82189445
按步走:
1、spring cloude集成LCN
<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、使用feign调用服务 修改requestInterceptor
public class TransactionRestTemplateInterceptor implements RequestInterceptor {
private Logger logger = LoggerFactory.getLogger(TransactionRestTemplateInterceptor.class);
public TransactionRestTemplateInterceptor() {
}
public void apply(RequestTemplate requestTemplate) {
TxTransactionLocal txTransactionLocal = TxTransactionLocal.current();
String groupId = txTransactionLocal == null ? null : txTransactionLocal.getGroupId();
this.logger.info("LCN-SpringCloud TxGroup info -> groupId:" + groupId);
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = requestAttributes == null ? null : ((ServletRequestAttributes) requestAttributes).getRequest();
Object attribute = request.getAttribute("OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE");
String token = attribute == null ? null : attribute.toString();
requestTemplate.header("Authorization", "Bearer " + token);
if (txTransactionLocal != null) {
requestTemplate.header("tx-group", new String[]{groupId});
}
}
}
@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;
}
@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;
}
3、新建tx_manage项目,注册为eureka服务
#服务端口
server.port=7000
#tx-manager不得修改!!!!!!
spring.application.name=tx-manager
#eureka 地址(注册到自己的eureka)
eureka.client.service-url.defaultZone=http://127.0.0.1:8880/eureka/
#yml
init-db: true
#txmanager地址
tm:
manager:
url: http://127.0.0.1:7000/tx/manager/
logging:
level:
com:
codingapi: debug
##Ribbon的负载均衡策略
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
MaxAutoRetriesNextServer: 0
#tx.properties
url=http://127.0.0.1:7000/tx/manager/
4、如何使用
//事务发起方:@TxTransaction(isStart = true)
/**
* 测试分布式事务
*/
@TxTransaction(isStart = true)
@Transactional
public void testM() {
MallUser user = mallUserMapper.selectList(new EntityWrapper<MallUser>()).get(0);
Random random = new Random();
int i = random.nextInt(100);
user.setName(i + "FF");
mallUserMapper.updateById(user);
log.info("i:" + i);
MallItem mallItem = itemFeign.getItemById(1 + "");
mallItem.setItemDetail(i + "FF");
Boolean aBoolean = itemFeign.updateItemById(mallItem);
log.info("item:" + aBoolean);
throw new RuntimeException("333");
}
//事务参与方@TxTransaction
@Transactional
@TxTransaction
public Boolean updateItemById(MallItem mallItem){
boolean b = this.updateById(mallItem);
// throw new RuntimeException("33");
return b;
}
springCloud集成分布式事务seata 1.1.0(一)
seata官网:https://seata.io/zh-cn/