获取xid
SeataFilter
filter方式
@Component
public class SeataFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) servletRequest;// 获取xidString xid = req.getHeader(RootContext.KEY_XID.toLowerCase());boolean isBind = false;if (StringUtils.isNotBlank(xid)) {RootContext.bind(xid);isBind = true;}try {filterChain.doFilter(servletRequest, servletResponse);} finally {if (isBind) {RootContext.unbind(); // 释放}}}@Overridepublic void destroy() {}
}
spring mvc Interceptor
public class TransactionPropagationInterceptor extends HandlerInterceptorAdapter {private static final Logger LOGGER = LoggerFactory.getLogger(TransactionPropagationInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {String xid = RootContext.getXID();String rpcXid = request.getHeader(RootContext.KEY_XID);if (LOGGER.isDebugEnabled()) {LOGGER.debug("xid in RootContext[{}] xid in HttpContext[{}]", xid, rpcXid);}if (xid == null && rpcXid != null) {RootContext.bind(rpcXid);if (LOGGER.isDebugEnabled()) {LOGGER.debug("bind[{}] to RootContext", rpcXid);}}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) {if (RootContext.inGlobalTransaction()) {XidResource.cleanXid(request.getHeader(RootContext.KEY_XID));}}}
GlobalTransactionEvent
public GlobalTransactionEvent(long id, String role, String name, String applicationId,String group, Long beginTime, Long endTime, GlobalStatus status) {this.id = id;this.role = role;this.name = name;this.applicationId = applicationId;this.group = group;this.beginTime = beginTime;this.endTime = endTime;this.status = status;}