Java 在spring boot中创建KafkaTemplate的正确方法

Java 在spring boot中创建KafkaTemplate的正确方法,java,spring,spring-boot,apache-kafka,spring-kafka,Java,Spring,Spring Boot,Apache Kafka,Spring Kafka,我尝试在spring boot应用程序中配置apachekafka。我阅读了这篇文章,并遵循以下步骤: 1) 我将这一行添加到application.yaml: spring: kafka: bootstrap-servers: kafka_host:9092 producer: key-serializer: org.apache.kafka.common.serialization.StringDeserializer value-serializ

我尝试在spring boot应用程序中配置apachekafka。我阅读了这篇文章,并遵循以下步骤:

1) 我将这一行添加到
application.yaml

spring:
  kafka:
    bootstrap-servers: kafka_host:9092
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringDeserializer
      value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
2) 我创建了一个新主题:

    @Bean
    public NewTopic responseTopic() {
        return new NewTopic("new-topic", 5, (short) 1);
    }
现在我想使用
KafkaTemplate

private final KafkaTemplate<String, byte[]> kafkaTemplate;

public KafkaEventBus(KafkaTemplate<String, byte[]> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
}

但是,如果我需要创建ProducerFactory手册,那么在应用程序.yaml中设置有什么意义呢?

默认情况下
KafkaTemplate
是由中的Spring Boot创建的。由于Spring在依赖项注入过程中考虑了泛型类型信息,所以默认bean不能自动连接到
KafkaTemplate
,我认为您可以安全地忽略IDEA的警告;我在Boot的模板中使用不同的泛型类型进行连接没有问题

@SpringBootApplication
public class So55280173Application {

    public static void main(String[] args) {
        SpringApplication.run(So55280173Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template, Foo foo) {
        return args -> {
            template.send("so55280173", "foo");
            if (foo.template == template) {
                System.out.println("they are the same");
            }
        };
    }

    @Bean
    public NewTopic topic() {
        return new NewTopic("so55280173", 1, (short) 1);
    }

}

@Component
class Foo {

    final KafkaTemplate<String, String> template;

    @Autowired
    Foo(KafkaTemplate<String, String> template) {
        this.template = template;
    }

}

最初我也遇到了同样的问题,但当我执行时,它没有出现错误,运行良好


忽略Intellij IDEA的警告,这可能是IDEA发现自动连线组件的错误。

我尝试注入不同的东西:
KafkaTemplate
,只需
KafkaTemplate
-在所有情况下,高亮显示的
KafkaTemplate
都是原始类型,这会对类产生副作用。最好使用
KafkaTemplate
KafkaTemplate
无法自动连线。找不到“KafkaTemplate”类型的bean。
您是否删除了自己的
myMessageKafkaTemplate
bean?我相信您可以忽略IDEA的警告-查看我的答案。我收到错误:
尝试发送与连线无关的消息时,未能构建kafka producer
;我建议您提出一个新问题,提供更多信息、堆栈跟踪等。我混淆了密钥的序列化器和反序列化器类,因此在构造kafka producer时出现错误
。现在它可以工作了,但IDEA仍然显示出警告(正因为如此,我不太理解,认为有必要手动创建)听起来IDEA比spring更严格:(
@Bean
public ProducerFactory<String, byte[]> greetingProducerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka_hist4:9092");
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}
@SpringBootApplication
public class So55280173Application {

    public static void main(String[] args) {
        SpringApplication.run(So55280173Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template, Foo foo) {
        return args -> {
            template.send("so55280173", "foo");
            if (foo.template == template) {
                System.out.println("they are the same");
            }
        };
    }

    @Bean
    public NewTopic topic() {
        return new NewTopic("so55280173", 1, (short) 1);
    }

}

@Component
class Foo {

    final KafkaTemplate<String, String> template;

    @Autowired
    Foo(KafkaTemplate<String, String> template) {
        this.template = template;
    }

}
they are the same