[英]Spring Cloud Sleuth: Propagate traceId to other spring application
I have some spring service that can submit some AWS batch job. 我有一些Spring服务可以提交一些AWS批处理作业。 This is simple spring batch job that invoke requst to external service. 这是一个简单的spring批处理作业,它调用对外部服务的请求。 And i want to propagate traceId that generated in my service by including "org.springframework.cloud:spring-cloud-starter-sleuth" lib into classpath , to this job and add "TraceRestTemplateInterceptor" interceptor to external request initilaized with this traceId. 我想通过将“ org.springframework.cloud:spring-cloud-starter-sleuth” lib包含到classpath中,从而在服务中生成的traceId传播到此作业,并将“ TraceRestTemplateInterceptor”拦截器添加到由此traceId初始化的外部请求中。
How can i do that? 我怎样才能做到这一点? How can i initilaze interceptor which will put existing traceId from application parameter, environment, properties? 我如何初始化拦截器,该拦截器将从应用程序参数,环境,属性中放入现有的traceId? Or may be need to create some configuration beans? 还是可能需要创建一些配置bean?
UPDATE: 更新:
Simplified example: 简化示例:
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
Logger logger = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
//@Autowired
//RestTemplate restTemplate;
@Override
public void run(String... args) {
logger.info("Hello, world!");
//restTemplate.getForObject("some_url", String.class);
}
}
File application.properties: 文件application.properties:
x-b3-traceId=98519d97ce87553d
File build.gradle: 文件build.gradle:
dependencies {
implementation('org.springframework.cloud:spring-cloud-starter-sleuth')
}
Output: 输出:
INFO [-,,,] 15048 --- [ main] com.example.demo.DemoApplication : Hello, world!
First of all, I want to see here traceId which initilized in application.properties. 首先,我想在这里看到在application.properties中初始化的traceId。 Secondly, when uncomment resttemplate clause, this traceId propagated into request. 其次,当取消注释resttemplate子句时,此traceId传播到请求中。
Is it possible? 可能吗?
Resolved this issue only by manually putting into request HEADER key "X-B3-TRACEID" with corresponding value, which is inserted by external application as system property when submits target spring boot application. 仅通过手动将具有相应值的请求HEADER键“ X-B3-TRACEID”放入外部应用程序作为提交目标Spring Boot应用程序时的系统属性,即可解决此问题。 And manually inserting this key in MDC. 并手动将此密钥插入MDC。 Example, this snipet from spring boot application that must get traceId and propagate: 例如,来自Spring Boot应用程序的此代码段必须获取traceId并传播:
@Bean
public void setTraceIdToMDC(@Value("${x.b3.traceid}") String traceId) {
MDC.put("x-b3-traceId", traceId);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public CommandLineRunner commandLineRunnerer(RestTemplate restTemplate, @Value("${x.b3.traceid}") String traceId) {
return args -> {
MultiValueMap<String, String> header = new LinkedMultiValueMap<>();
header.add("X-B3-TRACEID", traceId);
HttpEntity httpEntity = new HttpEntity(header);
logger.info("Execute some request"); //<-- prints expected traceId
restTemplate.exchange("some_url", HttpMethod.GET, httpEntity, String.class);
};
}
Just add the dependency to the classpath and set rest template as a bean. 只需将依赖项添加到类路径并将rest模板设置为bean。 That's enough. 够了
You can get the bean: 您可以获取bean:
@Autowired private Tracer tracer;
And get the traceId with 并获得traceId与
tracer.getCurrentSpan().traceIdString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.