2019独角兽企业重金招聘Python工程师标准>>>
官网地址:
https://github.com/openzipkin/zipkin
http://zipkin.io/
https://www.oschina.net/p/zipkin
截止到2017/1/4为止,最新版本为: Zipkin 1.19
下载地址: https://github.com/openzipkin/zipkin/archive/1.19.0.tar.gz
---
下载后,上传到我的linux上,解压缩,大概看了下,去掉一些不需要的代码块,java文件数也不是很多。
接下来开始编译,根据官网的说明,http://zipkin.io/pages/quickstart
./mvnw -DskipTests --also-make -pl zipkin-server clean install
网速太慢。。。
直接上https://search.maven.org/#search%7Cga%7C1%7Cio.zipkin.java%20zipkin-server下载现成的jar包下来。
java -jar ....jar启动
然后找了这样一篇文章
package brave;import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.ClientRequestAdapter;
import com.github.kristofa.brave.ClientRequestInterceptor;
import com.github.kristofa.brave.ClientResponseAdapter;
import com.github.kristofa.brave.ClientResponseInterceptor;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.KeyValueAnnotation;
import com.github.kristofa.brave.ServerRequestAdapter;
import com.github.kristofa.brave.ServerRequestInterceptor;
import com.github.kristofa.brave.ServerResponseAdapter;
import com.github.kristofa.brave.ServerResponseInterceptor;
import com.github.kristofa.brave.SpanId;
import com.github.kristofa.brave.TraceData;
import com.github.kristofa.brave.http.HttpSpanCollector;
import com.twitter.zipkin.gen.Endpoint;//https://my.oschina.net/u/223522/blog/736852-一个例子
//http://www.tuicool.com/articles/f2qAZnZ-servlet
public class Test {private static HttpSpanCollector collector = null;private static Brave brave = null;private static Brave brave2 = null;private static void braveInit(){collector = HttpSpanCollector.create("http://102.45.78.213:9411/", new EmptySpanCollectorMetricsHandler());brave = new Brave.Builder("appserver").spanCollector(collector).build();brave2 = new Brave.Builder("datacenter").spanCollector(collector).build();}static class Task {String name;SpanId spanId;public Task(String name, SpanId spanId) {super();this.name = name;this.spanId = spanId;}}public static void main(String[] args) throws Exception {braveInit();final BlockingQueue<Task> queue = new ArrayBlockingQueue<Task>(10);Thread thread = new Thread(){public void run() {while (true) {try {Task task = queue.take();dcHandle(task.name, task.spanId);} catch (Exception e) {e.printStackTrace();}}}};thread.start();{ServerRequestInterceptor serverRequestInterceptor = brave.serverRequestInterceptor();ServerResponseInterceptor serverResponseInterceptor = brave.serverResponseInterceptor();ClientRequestInterceptor clientRequestInterceptor = brave.clientRequestInterceptor();ClientResponseInterceptor clientResponseInterceptor = brave.clientResponseInterceptor();serverRequestInterceptor.handle(new ServerRequestAdapterImpl("group_data"));ClientRequestAdapterImpl clientRequestAdapterImpl = new ClientRequestAdapterImpl("get_radio_list");clientRequestInterceptor.handle(clientRequestAdapterImpl);queue.offer(new Task("get_radio_list", clientRequestAdapterImpl.getSpanId()));Thread.sleep(10);clientResponseInterceptor.handle(new ClientResponseAdapterImpl());clientRequestAdapterImpl = new ClientRequestAdapterImpl("get_user_list");clientRequestInterceptor.handle(clientRequestAdapterImpl);queue.offer(new Task("get_user_list", clientRequestAdapterImpl.getSpanId()));Thread.sleep(10);clientResponseInterceptor.handle(new ClientResponseAdapterImpl());clientRequestAdapterImpl = new ClientRequestAdapterImpl("get_program_list");clientRequestInterceptor.handle(clientRequestAdapterImpl);queue.offer(new Task("get_program_list", clientRequestAdapterImpl.getSpanId()));Thread.sleep(10);clientResponseInterceptor.handle(new ClientResponseAdapterImpl());serverResponseInterceptor.handle(new ServerResponseAdapterImpl());}Thread.sleep(3000);}public static void dcHandle(String spanName, SpanId spanId){ServerRequestInterceptor serverRequestInterceptor = brave2.serverRequestInterceptor();ServerResponseInterceptor serverResponseInterceptor = brave2.serverResponseInterceptor();serverRequestInterceptor.handle(new ServerRequestAdapterImpl(spanName, spanId));serverResponseInterceptor.handle(new ServerResponseAdapterImpl());}static class ServerRequestAdapterImpl implements ServerRequestAdapter {Random randomGenerator = new Random();SpanId spanId;String spanName;ServerRequestAdapterImpl(String spanName){this.spanName = spanName;long startId = randomGenerator.nextLong();SpanId spanId = SpanId.builder().spanId(startId).traceId(startId).parentId(startId).build();this.spanId = spanId;}ServerRequestAdapterImpl(String spanName, SpanId spanId){this.spanName = spanName;this.spanId = spanId;}@Overridepublic TraceData getTraceData() {if (this.spanId != null) {return TraceData.builder().spanId(this.spanId).build();}long startId = randomGenerator.nextLong();SpanId spanId = SpanId.builder().spanId(startId).traceId(startId).parentId(startId).build();return TraceData.builder().spanId(spanId).build();}@Overridepublic String getSpanName() {return spanName;}@Overridepublic Collection<KeyValueAnnotation> requestAnnotations() {Collection<KeyValueAnnotation> collection = new ArrayList<KeyValueAnnotation>();KeyValueAnnotation kv = KeyValueAnnotation.create("radioid", "165646485468486364");collection.add(kv);return collection;}}static class ServerResponseAdapterImpl implements ServerResponseAdapter {@Overridepublic Collection<KeyValueAnnotation> responseAnnotations() {Collection<KeyValueAnnotation> collection = new ArrayList<KeyValueAnnotation>();KeyValueAnnotation kv = KeyValueAnnotation.create("radioid", "165646485468486364");collection.add(kv);return collection;}}static class ClientRequestAdapterImpl implements ClientRequestAdapter {String spanName;SpanId spanId;ClientRequestAdapterImpl(String spanName){this.spanName = spanName;}public SpanId getSpanId() {return spanId;}@Overridepublic String getSpanName() {return this.spanName;}@Overridepublic void addSpanIdToRequest(SpanId spanId) {//记录传输到远程服务System.out.println(spanId);if (spanId != null) {this.spanId = spanId;System.out.println(String.format("trace_id=%s, parent_id=%s, span_id=%s", spanId.traceId, spanId.parentId, spanId.spanId));}}@Overridepublic Collection<KeyValueAnnotation> requestAnnotations() {Collection<KeyValueAnnotation> collection = new ArrayList<KeyValueAnnotation>();KeyValueAnnotation kv = KeyValueAnnotation.create("radioid", "165646485468486364");collection.add(kv);return collection;}@Overridepublic Endpoint serverAddress() {return null;}}static class ClientResponseAdapterImpl implements ClientResponseAdapter {@Overridepublic Collection<KeyValueAnnotation> responseAnnotations() {Collection<KeyValueAnnotation> collection = new ArrayList<KeyValueAnnotation>();KeyValueAnnotation kv = KeyValueAnnotation.create("radioname", "火星人1");collection.add(kv);return collection;}}
}
maven文件如下:
<dependencies><dependency><groupId>io.zipkin.brave</groupId><artifactId>brave-spancollector-http</artifactId><version>3.9.0</version></dependency></dependencies>
查看上报的效果