当前位置: 首页 > 知识库问答 >
问题:

从请求标头在侦探中添加traceId

莫骞仕
2023-03-14

我想在我的应用程序中使用spring cloud sleuth。对于每个请求,我都会在标头中获得“traceId”,我希望sleuth将其用作跟踪id。但问题是,sleuth仅在密钥为“X-B3-traceId”时才从标头中获取跟踪id。所以为了改变这一点,我使用了引用。但当我尝试构建应用程序时,我得到了以下结果:

SpringBootKafkaTestApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:658
            Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:185
                Caused by: java.lang.NullPointerException at HexCodec.java:26

在启动服务器时,我得到了以下信息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
 2021-06-20 21:56:17.541 ERROR             [,,${false}] ---- Application run failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kafkaTracing' defined in class path resource [org/springframework/cloud/sleuth/autoconfig/brave/instrument/messaging/BraveMessagingAutoConfiguration$SleuthKafkaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [brave.kafka.clients.KafkaTracing]: Factory method 'kafkaTracing' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.7.jar:5.3.7]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.7.jar:5.3.7]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438) [spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:337) [spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336) [spring-boot-2.5.0.jar:2.5.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325) [spring-boot-2.5.0.jar:2.5.0]
    at com.test.kafka.springbootkafkatest.SpringBootKafkaTestApplication.main(SpringBootKafkaTestApplication.java:13) [main/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_291]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_291]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_291]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_291]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.5.0.jar:2.5.0]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [brave.kafka.clients.KafkaTracing]: Factory method 'kafkaTracing' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.7.jar:5.3.7]
    ... 24 more
Caused by: java.lang.NullPointerException
    at brave.internal.codec.HexCodec.lowerHexToUnsignedLong(HexCodec.java:26) ~[brave-5.13.2.jar:?]
    at com.test.kafka.springbootkafkatest.Utils.CustomPropagator.lambda$extractor$1(CustomPropagator.java:32) ~[main/:?]
    at brave.kafka.clients.KafkaTracing.<init>(KafkaTracing.java:170) ~[brave-instrumentation-kafka-clients-5.13.2.jar:?]
    at brave.kafka.clients.KafkaTracing$Builder.build(KafkaTracing.java:133) ~[brave-instrumentation-kafka-clients-5.13.2.jar:?]
    at org.springframework.cloud.sleuth.autoconfig.brave.instrument.messaging.BraveMessagingAutoConfiguration$SleuthKafkaConfiguration.kafkaTracing(BraveMessagingAutoConfiguration.java:124) ~[spring-cloud-sleuth-autoconfigure-3.0.3.jar:3.0.3]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_291]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_291]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_291]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_291]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.7.jar:5.3.7]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.7.jar:5.3.7]
    ... 24 more

Process finished with exit code 0

我使用的是Spring boot版本--“2.5.0”和Spring cloud版本--“2020.0.3”,如有任何帮助,将不胜感激。

共有1个答案

何星鹏
2023-03-14

在提出问题之前,请务必阅读文档https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/howto.html#how-to-change-context-propagation

为了方便你,我正在从文件中复制代码

@Component
  class CustomPropagator extends Propagation.Factory implements Propagation<String> {

    @Override
    public List<String> keys() {
        return Arrays.asList("myCustomTraceId", "myCustomSpanId");
    }

    @Override
    public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) {
        return (traceContext, request) -> {
            setter.put(request, "myCustomTraceId", traceContext.traceIdString());
            setter.put(request, "myCustomSpanId", traceContext.spanIdString());
        };
    }

    @Override
    public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
        return request -> TraceContextOrSamplingFlags.create(TraceContext.newBuilder()
                .traceId(HexCodec.lowerHexToUnsignedLong(getter.get(request, "myCustomTraceId")))
                .spanId(HexCodec.lowerHexToUnsignedLong(getter.get(request, "myCustomSpanId"))).build());
    }

    @Override
    public <K> Propagation<K> create(KeyFactory<K> keyFactory) {
        return StringPropagationAdapter.create(this, keyFactory);
    }

  }
 类似资料:
  • 我有一个Spring Boot应用程序,它依赖于spring-cloud-starter-sleuth-3.0.3和spring-cloud-sleuth-zipkin-3.0.3。 我需要在从webclient调用API时将跟踪id传递给请求头。 示踪剂。currentSpan()为null,因此引发NPE。 根据文档,给出了将跟踪id添加到响应头的方法https://docs.spring.i

  • 我创建了一个基于wsdl文件的客户机。 这可能是因为web服务是用C#编写的,而且最有可能的是,它需要oasis头,在其中存储用户名和密码值。 我现在能创造的新环境是: 所需的enevelope如下:

  • 我使用的是Swagger 2.0和Swagger UI 3.0.3。 在我的api_定义中。yaml在我的路径之前有以下内容: 这将在Swagger UI中添加一个授权按钮,用户可以在其中粘贴他们的API密钥。我希望此API密钥在每个请求的请求标头中发送。但这不会发生,我不确定为什么。我错过了什么吗? 编辑: 请求似乎发出了,我回来了。 Chrome开发工具显示以下请求头: 我有路径设置为: 定义

  • 问题内容: 我正在与现有的servlet集成,该servlet从HTTP标头中提取一些属性。基本上,我实现的接口无法访问实际请求,它只能访问HTTP标头的k-> v映射。 我需要传递一个请求参数。计划是使用servlet过滤器从参数到标头值,但是HttpServletRequest对象当然没有addHeader()方法。 有任何想法吗? 问题答案: Extend ,覆盖标头吸气剂以返回参数: ..

  • 我正在使用kotlin开发一个android应用程序,在这条道路上,我想使用请求来响应restall服务器...现在看看我从服务器请求它的代码,它是真的,但我不知道如何将头的参数添加到我的请求

  • 问题内容: 我想将自定义标头添加到jQuery的AJAX POST请求中。 我已经试过了: 当我发送此请求并观看FireBug时,看到以下标头: 选项xxxx / yyyy HTTP / 1.1 主机:127.0.0.1:6666 用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko / 20100101 Firefox / 11.0 接受