- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mobicule</groupId>
<artifactId>spring-boot-kafka-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-kafka-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
KafkaConfiguration.java
package com.mobicule.springbootkafkaconsumer.config;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
@EnableKafka
@Configuration
public class KafkaConfiguration {
@Bean
public ConsumerFactory<String, String> consumerFactory()
{
Map<String, Object> config = new HashMap<String, Object>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return new DefaultKafkaConsumerFactory<String, String>(config);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaConsumerFactory()
{
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<String, String>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
KafkaConsumer.java
package com.mobicule.springbootkafkaconsumer.listener;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class KafkaConsumer {
@KafkaListener(topics="ConsumerDemo",groupId="group_id")
public void consume(String message)
{
System.out.println("In kafka consumer "+message);
}
}
我已经使用 Spring Initializer(没有服务器)配置了 Basic Kafka Producer 项目
1) 我使用终端生成了关于主题“ConsumerConfig”的消息“Hello World”
然后,当我执行我的消费者项目时,出现错误
The following candidates were found but could not be injected:
- Bean method 'kafkaConsumerFactory' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) found beans of type 'org.springframework.kafka.core.ConsumerFactory' consumerFactory
- User-defined bean method 'consumerFactory' in 'KafkaConfiguration'
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.kafka.core.ConsumerFactory' in your configuration.
所以我搜索了一下,找到了一个链接,上面写着 put@SpringBootApplication(exclude = KafkaAutoConfiguration.class)
即使在我的代码中加入了上面的内容之后,我还是得到了一个错误
说明:
A component required a bean named 'kafkaListenerContainerFactory' that could not be found.
Action:
Consider defining a bean named 'kafkaListenerContainerFactory' in your configuration.
我在这里错过了什么?我需要再进行配置吗?
最佳答案
如果您使用 Spring Boot,则不需要自定义 ConsumerFactory
,也不需要 ConcurrentKafkaListenerContainerFactory
。您应该完全依赖 spring.kafka
命名空间中的自动配置和适当的配置属性:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-kafka
如果您仍然坚持排除 KafkaAutoConfiguration
并手动执行所有操作,那么您确实需要使用请求的 kafkaListenerContainerFactory
bean 名称命名您的 ConcurrentKafkaListenerContainerFactory
: https://docs.spring.io/spring-kafka/docs/2.2.0.RELEASE/reference/html/_reference.html#kafka-listener-annotation
关于spring-boot - Spring Boot Kafka 消费者抛出没有名为 'kafkaListenerContainerFactory' 的 bean 可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319590/
嗨,我目前正在涉足 Spring Kafka,并成功向我的监听器添加了一个 KafkaListenerContainerFactory。现在我想添加多个 KafkaListenerContainerF
我是 kafka、springboot 的新手,正在尝试将 kafka 和 elasticsearch 集成到我的 springboot 应用程序中。 当我尝试运行 springboot 应用程序时,
在 spring-kafka 文档中,我们可以看到默认容器工厂假定可用,bean 名称为 kafkaListenerContainerFactory除非通过配置提供了明确的默认值。 我想问一下是否可以
pom.xml 4.0.0 com.mobicule spring-boot-kafka-consumer 0.0.1-SNAPSHOT jar s
我是一名优秀的程序员,十分优秀!