实现de 分布式链路追踪成了微服务的标配,随着opentracing标准的推出,jaeger+es几乎成了标配。它的架构如下
具体参考如下:https://www.jaegertracing.io/docs/1.38/architecture/,重要的组件如下:
version: "3"
services:
elasticsearch:
image: docker.io/elasticsearch:7.17.6
networks:
- elastic-jaeger
ports:
- "127.0.0.1:9200:9200"
- "127.0.0.1:9300:9300"
restart: on-failure
environment:
- cluster.name=jaeger-cluster
- discovery.type=single-node
- http.host=0.0.0.0
- transport.host=127.0.0.1
- ES_JAVA_OPTS=-Xms512m -Xmx512m
- xpack.security.enabled=false
volumes:
- ~/bilibili/live/learn/elasticsearch/esdata:/usr/share/elasticsearch/data
jaeger-collector:
image: jaegertracing/jaeger-collector
ports:
- "14269:14269"
- "14268:14268"
- "14267:14267"
- "9411:9411"
networks:
- elastic-jaeger
restart: on-failure
environment:
- SPAN_STORAGE_TYPE=elasticsearch
- METRICS_STORAGE_TYPE=prometheus
- PROMETHEUS_SERVER_URL=http://localhost:9090
command: [
"--es.server-urls=http://elasticsearch:9200",
"--es.num-shards=1",
"--es.num-replicas=0",
"--log-level=error"
]
depends_on:
- elasticsearch
jaeger-agent:
image: jaegertracing/jaeger-agent
hostname: jaeger-agent
command: ["--collector.host-port=jaeger-collector:14267"]
ports:
- "5775:5775/udp"
- "6831:6831/udp"
- "6832:6832/udp"
- "5778:5778"
networks:
- elastic-jaeger
restart: on-failure
environment:
- SPAN_STORAGE_TYPE=elasticsearch
- METRICS_STORAGE_TYPE=prometheus
- PROMETHEUS_SERVER_URL=http://localhost:9090
depends_on:
- jaeger-collector
jaeger-query:
image: jaegertracing/jaeger-query
environment:
- SPAN_STORAGE_TYPE=elasticsearch
- no_proxy=localhost
- METRICS_STORAGE_TYPE=prometheus
- PROMETHEUS_SERVER_URL=http://localhost:9090
ports:
- "16686:16686"
- "16687:16687"
networks:
- elastic-jaeger
restart: on-failure
command: [
"--es.server-urls=http://elasticsearch:9200",
"--span-storage.type=elasticsearch",
"--log-level=debug"
]
depends_on:
- jaeger-agent
volumes:
esdata:
driver: local
networks:
elastic-jaeger:
driver: bridge
使用depends_on进行容器排序时并不能完美的解决容器之间的依赖问题,原因是因为 depends_on只能保证容器进入到 运行状态而不是完全状态,依赖容器只能判断被容器是否启动,不能判断容器里的进程是否活跃,即没有k8s的探活机制,所以直接启动会报错。网上已经列出来了解决方法,使用 wait-for-it或者 wait-for。实现比较麻烦。所以需要单独先启动es,判断下是否启动成功,然后启动jaeger
% docker compose -f learn/elasticsearch/docker-compose-jager-es.yml up elasticsearch
% nc -v 127.0.0.1 9200
启动jager-all-in-one,由于docker for mac并不是直接在linux上运行的中间有一层x-hyve虚拟机,所以在duoker内部没法直接访问宿主机。解决办法是在在 Docker 20.10 及以上版本中,在 Docker 命令行中,添加:
--add-host=host.docker.internal:host-gateway
在 docker-compose 中,添加:
extra_hosts:
- host.docker.internal:host-gateway
即可通过 host.docker.internal 域名访问宿主机。此操作会在容器内部,添加一行/etc/hosts。实践发现在docker-compose中并不work,采用前一种方式。
docker run --name jaeger-es \
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
-e SPAN_STORAGE_TYPE=elasticsearch \
-e ES_SERVER_URLS=http://host.docker.internal:9200 \
-e METRICS_STORAGE_TYPE=prometheus \
-e PROMETHEUS_SERVER_URL=http://host.docker.internal:9090/prometheus \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 16685:16685 \
-p 14268:14268 \
-p 14250:14250 \
-p 9411:9411 \
--add-host=host.docker.internal:host-gateway \
jaegertracing/all-in-one:latest
测试下
% nc -v 127.0.0.1 16687
Connection to 127.0.0.1 port 16687 [tcp/*] succeeded!
部署完ageent、collector 和es后我们还需要在业务代码中引入jaeger客户端,做trace的传接和日志的采样上报。
https://pkg.go.dev/github.com/jaegertracing/jaeger-client-go@v2.30.0+incompatible
这个包由于不符合opentracing标准,已经不兼容详细可见官网
https://www.jaegertracing.io/docs/1.38/client-libraries/#deprecating-jaeger-clients
../../go/pkg/mod/github.com/jaegertracing/jaeger-client-go@v2.30.0+incompatible/zipkin_thrift_span.go:24:2: use of internal package github.com/uber/jaeger-client-go/internal/spanlog not allowed
解决方案是用uber的库替换
import "github.com/uber/jaeger-client-go"
❌ import "github.com/jaegertracing/jaeger-client-go"
部署完毕后测试下:
http://127.0.0.1:16686/
至此安装成功,关于client如何介入http和grpc服务,下一篇详细分析。
本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!