【问题标题】:Spring cloud sleuth Annotations: Autowiring doesn't workSpring cloud sleuth 注解:自动装配不起作用
【发布时间】:2023-04-09 20:00:02
【问题描述】:

我在我的应用程序中配置了 Spring cloud sleuth(还有 zipkin)。我有一个调用服务的控制器,它又调用存储库,最后调用数据库。

设置工作正常,Sleuth 正在生成跨度 ID,它在 zipkin 中也可见。我想尝试跨多个内部 bean 和方法创建跨度。我遇到了Managing Spans with Annotations。这似乎不起作用。

当我使用此处提到的任何注释(例如 @NewSpan@ContinueSpan)时,自动装配停止工作。我在 Controller 中自动装配的服务类是 null。如果我删除这些注释,一切都会再次运行。

我正在使用。

spring-boot 2.2.5.RELEASE

spring-cloud.version Hoxton.SR3 我的 pom 中有这些依赖项

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-sleuth</artifactId> // This is pulling 2.2.2.RELEASE version
 </dependency>
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zipkin</artifactId>
 </dependency>

这是一个示例代码

@RestController
class SomeController {

@Autowired
SomeService someService;

    @GetMapping("/init")
    @NewSpan("init")
    private void init()  {
        someService.init();
    }
}

我的服务类就像

@Service
Class SomeService {
  .....

   @ContinueSpan(log = "init")
    public void init() {

    }
}

我的猜测是,Spring-Aop 与它有关。有什么想法吗?

【问题讨论】:

  • 你能提供一些额外的信息吗?您使用的是哪个版本的 Sleuth?你是如何使用注释的?
  • @MarcinGrzejszczak 更新了问题的详细信息

标签: spring-boot spring-cloud spring-cloud-sleuth


【解决方案1】:

请不要使用字段注入,使用构造函数注入。此外,那里的新跨度没有意义,因为您已经拥有由框架创建的新跨度。

【讨论】:

  • 即使我使用构造函数注入也不会改变。是的,我从控制器中删除了注释并在我的服务层中使用,我没有任何问题。任何想法,为什么它在控制器中不起作用?
  • 您很可能在代理内部,并且尝试使用代理没有的字段。你可以调用一个方法,也许这会起作用。无论如何这不是一个好主意,因为你已经有一个跨度。
猜你喜欢
  • 1970-01-01
  • 2019-06-04
  • 2014-09-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 2016-06-03
  • 1970-01-01
相关资源
最近更新 更多