1

我从spring-cloud-sleuth-core找到restTemplateInterceptor和feignRequestInterceptor,但是我们的项目使用hessian connect微服务,我发现spring-cloud-sleuth不能注入到hessian客户端。任何人都可以分享如何将 spring-cloud-sleuth 与 hessian 一起使用的代码吗?谢谢~

4

2 回答 2

1

获取spring_context的trace,从trace的bean中创建一个span,然后通过重写hessianProxy将span的信息添加到http头中,就可以了。</p>

提供演示

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;
}
}
于 2019-01-13T12:34:25.960 回答
0

hessian 客户端没有默认的 spring-cloud-sleuth 注入,因此,我必须在 hessian 客户端周围制作 2 个钩子,将添加 zipkin 通知代码添加到钩子中。

于 2016-07-12T07:01:29.663 回答