【问题标题】:Spring boot microservice as Kafka consumer exceptionSpring Boot 微服务作为 Kafka 消费者异常
【发布时间】:2021-07-13 15:38:45
【问题描述】:

我正在尝试将 kafka 集成到我的 Spring Boot 项目中,但我遇到了一个异常。

2021-07-13T13:22:13.12+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.129+08:00","@version":"1","message":"Located property sour
igService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource ]}","logger_name":"org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration","thread_name":"main","level":"INFO"
   2021-07-13T13:22:13.14+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.148+08:00","@version":"1","message":"'cloud' property sour
ry.reconfiguration.CloudPropertySourceApplicationContextInitializer","thread_name":"main","level":"INFO","level_value":20000}
   2021-07-13T13:22:13.14+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.149+08:00","@version":"1","message":"Reconfiguration enabl
onfiguration.CloudServiceReconfigurationApplicationContextInitializer","thread_name":"main","level":"INFO","level_value":20000}
   2021-07-13T13:22:13.15+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.156+08:00","@version":"1","message":"Refreshing org.spring
ConfigApplicationContext@57e03347: startup date [Tue Jul 13 13:22:13 SGT 2021]; parent: org.springframework.boot.web.servlet.context.AnnotationCo
290dbf45","logger_name":"org.springframework.context.annotation.AnnotationConfigApplicationContext","thread_name":"main","level":"INFO","level_va
   2021-07-13T13:22:13.19+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.193+08:00","@version":"1","message":"Skipping reconfigurat
igured","logger_name":"org.cloudfoundry.reconfiguration.CloudServiceReconfigurationBeanFactoryPostProcessor","thread_name":"main","level":"INFO",
   2021-07-13T13:22:13.21+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.215+08:00","@version":"1","message":"JSR-330 'javax.inject
 for autowiring","logger_name":"org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor","thread_name":"main","level":"
   2021-07-13T13:22:13.41+0800 [APP/PROC/WEB/0] OUT {"@timestamp":"2021-07-13T13:22:13.410+08:00","@version":"1","message":"Exception encountered
ling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'binderHealthIndicator' def
m.binder.rabbit.config.RabbitServiceAutoConfiguration$RabbitHealthIndicatorConfiguration: Unsatisfied dependency expressed through method 'binder
ception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rabbitTemplate' defined in org.spring
nfig.RabbitServiceAutoConfiguration$CloudProfile$CloudConnectors$UseCloudConnectors: Unsatisfied dependency expressed through method 'rabbitTempl
rg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitConnectionFactory' defined in org.springframework.cl
erviceAutoConfiguration$CloudProfile$CloudConnectors$UseCloudConnectors: Bean instantiation via factory method failed; nested exception is org.sp
ception: Failed to instantiate [org.springframework.amqp.rabbit.connection.ConnectionFactory]: Factory method 'rabbitConnectionFactory' threw exc
ramework.cloud.CloudException: No unique service matching interface org.springframework.amqp.rabbit.connection.ConnectionFactory found. Expected
amework.context.annotation.AnnotationConfigApplicationContext","thread_name":"main","level":"WARN","level_value":30000}

pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>
        <dependency>
           <groupId>io.pivotal.spring.cloud</groupId>
           <artifactId>spring-cloud-services-starter-service-registry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-spring-service-connector</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
        </dependency>

知道我错过了什么吗?

这是我的参考教程 https://www.youtube.com/watch?v=xyaFygU9C2Q

谢谢!

【问题讨论】:

  • 在日志中可以看到,Spring无法注入rabbitTemplate,所以请创建一个bean并用@Bean注解
  • @karthikakinapelli 我很困惑抱歉,但我没有使用 rabbitmq。我正在使用卡夫卡。为什么要找rabbitmq?

标签: java spring spring-boot apache-kafka


【解决方案1】:

添加 rabbitTemplate 相关依赖并在您的配置类中创建 rabbitTemplate bean 然后执行 Autowireing。

如果不使用从你的代码中删除 rabbitTemplate 相关的配置。

【讨论】:

  • 嗨,我对此很陌生。你能举个例子吗?谢谢!
  • 嘿那个视频示例,发布事件到云。你也做同样的事情,如果是这样,你也可以添加 spring cloud 依赖项。
  • 如果您尝试使用 Spring Boot 学习 Kafka,请尝试在本地运行 Kafka 并使用一些主题并做生产者和消费者的事情。
  • 那里有一些很好的教程可以帮助你基本理解。 reflectoring.io/spring-boot-kafka
猜你喜欢
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 2018-04-06
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2019-08-25
相关资源
最近更新 更多