在春季启动中创建KafkaTemplate的正确方法 [英] The correct way for creation of KafkaTemplate in spring boot

查看:50
本文介绍了在春季启动中创建KafkaTemplate的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 spring boot 应用程序中配置 apache kafka .我阅读了此

要解决此问题,我需要创建bean:

  @Bean公共KafkaTemplate< String,byte []>myMessageKafkaTemplate(){返回新的KafkaTemplate<>(greetingProducerFactory());} 

并传递给构造函数属性 greetingProducerFactory():

  @Beanpublic 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);返回新的DefaultKafkaProducerFactory<>(configProps);} 

但是如果我需要创建ProducerFactory手册,那么在 application.yam l中进行设置有什么意义呢?

解决方案

我认为您可以放心地忽略IDEA的警告;我在使用不同的通用类型的Boot模板中进行接线没有问题...

  @SpringBootApplication公共类So55280173Application {公共静态void main(String [] args){SpringApplication.run(So55280173Application.class,args);}@豆公共ApplicationRunner运行程序(KafkaTemplate< String,String>模板,Foo foo){返回参数->{template.send("so55280173","foo");如果(foo.template == template){System.out.println(它们相同");}};}@豆公共NewTopic topic(){返回新的NewTopic("so55280173",1 ,,(短)1);}}@零件Foo类{最终的KafkaTemplate< String,String>模板;@AutowiredFoo(KafkaTemplate< String,String>模板){this.template =模板;}} 

 它们是相同的 

I try configure apache kafka in spring boot application. I read this documentation and follow the steps:

1) I add this lines to aplication.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) I create new Topic:

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

And now I want use KafkaTemplate:

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

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

But Intellij IDE highlights:

To fix this I need create bean:

@Bean
public KafkaTemplate<String, byte[]> myMessageKafkaTemplate() {
    return new KafkaTemplate<>(greetingProducerFactory());
}

And pass to constructor propirties greetingProducerFactory():

@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);
}

But then what's the point of setting in application.yaml if I need create ProducerFactory manual?

解决方案

I think you can safely ignore IDEA's warning; I have no problems wiring in Boot's template with different generic types...

@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;
    }

}

and

they are the same

这篇关于在春季启动中创建KafkaTemplate的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆