sleuth+zipkin请求URI路径参数问题(十)

问题背景

应用系统中controller代码如下

@RequestMapping("/test/{id}")
public String test(@PathVariable("id")Integer id) throws Exception {
		return providerClient.provider();
}

通过@PathVariable注解,在uri中携带参数,这种做法在业界也是很普遍的,假如有一个请求进来

http://localhost:7900/test/100

这个时候zipkin收集到的接口地址是 /test/100 , 存入数据库中的时候,span的name = “http:/test/100” , 这个在没有特殊需求的情况下没有问题,但是我规划的微服务链路追踪系统,是需要对接口的调用链,调用次数,性能,响应时间等数据进行分析的, 我需要分析的是/test/{id}这个接口,而不是/test/100这种接口。

问题

假如存在如下这种情况

@RequestMapping("/test/{id}")
public String test(@PathVariable("id")Integer id) throws Exception {
  return providerClient.provider();
}

@RequestMapping("/test/100")
public String tes1(Integer id) throws Exception {
  return providerClient.provider();
}

然后我们访问

http://localhost:7900/test/100

这个时候,是访问到那个方法呢? 是test 还是test1 ? 答案是访问到了test1 , spring mvc 首先是将uri执行进行比对,匹配上了则直接返回, 匹配不上才会进行正则匹配。 基于这个前提,接下来我们需要进行应用进行改造。

改造方案

1.基于sleuth1.2.6版本(之前的版本不允许重写这个Filter,其他版本只能改源码了),重写TraceFilter,因为提取URI是在这个Filter里面做的

下一节将把具体的改造方案的源码贴出来。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u012394095/article/details/82799163