b]安装运行
在这篇文章中使用的所有源代码可在Github上 。 假设你有一个本地安装的Go 1.7,以及一个运行Docker,我们将用来运行Zipkin服务器。
该演示项目使用glide进行依赖管理,请先安装它。 例如,在Mac OS上,您可以:
$ brew install glide
然后获取演示代码,构建并运行它:
$ go get github.com/yurishkuro/opentracing-go-nethttp-demo $ cd $GOPATH/src/github.com/yurishkuro/opentracing-go-nethttp-demo $ glide install <strong>... lots of output ...</strong> $ go build . $ ./opentracing-go-nethttp-demo 2016/10/06 00:10:54 Starting server on port 8000
|
现在在另一个终端,让我们开始Zipkin服务器
$ docker run -d -p 9410-9411:9410-9411 openzipkin/zipkin:1.12.0 Unable to find image 'openzipkin/zipkin:1.12.0' locally 1.12.0: Pulling from openzipkin/zipkin 4d06f2521e4f: Already exists 93bf0c6c4f8d: Already exists a3ed95caeb02: Pull complete 3db054dce565: Pull complete 9cc214bea7a6: Pull complete Digest: sha256:bf60e4b0ba064b3fe08951d5476bf08f38553322d6f640d657b1f798b6b87c40 Status: Downloaded newer image for openzipkin/zipkin:1.12.0 da9353ac890e0c0b492ff4f52ff13a0dd12826a0b861a67cb044f5764195e005
|
如果你没有Docker,另一种运行Zipkin服务器的方法是直接从jar开始:
$ wget -O zipkin.jar'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
$ java -jar zipkin.jar
打开UI:
http://localhost:9411/
请注意,屏幕UI界面中服务名称下拉列表为空,因为我们还没有生成任何跟踪。
现在打开另一个终端并运行:
$ cd $GOPATH/src/github.com/yurishkuro/opentracing-go-nethttp-demo
$ ./opentracing-go-nethttp-demo -actor client
Received result: The time is Thu Oct 6 00:23:22 2016
如果您重新加载UI页面,您应该会在第一个下拉列表中看到“客户端”。
。。。。
补充,之前提到跟踪变量初始化问题,tracer是是OpenTracing API的实际实现。 在我的例子用了https://github.com/uber/jaeger-client-go ,这是来自Jaeger的开源项目,是Uber的分布式跟踪系统的与OpenTracing兼容的客户端库。我们计划在2016年年底之前开源后端和UI。这个例子中的主程序看起来像这样:
package main
import ( "flag" "log"
"github.com/uber/jaeger-client-go" "github.com/uber/jaeger-client-go/transport/zipkin" )
var ( zipkinURL = flag.String("url", "http://localhost:9411/api/v1/spans", "Zipkin server URL") serverPort = flag.String("port", "8000", "server port") actorKind = flag.String("actor", "server", "server or client") )
const ( server = "server" client = "client" )
func main() { flag.Parse()
if *actorKind != server && *actorKind != client { log.Fatal("Please specify '-actor server' or '-actor client'") }
// Jaeger tracer can be initialized with a transport that will // report tracing Spans to a Zipkin backend transport, err := zipkin.NewHTTPTransport( *zipkinURL, zipkin.HTTPBatchSize(1), zipkin.HTTPLogger(jaeger.StdLogger), ) if err != nil { log.Fatalf("Cannot initialize HTTP transport: %v", err) } // create Jaeger tracer tracer, closer := jaeger.NewTracer( *actorKind, jaeger.NewConstSampler(true), // sample all traces jaeger.NewRemoteReporter(transport, nil), )
if *actorKind == server { runServer(tracer) return }
runClient(tracer)
// Close the tracer to guarantee that all spans that could // be still buffered in memory are sent to the tracing backend closer.Close() }
|
Jaeger跟踪系统本身不同于Zipkin,但是我们目前使用的是Zipkin兼容的Thrift数据模型,它允许我使用Zipkin服务器来显示跟踪。
结论
我们使用几个组件的组合来说明跨过进程边界HTTP请求的简单跟踪,并深入了解实际的HTTP请求执行:
1.标准的net/http/httptrace package,
2.https://github.com/opentracing-contrib/go-stdlib instrumentation library,
3.Jaeger Go client https://github.com/uber/jaeger-client-go that implements OpenTracing API, and
4.Zipkin server and UI from https://github.com/openzipkin/zipkin.
本案例演示源码:https://github.com/yurishkuro/opentracing-go-nethttp-demo.