一、SpringBoot 中新建topic
非常简单只需要写个配置类就可以了
@Configuration
public class KafkaInitialConfiguration {
//创建TopicName为topic-new的Topic并设置分区数为4以及副本数为2
@Bean
public NewTopic initialTopic() {
return new NewTopic("topic-new",4, (short) 2 );
}
}
副本数量不能大于broker 数量,概念问题!
此时直接启动SpringBoot 查询是否有新建的topic
下面介绍下上面使用的工具,kafka tool 后面演示及我们使用中可能会用到它。
二、kafka可视化客户端工具(Kafka Tool)
1、下载地址:http://www.kafkatool.com/download.html
2、设置连接
进行测试连接 ,完成!
可以查看broker,topic ,consumers
3、设置topic 消息查看
由于消息不能直接查看,需要配置以字符串的形式显示kafka消息体
这里就能看到消息体了
三、topic操作汇总
1、配置类
重新配置 kafkaAdmin 和 adminClient
/**
* kafka 配置类
* @author Administrator
*
*/
@Configuration
public class KafkaInitialConfiguration {
//创建TopicName为topic-new的Topic并设置分区数为4以及副本数为2
@Bean
public NewTopic initialTopic() {
return new NewTopic("topic-new",4, (short) 2 );
}
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> props = new HashMap<>();
//配置Kafka实例的连接地址
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "10.135.128.39:9092,10.135.128.39:9093");
KafkaAdmin admin = new KafkaAdmin(props);
return admin;
}
@Bean
public AdminClient adminClient() {
return AdminClient.create(kafkaAdmin().getConfig());
}
}
2、kafka工具类 kafkaUtils
@Component
public class KafkaUtils {
@Autowired
private AdminClient adminClient;
/**
* 组装单个topic
* @param topic
* @return
*/
public boolean createSingleTopic(String topic,int numPartitions,short replicationFactor) {
NewTopic newTopic = new NewTopic(topic, numPartitions, replicationFactor);
List<NewTopic> newTopics = new ArrayList<NewTopic>(1);
newTopics.add(newTopic);
return createTopics(newTopics, null);
}
/**
* 创建topic 通用方法
* @param newTopics
* @param createTopicsOptions
* @return
*/
public boolean createTopics(List<NewTopic> newTopics, CreateTopicsOptions createTopicsOptions) {
if (createTopicsOptions == null) {
createTopicsOptions = new CreateTopicsOptions();
createTopicsOptions.timeoutMs(1000);
}
CreateTopicsResult results = adminClient.createTopics(newTopics, createTopicsOptions);
KafkaFuture<Void> kafkaFuture = results.all();
return kafkaFuture.isDone();
}
/**
* 删除topics
* @param name
* @return
*/
public boolean deleteTopic(String... name) {
if (name == null || name.length == 0) {
return true;
}
List<String> topics = Arrays.asList(name);
DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(topics);
return deleteTopicsResult.all().isDone();
}
/**
* 获取topic 详情
* @param topics
* @return
* @throws InterruptedException
* @throws ExecutionException
*/
public Map<String, TopicDescription> descriptTopic(String... topics) throws InterruptedException, ExecutionException {
DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Arrays.asList(topics));
return describeTopicsResult.all().get();
}
/**
* 查询主题名
*
* @return
*/
public List<String> queryAllTopic() {
ListTopicsResult listTopicsResult = adminClient.listTopics();
KafkaFuture<Collection<TopicListing>> kafkaFuture = listTopicsResult.listings();
Collection<TopicListing> collections;
List<String> topics = null;
try {
collections = kafkaFuture.get();
if (collections != null && collections.size() != 0) {
topics = new ArrayList<String>(collections.size());
for (TopicListing topicListing : collections) {
topics.add(topicListing.name());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return topics;
}
}
3、SpringBoot启动类 测试
@SpringBootApplication
public class SpringBootForKafkaApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringBootForKafkaApplication.class, args);
KafkaUtils kafkaUtils = context.getBean(KafkaUtils.class);
List<String> queryAllTopic = kafkaUtils.queryAllTopic();
System.out.println(queryAllTopic.toString());
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
kafkaUtils.createSingleTopic("topic-new03", 2, (short)2);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
kafkaUtils.deleteTopic("topic-new02");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> queryAllTopic2 = kafkaUtils.queryAllTopic();
System.out.println(queryAllTopic2.toString());
}
}
注意需要再后面打印前添加一定的延迟 否则打印结果与工具上的不一致,可以原因都懂吧。
查看控制台结果: