用seata-spring-boot-starter,要自己处理xid的传递问题吗?
当前依赖,全局事务XID,不需要手动进行绑定,自动进行传递
<dependency>
<groupId>com.alibaba.cloud</groupId>
<!–加入spring-cloud-alibaba-seata,解决xid不传递问题–>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
全局事务XID需要通过过滤器或拦截器进行手动绑定,否则下游服务获取不到全局XID回滚不了
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.3.0</version>
<!-- 这里需要排除自身的seata-all -->
<exclusions>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 导入与之前下载的seata版本一致的包 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.3.0</version>
</dependency>
OpenFeign进行手动传递XID
@Component
public class FeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("XID", RootContext.getXID());
}
}
提供者手动绑定XID
@Component
public class SeataFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
//手动绑定XID
String xid = request.getHeader("XID");
if(StringUtils.isNotBlank(xid)){
RootContext.bind(xid);
}
filterChain.doFilter(servletRequest,servletResponse);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。