kafka通过@Bean使用NewTopic创建Topic报错
报错
- 读取配置,创建指定分区数和副本数的topic
- 项目启动时报错:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'radar.group' available
Description:
A component required a bean named 'radar.group' that could not be found.
Action:
Consider defining a bean named 'radar.group' in your configuration.
分析与解决
@Bean
public NewTopic radar(){
return new NewTopic(topic, patitions, replications);
}
- 在linux窗口,使用命令kafka-topics --list --bootstrap-server localhost:9092查看kafka现有的topic,发现确实没有创建成功
- 再结合报错里的'radar.group',我想可能是我的bean名字的问题。
- 我创建的topic名字是radar,就没多想方法名顺手写了radar,没显式声明时,方法名就是bean名字,这样是不行的,kafka无法去使用radar
- 将方法名改成topic,顺利启动,topic也创建成功了
@Bean
public NewTopic topic(){
return new NewTopic(topic, patitions, replications);
}