spring-cloud-sleuth trace hessian客户端

时间:2016-07-01 02:35:07

标签: hessian spring-cloud-sleuth

我从spring-cloud-sleuth-core中找到restTemplateInterceptor和feignRequestInterceptor,但我们的项目使用了hessian连接微服务,我发现spring-cloud-sleuth无法注入到粗麻布客户端。 任何人都可以分享如何使用spring-cloud-sleuth与粗麻布的代码?感谢〜

2 个答案:

答案 0 :(得分:0)

对于粗麻布客户端没有默认的spring-cloud-sleuth注入,所以,我必须在hessian客户端周围做2个钩子,添加zip zipkin通知代码到钩子。

答案 1 :(得分:0)

获取spring_context的跟踪,从trace的bean生成一个跨度,然后通过重写hessianProxy将跨度的信息添加到http标头中,它将起作用。

提供演示

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.caucho.hessian.client.HessianConnection;
import com.caucho.hessian.client.HessianProxyFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.instrument.web.TraceFilter;
import org.springframework.cloud.sleuth.util.SpanNameUtil;

import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author yezhi.li
 * @date 19-1-3 下午2:41
 */
public class MyHessianProxy extends com.caucho.hessian.client.HessianProxy {

//create function new proxy
private URL urlLocal =null;

protected static final String TRACE_REQUEST_ATTR = TraceFilter.class.getName()
        + ".TRACE";

private static final long serialVersionUID = 6732486042199684754L;

private Logger log = LoggerFactory.getLogger(MyHessianProxy.class);

protected MyHessianProxy(URL url, HessianProxyFactory factory) {
    super(url, factory);
    this.urlLocal =url;
}

protected MyHessianProxy(URL url, HessianProxyFactory factory, Class<?> type) {
    super(url, factory, type);
    this.urlLocal =url;
}

@Override
protected void addRequestHeaders(HessianConnection conn)
{


    Tracer tracer = SpringUtil.getBean(Tracer.class);
    String spanName = null;
    try {
        spanName = getName(urlLocal.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    Span newSpan = tracer.createSpan(spanName,trace.getcurrentSpan());
    inject(newSpan ,conn);
    //detach span
    tracer.detach(newSpan);
}



private String getName(URI uri) {
    // The returned name should comply with RFC 882 - Section 3.1.2.
    // i.e Header values must composed of printable ASCII values.
    return SpanNameUtil.shorten(uriScheme(uri) + ":" + uri.getRawPath());
}

private String uriScheme(URI uri) {
    return uri.getScheme() == null ? "http" : uri.getScheme();
}

public void inject(Span span,HessianConnection conn){
    setHeader(conn,   Span.TRACE_ID_NAME, span.traceIdString());
    setIdHeader(conn,   Span.SPAN_ID_NAME, span.getSpanId());
    setHeader(conn,   Span.SAMPLED_NAME, span.isExportable() ? Span.SPAN_SAMPLED : Span.SPAN_NOT_SAMPLED);
    setHeader(conn,   Span.SPAN_NAME_NAME, span.getName());
    setIdHeader(conn,   Span.PARENT_ID_NAME, getParentId(span));
    setHeader(conn,   Span.PROCESS_ID_NAME, span.getProcessId());
    for (Map.Entry<String, String> entry : span.baggageItems()) {
        conn.addHeader(prefixedKey(entry.getKey()), entry.getValue());
    }
}

private void setIdHeader(HessianConnection conn, String name, Long value) {
    if (value != null) {
        setHeader(conn,  name, Span.idToHex(value));
    }
}

private void setHeader(HessianConnection conn,String key,String value){
    conn.addHeader(key,value);
}

private Long getParentId(Span span) {
    return !span.getParents().isEmpty() ? span.getParents().get(0) : null;
}

private String prefixedKey(String key) {
    if (key.startsWith(Span.SPAN_BAGGAGE_HEADER_PREFIX
            + "-")) {
        return key;
    }
    return Span.SPAN_BAGGAGE_HEADER_PREFIX + "-"
            + key;
}
}