我可以从以下 Spring Boot 控制器代码中看到跨度记录在 Zipkin UI 中:
@RestController
class ConcurrentController {
@Autowired
lateinit var restTemplate : RestTemplate
val urls = arrayListOf<String>("http://www.google.com","http://www.facebook.com")
private val logger = Logger.getLogger(this::class.java.getName())
@RequestMapping("/concurrent1")
fun endPoint1() : String {
logger.info("/concurrent1")
var s = ""
runBlocking {
val a = urls.map { url ->
logger.info(url)
async(CommonPool) {
logger.info("getting $url")
restTemplate.getForObject(url, String::class.java)
sleep(5000)
logger.info("got $url")
}
}
val b = a.map { it.await() }
s = b.joinToString { "" }
}
return s
}
}
日志输出如下所示:
2017-10-30 20:56:04.525 INFO [coroutinesDemo,eb6fd4fedb6f3a6d,eb6fd4fedb6f3a6d,true] 13547 --- [nio-8080-exec-1] c.e.coroutines.ConcurrentController : /concurrent1
2017-10-30 20:56:04.543 INFO [coroutinesDemo,eb6fd4fedb6f3a6d,eb6fd4fedb6f3a6d,true] 13547 --- [nio-8080-exec-1] c.e.coroutines.ConcurrentController : http://www.google.com
2017-10-30 20:56:04.548 INFO [coroutinesDemo,eb6fd4fedb6f3a6d,eb6fd4fedb6f3a6d,true] 13547 --- [nio-8080-exec-1] c.e.coroutines.ConcurrentController : http://www.facebook.com
2017-10-30 20:56:04.548 INFO [coroutinesDemo,,,] 13547 --- [onPool-worker-9] c.e.coroutines.ConcurrentController : getting http://www.google.com
2017-10-30 20:56:04.549 INFO [coroutinesDemo,,,] 13547 --- [onPool-worker-2] c.e.coroutines.ConcurrentController : getting http://www.facebook.com
2017-10-30 20:56:09.703 INFO [coroutinesDemo,,,] 13547 --- [onPool-worker-2] c.e.coroutines.ConcurrentController : got http://www.facebook.com
2017-10-30 20:56:09.703 INFO [coroutinesDemo,,,] 13547 --- [onPool-worker-9] c.e.coroutines.ConcurrentController : got http://www.google.com
但跟踪在 UI 中是独立的。
我希望对 Google 和 Facebook url 的两个调用同时嵌套在对/concurrent1
端点的调用下。
我怀疑这是由于执行协程的线程与启动 Spring 应用程序的线程不同,但我现在不知道如何继续使用 Spring Sleuth!