一、kafkaAutoConfiguration配置;创建新的主题:kafkaAutoConfiguration配置
![](https://i-blog.csdnimg.cn/blog_migrate/c1f920ac6bf3a0013220c5e0c5665790.png)
二、新建主题,可以通过configuration配置
package com.yanqi.kafka.demo.config;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class KafkaConfig {
private ProducerFactory<Integer, String> producerFactory;
@Bean
public NewTopic topic1() {
return new NewTopic("nptc-01", 3, (short) 1);
}
@Bean
public NewTopic topic2() {
return new NewTopic("nptc-02", 5, (short) 1);
}
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> configs = new HashMap<>();
configs.put("bootstrap.servers", "node1:9092");
KafkaAdmin admin = new KafkaAdmin(configs);
return admin;
}
@Bean
@Autowired
public KafkaTemplate<Integer, String> kafkaTemplate(ProducerFactory<Integer, String> producerFactory) {
this.producerFactory = producerFactory;
// 覆盖ProducerFactory原有设置
Map<String, Object> configsOverride = new HashMap<>();
configsOverride.put(ProducerConfig.BATCH_SIZE_CONFIG, 200);
KafkaTemplate<Integer, String> template = new KafkaTemplate<Integer, String>(
producerFactory, configsOverride
);
return template;
}
}
三、运行Demo02SpringbootKafkaApplication
### --- 查看运行结果
[root@hadoop ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
__consumer_offsets
nptc-01
nptc-02
topic-spring-01