【问题标题】:Disable Spring Cloud Sleuth when running Integration Tests?运行集成测试时禁用 Spring Cloud Sleuth?
【发布时间】:2019-01-19 09:46:20
【问题描述】:

当使用 org.springframework.cloud:spring-cloud-gcp-starter-trace:1.0.0.RELEASE 并在本地运行我的集成测试时,我收到以下错误消息:

org.springframework.beans.factory.BeanCreationException: 错误 创建在类路径中定义的名称为“stackdriverSender”的bean 资源 [org/springframework/cloud/gcp/autoconfigure/trace/StackdriverTraceAutoConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [zipkin2.reporter.Sender]:工厂方法 'stackdriverSender' 抛出异常;嵌套异常是 java.io.IOException:应用程序默认凭据不是 可用的。如果在 Google Compute Engine 中运行,它们就可用。 否则,环境变量 GOOGLE_APPLICATION_CREDENTIALS 必须定义指向定义凭据的文件。看 https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。

这是完全可以理解的,因为此环境变量在本地不存在,并且我不想在运行测试时使用 Sleuth/Stackdriver 跟踪。我查看了reference documentation,但我似乎只能找到有关如何为特定集成点(如 RxJava、RestTemplate 等)禁用 Sleuth 的信息。但是如何完全禁用 Sleuth?

我尝试设置spring.sleuth.enabled=false,但这似乎没有任何区别。

【问题讨论】:

    标签: java spring-boot stackdriver spring-cloud-sleuth spring-cloud-gcp


    【解决方案1】:

    我实际上通过查看sourceStackdriverTraceAutoConfiguration 类找到了答案。如果使用 GCP 解决方法是设置spring.cloud.gcp.trace.enabled=false。这会禁用所有集成点的跟踪。

    【讨论】:

      【解决方案2】:

      操作方法:使用 Zipkin 禁用 Webflux Sleuth 实现的所有测试

      如果,你有这些依赖:

      implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
      implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
      

      然后,通过将 application.yml 添加到您的 test/resources 文件夹,为您的集成测试完全禁用 Sleuth/Zipkin:

      spring:
        sleuth: # All sleuth features disabled for integration tests
          enabled: false
          reactor:
            enabled: false
          web:
            enabled: false
          async:
            enabled: false
            client:
              template:
                enabled: false
        zipkin: # All zipkin features disabled for integration tests
          enabled: false
      

      【讨论】:

        【解决方案3】:

        您可以按如下方式禁用跟踪、日志记录和设置假项目 ID:

        spring.cloud.gcp.project-id=fake-project-id
        spring.cloud.gcp.logging.enabled=false
        spring.cloud.gcp.trace.enabled=false
        

        【讨论】:

          猜你喜欢
          • 2016-06-12
          • 2019-09-24
          • 2016-11-23
          • 2017-09-13
          • 1970-01-01
          • 1970-01-01
          • 2016-12-29
          • 2018-05-04
          • 2017-08-17
          相关资源
          最近更新 更多