全局事務ID在多應用中遠程傳遞

代碼原理來自seata的分佈式事務中,全局事務ID在多應用中的傳遞:

https://blog.csdn.net/f4761/article/details/89077400

https://blog.csdn.net/yunqiinsight/article/details/88343096

 

 

如果是HTTP協議,多應用直接無代碼侵入,生成全局唯一ID,並且在遠程調用時,此ID在多個應用間傳遞:

1、發送http請求時,進行攔截,並且之前在生成ID時,將ID放入ThreadLocal中,攔截器直接取ID,然後放入http的header中

public class FescarRestTemplateInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
            ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest);
 
        String xid = RootContext.getXID();
 
        if (!StringUtils.isEmpty(xid)) {
            requestWrapper.getHeaders().add(RootContext.KEY_XID, xid);
        }
        return clientHttpRequestExecution.execute(requestWrapper, bytes);
    }
}

 

2、接收請求方,攔截http請求,並且獲取header中的ID,然後放入ThreadLocal中,然後在之前的業務中就可以使用該ID了,業務過程中不需要在請求時,進行代碼改造,然後進行傳遞

/**
 *
 * Fescar HandlerInterceptor, Convert Fescar information into
 * @see com.alibaba.fescar.core.context.RootContext from http request's header in
 * {@link org.springframework.web.servlet.HandlerInterceptor#preHandle(HttpServletRequest , HttpServletResponse , Object )},
 * And clean up Fescar information after servlet method invocation in
 * {@link org.springframework.web.servlet.HandlerInterceptor#afterCompletion(HttpServletRequest, HttpServletResponse, Object, Exception)}
 */
public class FescarHandlerInterceptor implements HandlerInterceptor {
 
    private static final Logger log = LoggerFactory
            .getLogger(FescarHandlerInterceptor.class);
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception {
 
        String xid = RootContext.getXID();
        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) {
            RootContext.bind(rpcXid);
            if (log.isDebugEnabled()) {
                log.debug("bind {} to RootContext", rpcXid);
            }
        }
        return true;
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception e) throws Exception {
 
        String rpcXid = request.getHeader(RootContext.KEY_XID);
 
        if (StringUtils.isEmpty(rpcXid)) {
            return;
        }
 
        String unbindXid = RootContext.unbind();
        if (log.isDebugEnabled()) {
            log.debug("unbind {} from RootContext", unbindXid);
        }
        if (!rpcXid.equalsIgnoreCase(unbindXid)) {
            log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid);
            if (unbindXid != null) {
                RootContext.bind(unbindXid);
                log.warn("bind {} back to RootContext", unbindXid);
            }
        }
    }
 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章