我们在开发过程中使用Kafka会遇到topic太多,自己创建太费劲,所以想一次配置终身使用,自己去创建topic,和flyway一样自己去创建表,今天总结一下配置方式。
1.application.yml配置
kafka:# 自动创建topictopics:# topic-name: import_vulnera_topic
#分区数num-partitions:6#副本数replication-factor:2# topic-name: import_vulnerability_topic
num-partitions:6replication-factor:2# topic-name: import_vulnerability_result_topic
num-partitions:6replication-factor:2
2.配置Configuration
importorg.apache.kafka.clients.admin.NewTopic;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.context.support.GenericWebApplicationContext;importjavax.annotation.PostConstruct;importjava.util.List;/**
* 自动创建topic
*/@Configuration@SuppressWarnings("all")publicclassTopicAdministrator{privatefinalTopicConfigurations configurations;privatefinalGenericWebApplicationContext context;publicTopicAdministrator(TopicConfigurations configurations,GenericWebApplicationContext genericContext){this.configurations = configurations;this.context = genericContext;}@PostConstructpublicvoidinit(){initializeBeans(configurations.getTopics());}privatevoidinitializeBeans(List<TopicConfigurations.Topic> topics){
topics.forEach(t -> context.registerBean(t.name,NewTopic.class, t::toNewTopic));}}
importlombok.Data;importorg.apache.kafka.clients.admin.NewTopic;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importjava.util.List;@Configuration@ConfigurationProperties(prefix ="kafka")@DatapublicclassTopicConfigurations{privateList<Topic> topics;@DatastaticclassTopic{String name;Integer numPartitions =3;Short replicationFactor =1;NewTopictoNewTopic(){returnnewNewTopic(this.name,this.numPartitions,this.replicationFactor);}}}
本文转载自: https://blog.csdn.net/daohangtaiqian/article/details/128658920
版权归原作者 飞四海 所有, 如有侵权,请联系我们删除。
版权归原作者 飞四海 所有, 如有侵权,请联系我们删除。