分布式追踪trace异常问题
我部署了一个简单的微服务系统,这个系统的拓扑图如下所示

然后我使用一个分布式追踪系统jaeger对其进行链路追踪,由于我配置了istio,并对这个系统所涉及到的所有pod都安装了sidecar,所以无需对原有代码进行操作即可进行分布式追踪,但是现在问题是一个请求会形成多个trace

看了下代码,可能是异步操作的问题
public CustomResult addOrder(@RequestBody OrderDTO orderDTO) {
String orderContent = orderDTO.getOrderContent();
String userId = orderDTO.getOrderUserId();
String price = orderDTO.getOrderPrice();
// 首先请求库存服务来确认库存
// String reserveUrl = "http://localhost:8004/api/reserve/getReserve?orderContent=" + orderContent;
String reserveUrl = "http://reserve.demo.svc.cluster.local/api/reserve/getReserve?orderContent=" + orderContent;
CustomResult reserveResult = restTemplate.getForObject(reserveUrl, CustomResult.class);
// 检查库存服务的响应
if (reserveResult != null && reserveResult.getStatus() == 20000) {
// 库存确认后请求支付服务 Post请求
String payUrl = "http://pay.demo.svc.cluster.local/api/pay/topay";
PayDTO payDTO = new PayDTO(userId,price);
CompletableFuture<CustomResult> payResultFuture = CompletableFuture.supplyAsync(() -> {
return restTemplate.postForObject(payUrl, payDTO, CustomResult.class);
});
// 请求支付服务的响应
CustomResult payResult = null;
try {
payResult = payResultFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
// 检查支付服务的响应
if (payResult != null && payResult.getStatus() == 20000) {
return new CustomResult(20000, "下单成功", null);
} else {
return new CustomResult(40000, "支付失败", null);
}
} else {
return new CustomResult(40000, "库存不足", null);
}
}
但是在reserve服务请求下面的notification、delivery的时候我使用的是同步操作,但是这还是会形成两个trace
@GetMapping("/getReserve")
public CustomResult getReserve(@RequestParam String orderContent) {
// 请求通知服务
String noticeUrl = "http://notification.demo.svc.cluster.local/api/notification/toNotify";
restTemplate.getForObject(noticeUrl,String.class);
// 请求配送服务 post请求
DeliveryDTO deliveryDTO = new DeliveryDTO("北京","北京","海淀","中关村南四街","中国科学院软件研究所" ,"粥隔离","18888888888");
String deliveryUrl = "http://delivery.demo.svc.cluster.local/api/delivery/toDelivery";
restTemplate.postForObject(deliveryUrl,deliveryDTO,String.class);
return new CustomResult(20000, "库存充足", null);
}

按理来说这本来应该是一个trace,但是为什么却出现了多个trace呢?
请各位big brother们分析下给出具体的原因解释以及问题的解决方案,谢谢!