首页 > 其他分享 >Seata

Seata

时间:2022-12-10 18:23:04浏览次数:38  
标签:RootContext xid Seata request Request seataXid headers

 

 

Seata事务ID的传递

https://www.cnblogs.com/ciel717/p/16185061.html

  • 发送:SeataFeignClient替换了默认的feignClient,把xid放到了requestHeader里。

  SeataFeignClient.execute:塞到消息头

@Override
public Response execute(Request request, Request.Options options) throws IOException {
    //设置xid
    Request modifiedRequest = getModifyRequest(request);
    //调用下游服务
    return this.delegate.execute(modifiedRequest, options);
}

private Request getModifyRequest(Request request) {
    String xid = RootContext.getXID();
    if (StringUtils.isEmpty(xid)) {
        return request;
    }
    Map<String, Collection<String>> headers = new HashMap<>(MAP_SIZE);
    //设置xid到消息头
    headers.putAll(request.headers());
    List<String> seataXid = new ArrayList<>();
    seataXid.add(xid);
    headers.put(RootContext.KEY_XID, seataXid);
    return Request.create(request.method(), request.url(), headers, request.body(),request.charset());
}
  • 接收:拦截器实现

   SeataHandlerInterceptor.preHandle():通过WebMvcConfigure::addInterceptors中绑定该拦截器

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) {
    String xid = RootContext.getXID();
    //从消息头中获取xid
    String rpcXid = request.getHeader(RootContext.KEY_XID);
    if (log.isDebugEnabled()) {
        log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
    }
    if (xid == null && rpcXid != null) {
        // 设置xid
        RootContext.bind(rpcXid);
        if (log.isDebugEnabled()) {
            log.debug("bind {} to RootContext", rpcXid);
        }
    }
    return true;
}

 

 

 

@OverridepublicResponseexecute(Request request, Request.Options options) throws IOException { //设置xidRequest modifiedRequest = getModifyRequest(request); //调用下游服务returnthis.delegate.execute(modifiedRequest, options); } privateRequestgetModifyRequest(Request request) { String xid = RootContext.getXID(); if (StringUtils.isEmpty(xid)) { return request; } Map<String, Collection<String>> headers = newHashMap<>(MAP_SIZE); //设置xid到消息头 headers.putAll(request.headers()); List<String> seataXid = newArrayList<>(); seataXid.add(xid); headers.put(RootContext.KEY_XID, seataXid); returnRequest.create(request.method(), request.url(), headers, request.body(),request.charset()); }

标签:RootContext,xid,Seata,request,Request,seataXid,headers
From: https://www.cnblogs.com/clarino/p/16972033.html

相关文章

  • Centos7下Seata的安装和配置
    Seata是由阿里中间件团队发起的开源项目Fescar,后更名为Seata,它是一个是开源的分布式事务框架。传统2PC的问题在Seata中得到了解决,它通过对本地关系数据库的分支事务的协调......
  • 全网首发:Seata Saga状态机设计器实战
    前言目前业界公认Saga是作为长事务的解决方案。而seata作为目前最流行的分布式事物解决方案也提供了Saga的支持。而采用Seata的Saga模式进行事物控制,核心就是通过状态机来......
  • Seata TCC模式实战
    前言最近状态有点不好,所以创作动力不足,发觉日常生活一定要做减法,对少量的事保持持续专注的投入,养成良好的习惯。今天补充下,SeataTCC模式实战。一、TCC设计原则从TCC模型......
  • 还不会分布式事务,seata xa模式入门实战送上
    文章目录​​前言​​​​一、什么是seata?​​​​二、seata原理说明​​​​1、角色说明​​​​2、什么是Seata的事务模式?​​​​三、SEATA的分布式案例​​​​1、业......
  • 有来实验室|第一篇:Seata1.5.2版本部署和开源全栈商城订单支付业务实战
    在线体验:Seata实验室一.前言相信youlai-mall的实验室大家有曾在项目中见到过,但应该都还处于陌生的阶段,毕竟在此之前实验室多是以概念般的形式存在,所以我想借着此次......
  • 深入浅出Seata的AT模式
    单个掉队,导致集体被动摆烂;一、业务背景在分布式架构中,事务管理是个无法避开的复杂问题,虽然有多种解决方案,但是需要根据业务去选择合适的;从个人最近几年的实践经验来看,S......
  • SpringCloud Alibaba(六) - Seata 分布式事务锁
    1、Seata简介1.1Seata是什么Seata是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata将为用户提供了AT、TCC、SAGA和XA事务模式......
  • Winsw将jar-bat-nacos-seata-nginx等快捷部署为windows服务
    Winsw将jar-bat-nacos-seata-nginx等快捷部署为windows服务1.软件下载WinSW的github下载地址:https://github.com/winsw/winsw/releases本文以WinSWv2.11.0为例2.注意......
  • 深入浅出Seata的AT模式
    目录一、业务背景二、Seata架构1、核心组件2、AT模式三、案例分析1、流程分析2、写隔离3、读隔离四、对比XA模式五、参考源码单个掉队,导致集体被动摆烂;一、业务背景在......
  • 开源分布式事务中间件Seata使用指南
    介绍 Seata是阿里巴巴开源的分布式事务中间件,一种分布式事务解决方案,具有高性能和易于使用的微服务架构。 初衷对业务无侵入:即减少技术架构上的微服务化所带来的分布式事......