本文介绍了spring boot中创建KafkaTemplate的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我尝试在 spring boot 应用程序中配置 apache kafka.我读了这个 为了解决这个问题,我需要创建 bean:@Bean公共 KafkaTemplatemyMessageKafkaTemplate() {返回新的 KafkaTemplate<>(greetingProducerFactory());}并传递给构造函数属性 greetingProducerFactory():@Bean公共 ProducerFactory问候生产者工厂(){映射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.yaml 中设置有什么意义? 解决方案 我认为你可以放心地忽略 IDEA 的警告;我在使用不同泛型类型的 Boot 模板中接线没有问题...@SpringBootApplication公共类 So55280173Application {公共静态无效主(字符串 [] args){SpringApplication.run(So55280173Application.class, args);}@豆角,扁豆public ApplicationRunner runner(KafkaTemplate template, Foo foo) {返回参数 ->{template.send("so55280173", "foo");如果(foo.template == 模板){System.out.println("它们是一样的");}};}@豆角,扁豆公共新话题话题(){return new NewTopic("so55280173", 1, (short) 1);}}@成分类 Foo {最终 KafkaTemplate模板;@自动连线Foo(KafkaTemplate 模板) {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.ByteArraySerializer2) 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:@Beanpublic KafkaTemplate<String, byte[]> myMessageKafkaTemplate() { return new KafkaTemplate<>(greetingProducerFactory());}And pass to constructor propirties 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); 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...@SpringBootApplicationpublic 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); }}@Componentclass Foo { final KafkaTemplate<String, String> template; @Autowired Foo(KafkaTemplate<String, String> template) { this.template = template; }}andthey are the same 这篇关于spring boot中创建KafkaTemplate的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 23:41