kafka+spring boot 检查topic,分区扩展

属性配置 nacos配置注入

@Configuration
public class MyTopicConfig {

    private AdminClient adminClient;
    @Value("${kafka.topic.names}")
    private String topicNames;
    @Value("${kafka.topic.partitions}")
    private Integer partitions;
    @Value("${kafka.topic.replications}")
    private Short replications;
    @Value("${kafka.topic.bootstrapServers}")
    private String bootstrapServers;

    private Logger logger = LoggerFactory.getLogger(MyTopicConfig.class);

    @PostConstruct
    public void refreshTopics() {
        Properties properties = new Properties();
        properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        adminClient = KafkaAdminClient.create(properties);
        //检查心主题和分区
        adminClient.listTopics().names()
                .whenComplete((strings, throwable) -> {
                    logger.info("当前已有主题:{}", strings);
                    List newTopics = Arrays.asList(topicNames.split(","));
                    Iterator iterator = newTopics.iterator();
                    while (iterator.hasNext()) {
                        String str = iterator.next();
                        if (!strings.isEmpty() && strings.contains(str))
                            iterator.remove();
                    }
                    if (!newTopics.isEmpty()) {
                        logger.info("创建新的主题:{}", newTopics);
                        List newTopicList = new ArrayList<>();
                        for (String ntp : newTopics) {
                            NewTopic newTopic = new NewTopic(ntp, partitions, replications);
                            newTopicList.add(newTopic);
                        }
                        adminClient.createTopics(newTopicList);
                    }
                    //已存在的topic检查分区
                    adminClient.describeTopics(strings).all().whenComplete((descriptionMap, throwable1) -> {
                        for (Map.Entry entry : descriptionMap.entrySet()) {
                            List tps = entry.getValue().partitions();
                            logger.info("主题:{},描述:{}", entry.getKey(), tps);
                            if (tps.size() < partitions) {
                                logger.info("主题扩展分区:{}", entry.getKey());
                                //注意创建方式
                                NewPartitions newPartitions = NewPartitions.increaseTo(partitions);
                                Map partitionsMap = new HashMap<>();
                                partitionsMap.put(entry.getKey(), newPartitions);
                                adminClient.createPartitions(partitionsMap);
                            }
                        }
                    });
                });
    }

}

 

你可能感兴趣的:(java)