当前位置:网站首页>feign调用服务,被调用服务seata事务不开启或者xid为空
feign调用服务,被调用服务seata事务不开启或者xid为空
2022-04-22 05:11:00 【广铁小多多】
当前场景:app接口服务调用A服务和B服务。app接口内会回滚,A服务和B服务不会回滚。
App接口
@GlobalTransactional(rollbackFor = Exception.class)
public Result uploadFile(SaveLogDataReq req) {
System.out.println("seata全局事务id====================>"+ RootContext.getXID());
servieFeignA.addAAA(req);
servieFeignB.addBBB(req);
int i = 1/0;
return Resu.ok();
}
A服务
public Result addAAA(SaveLogDataReq req) {
System.out.println("seata全局事务id====================>"+ RootContext.getXID());
//todo AAA代码逻辑......
return Resu.ok();
}
B服务
public Result addBBB(SaveLogDataReq req) {
System.out.println("seata全局事务id====================>"+ RootContext.getXID());
//todo BBB代码逻辑......
return Resu.ok();
}
分析:查看打印日志显示,A服务和B服务的xid为空,或者与APP接口服务的xid不一致。
解决方案:
方案一,在fein远程调用的时候将当前的xid传入header
1,增加request拦截器
public class MultipartSupportConfig implements RequestInterceptor {
/**
* 解决服务直接调用请求头不传递的问题
* @param template
*/
@Override
public void apply(RequestTemplate template) {
//解决不传递请求头中的token
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null){
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
//可以在这里将自定义请求头传递进去, key 请求, value 值
//处理上游请求头信息,传递时继续携带
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
template.header(name, values);
}
}
// 解决seata的xid未传递
String xid = RootContext.getXID();
template.header(RootContext.KEY_XID, xid);
}
}
2,在调用A/B服务的fein注解上加入上面的request拦截器(configuration = MultipartSupportConfig.class)配置。
@FeignClient(contextId="LogDataService",value = "LogDataService",configuration = MultipartSupportConfig.class)
public interface ServieFeignA {
@PostMapping(value = "/api/logdata/v1/log/save",consumes = MediaType.APPLICATION_JSON_VALUE)
public Result<String> save(@RequestBody SaveLogDataReq req);
}
这个方案比较繁琐,不优先考虑。
方案二,使用spring-cloud-starter-alibaba-seata自动配置(推荐)
之前使用的是seata-spring-boot-starter配置,这里的seata自动配置不会对feign进行处理,所以fein调用不会传xid,导致全局事务未生效。
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
所以去掉这个maven配置,使用spring-cloud-starter-alibaba-seata替代。
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
这里会对fein进行seata相关的配置。

注意:
1,异常需要层层往上抛,如果你在子服务将异常处理的话(比如全局异常处理GlobalExceptionHandler),seata会认为你已经手动处理了异常。
2,出现事务失效的情况下,优先检查 RootContext.getXID() ,xid是否传递且一致。
3,主服务加上@GlobalTransactional注解即可,被调用服务不用加@GlobalTransactional和@Transactional.
4,@GlobalTransactional(rollbackFor = Exception.class)最好加上rollbackFor = Exception.class,表示遇到Exception都回滚,不然遇到有些异常(如自定义异常)则不会回滚。
版权声明
本文为[广铁小多多]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zdb1314/article/details/124211553
边栏推荐
- How does docker access redis service 6379 port in the host
- Summary of topic brushing in April 22
- Nexus私服——(三) 在项目实战中使用私服仓库
- style/TextAppearance. Compat. Notification. Info) not found.
- Chapter VIII affairs
- Summary of common methods, advantages and disadvantages of robot UAV visual obstacle avoidance
- Junit简介与入门
- Spark starter: wordcount
- Rookie's counter attack
- QBoxSet、QBoxPlotSeries
猜你喜欢
![[Reading Notes - > statistics] 07-03 introduction to the concept of discrete probability distribution Poisson distribution](/img/75/ca998bc741ac8029eb0f91f0bf26e8.png)
[Reading Notes - > statistics] 07-03 introduction to the concept of discrete probability distribution Poisson distribution

Write down the daily temperature

Analyzing redis distributed locks from the perspective of source code

6. Comparable to JMeter Net pressure measurement tool - crank practical chapter - collecting diagnosis tracking information and how to analyze bottlenecks

Chapter V function
![[I. XXX pest detection items] 3. Loss function attempt: focal loss](/img/91/5771f6d4b73663c75da68056207640.png)
[I. XXX pest detection items] 3. Loss function attempt: focal loss
Not sure whether it is a bug or a setting

What is the difference between two-dimensional convolution kernel and three-dimensional convolution kernel in convolution neural network?

Chapter I overview of database

TDD development mode and DDD development mode
随机推荐
MySQL double master and double slave + atlas data test
cv2. drawContours()、cv2. findContours()、cv2. Usage analysis of boundingrect (IMG) function
scanf、printf的输入输出(格式控制符)
Rookie's counter attack
Talk about anti reverse connection circuit in combination with practice (summary of anti reverse connection circuit)
Morphological operation of OpenCV image processing
Final Cut Pro mosaics in multiple places of the video at the same time
Junit常用注解
Nexus private server - (III) using private server warehouse in project practice
JS Chapter 12
EMO-DB 数据集的 Speech 特征提取
Pass files when openfeign is called
常见的测试方式
The urlcode of PHP cannot restore the original URL
style/TextAppearance. Compat. Notification. Info) not found.
ObjectBox使用记录
Nexus private server - (II) console installation of version 3.2.0, initial password location
One is based on Net core3 1's open source project helps you thoroughly understand the WPF framework prism
数据库第十三次作业 事务管理
Jackson