jaegeropentracing的Java-client完整分布式追踪链
jaegeropentracing的Java-client完整分布式追踪链,在分布式系统中透传trace信息
之前文章记录了jaegeropentracing的Java-client追踪链在单系统中的调用示例,现在记录下在分布式系统是如何实现一个完整的调用链的.
这篇文章是基于我之前的两篇文章编写了,链接如下:
Spring整合CXF webservice restful 实例
下面是代码:
client端代码如下:
public static void main(String[] args) throws InterruptedException {
Configuration conf = new Configuration("EK Demo Jaeger."); //配置全局configuration
//发送sender configuration
Configuration.SenderConfiguration senderConf = new Configuration.SenderConfiguration();
senderConf.withAgentHost("192.168.1.111");
senderConf.withAgentPort(5775);
Sender sender = senderConf.getSender();
log.info("[ sender ] : "+sender);
conf.withReporter(
new Configuration.ReporterConfiguration()
.withSender(senderConf)
.withFlushInterval(100)
.withLogSpans(false)
);
conf.withSampler(
new Configuration.SamplerConfiguration()
.withType("const")
.withParam(1)
);
Tracer tracer = conf.getTracer();
log.info(tracer.toString());
GlobalTracer.register(tracer);
Tracer.SpanBuilder spanBuilder = GlobalTracer.get().buildSpan("EK Demo P");
Span parent = spanBuilder.start();
parent.log(100, "before Controller Method is running......");
log.info("before Controller Method is running......");
Tracer.SpanBuilder childB = GlobalTracer.get().buildSpan("EK Demo child").asChildOf(parent);
Span child = childB.start();
JaegerSpanContext context = (JaegerSpanContext) child.context();
child.log("......"+context.contextAsString());
String url = "http://localhost:8080/jeeek/services/phopuService/getUserPost";
HttpClient httpClient = HttpClients.createSystem();
final HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "text/plain");
StringEntity se = null;
String weatherInfo = null;
try {
//透传context到服务端
tracer.inject(parent.context(), Format.Builtin.TEXT_MAP, new TextMap() {
@Override
public Iterator<Map.Entry<String, String>> iterator() {
throw new UnsupportedOperationException("TextMapInjectAdapter should only be used with Tracer.inject()");
}
@Override
public void put(String key, String value) {
log.info(key+",----------------------- "+value);
httpPost.setHeader(key, value);
}
});
se = new StringEntity("101010500");
se.setContentType("text/plain");
httpPost.setEntity(se);
HttpResponse response = null;
response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode();
log.info("[接口返回状态吗] : " + status);
weatherInfo = getReturnStr(response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
log.info("[接口返回信息] : " + weatherInfo);
Thread.sleep(5000);
child.finish();
Thread.sleep(5000);
parent.finish();
log.info("after Controller Method is running.......");
Thread.sleep(10000);
}
服务端代码如下:
@POST
@Produces(MediaType.APPLICATION_JSON) //指定返回数据的类型 json字符串
//@Consumes(MediaType.TEXT_PLAIN) //指定请求数据的类型 文本字符串
@Path("/getUserPost")
public User getUserPost(String userId) {
this.logger.info("Call getUserPost() method...." + userId); Configuration conf = new Configuration("EK Demo Jaeger."); //配置全局configuration
//发送sender configuration
Configuration.SenderConfiguration senderConf = new Configuration.SenderConfiguration(); senderConf.withAgentHost("192.168.1.111");
//senderConf.withAgentHost("192.168.3.22");
senderConf.withAgentPort(5775); Sender sender = senderConf.getSender();
logger.info("[ sender ] : "+sender); conf.withReporter(
new Configuration.ReporterConfiguration()
.withSender(senderConf)
.withFlushInterval(100)
.withLogSpans(false)
); conf.withSampler(
new Configuration.SamplerConfiguration()
.withType("const")
.withParam(1)
); Tracer tracer = conf.getTracer();
logger.info(tracer.toString());
if (!GlobalTracer.isRegistered())
GlobalTracer.register(tracer); Tracer.SpanBuilder spanBuilder = tracer.buildSpan("server Span"); /**
* 由于此处只是一个restful接口,所以自己通过request获取头信息然后封装到map中,才作为参数传递
* 在实际的RPC分布式系统中,可以直接调用 request.getAttachments() 来返回头信息的trace信息
**/
//获取客户端透传的traceId,然后绑定span到该trace对应的span上
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String traceId = request.getHeader("uber-trace-id");//此处可以根据实际遍历header来获取,header的key有可能会发生变化[不确定]
Map<String, String> map = new HashMap<>();//将header信息放到map中
map.put("uber-trace-id", traceId);
logger.info("--------------------"+traceId);
try {
//new TextMapExtractAdapter(map)此处参数是个map,在分布式系统中直接调用request.getAttachments()
SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(map));
if (spanContext != null) {
spanBuilder.asChildOf(spanContext);
}
} catch (Exception e) {
spanBuilder.withTag("Error", "extract from request fail, error msg:" + e.getMessage());
} User user = new User();
user.setUserName("中文");
user.setAge(26);
user.setSex("m"); Span span = spanBuilder.start();
span.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
span.finish(); try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} return user;
}
在springMVC系统中手动获取request,需要配置web.xml,如下:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
当然,这只是一个demo,坐下简单记录,有问题可以留言交流。
jaegeropentracing的Java-client完整分布式追踪链的更多相关文章
- 2020-05-27:SpringCloud用了那些组件?分布式追踪链怎么做的?熔断器工作原理?
福哥答案2020-05-27: SpringCloud分布式开发五大组件详解服务发现——Netflix Eureka客服端负载均衡——Netflix Ribbon断路器——Netflix Hystri ...
- [业界方案]用Jaeger来学习分布式追踪系统Opentracing
[业界方案]用Jaeger来学习分布式追踪系统Opentracing 目录 [业界方案]用Jaeger来学习分布式追踪系统Opentracing 0x00 摘要 0x01 缘由 & 问题 1. ...
- 开源分布式追踪系统 — Jaeger介绍
目录 一.Jaeger是什么 二.Jaeger架构 1. 术语 2. 架构图 三.关于采样率 四.部署与实践 一.Jaeger是什么 Uber开发的一个受Dapper和Zipkin启发的分布式跟踪系统 ...
- [业界方案] 用SOFATracer学习分布式追踪系统Opentracing
[业界方案] 用SOFATracer学习分布式追踪系统Opentracing 目录 [业界方案] 用SOFATracer学习分布式追踪系统Opentracing 0x00 摘要 0x01 缘由 &am ...
- 【学习笔记】分布式追踪Tracing
在软件工程中,Tracing指使用特定的日志记录程序的执行信息,与之相近的还有两个概念,它们分别是Logging和Metrics. Logging:用于记录离散的事件,包含程序执行到某一点或某一阶段的 ...
- Uber分布式追踪系统Jaeger使用介绍和案例
原文:Uber分布式追踪系统Jaeger使用介绍和案例[PHP Hprose Go] 前言 随着公司的发展,业务不断增加,模块不断拆分,系统间业务调用变得越复杂,对定位线上故障带来很大困难.整个调 ...
- Jaeger Client Go 链路追踪|入门详解
目录 从何说起 Jaeger 部署 Jaeger 从示例了解 Jaeger Client Go 了解 trace.span tracer 配置 Sampler 配置 Reporter 配置 分布式系统 ...
- ASP.NET Core使用Jaeger实现分布式追踪
前言 最近我们公司的部分.NET Core的项目接入了Jaeger,也算是稍微完善了一下.NET团队的技术栈. 至于为什么选择Jaeger而不是Skywalking,这个问题我只能回答,大佬们说了算. ...
- OpenTracing:开放式分布式追踪规范
前言 想实现一个简单的追踪系统似乎是容易的,需要必要的调用链id,时间戳等:想实现一款易用不侵入代码的追踪系统就很麻烦了,需要接触CLR和IL相关知识:即使你费劲心力做出了那些,如果性能不够好,也没有 ...
随机推荐
- BJOI 2019 模拟赛 #2 题解
T1 完美塔防 有一些空地,一些障碍,一些炮台,一些反射镜 障碍会挡住炮台的炮, 反射镜可以 90° 反射炮台的光线,炮台可以选择打他所在的水平一条线或者竖直一条线 求是否有一组方案满足每个空地必须要 ...
- 每天一个linux命令(性能、优化):【转载】top命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- BZOJ1183 Croatian2008 Umnozak 【数位DP】*
BZOJ1183 Croatian2008 Umnozak Description 定义一个数的digit-product是它的各个位上的数字的乘积,定义一个数的self-product是它本身乘以它 ...
- Windows7 下python3和python2同时 安装python3和python2
1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...
- .NET中查看一个强命名程序集(*****.dll)的PublicKeyToken的方法
使用命令行工具SDK Command Prompt,键入:SN -T C:\*****.dll (dll文件所在的路径) 就会显示出该dll具体的PublicKeyToken数值. 如果该程序集没有 ...
- [转]50个很棒的Python模块
转自:http://www.cnblogs.com/foxhengxing/archive/2011/07/29/2120897.html Python具有强大的扩展能力,以下列出了50个很棒的Pyt ...
- Tencent Server Web(TSW) 腾讯开源的nodejs 基础设施
Tencent Server Web(TSW),是一套面向WEB前端开发者,以提升问题定位效率为初衷,提供染色抓包.全息日志和异常发现的Node.js基础设施.TSW关注业务的运维监控能力,适用于 ...
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
- Falcon
1. JE falcon还需要安装je用来处理jdbc,否则打不开falcon的页面,爆内部错误503,然后看异常信息:Caused by: org.apache.falcon.FalconExcep ...
- 在ubuntu下,进行php7源码安装
作为一名php的攻城师,如果没有玩php源码安装是说不过去的.我们知道php之所以这么流行,跟它的开源文化和lamp配套有很大关系.由于PHP7废弃了很多功能,所以一些依赖这些功能的程序可能无法运行, ...