This commit is contained in:
Shikong 2023-04-25 16:48:28 +08:00
parent 73e72c8526
commit 2bb7c6dc65

View File

@ -5,8 +5,10 @@ import cn.skcks.study.springcloud.utils.JsonUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.DescribeConfigsResult;
import org.apache.kafka.clients.admin.ListTopicsOptions;
import org.apache.kafka.clients.admin.ListTopicsResult;
import org.apache.kafka.common.config.ConfigResource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
@ -14,6 +16,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.Map;
@ActiveProfiles("test")
@ -23,6 +26,8 @@ public class KafkaConfigTest {
@Autowired
private KafkaProperties kafkaProperties;
private final static String TOPIC = "test";
private Map<String, Object> adminProperties;
private Map<String, Object> producerProperties;
private Map<String, Object> consumerProperties;
@ -51,24 +56,35 @@ public class KafkaConfigTest {
@Test
@SneakyThrows
void topics() {
try (AdminClient adminClient = adminClient()) {
try (AdminClient client = adminClient()) {
ListTopicsOptions options = new ListTopicsOptions();
// 包含内置主题
options.listInternal(true);
ListTopicsResult topicsResult = adminClient.listTopics(options);
ListTopicsResult topicsResult = client.listTopics(options);
topicsResult.listings().get().stream().sorted((a,b) -> {
if(a.isInternal() && b.isInternal()){
return 0;
}
if (a.isInternal()){
return -1;
} else {
return 1;
}
return a.isInternal()?-1:1;
}).forEach(topic -> {
log.info("topic isInternal=>{}, uuid=>{}, name=>{}", topic.isInternal(), topic.topicId(), topic.name());
});
}
}
@Test
@SneakyThrows
void testTopicInfo(){
try(AdminClient client = adminClient()){
ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC,TOPIC);
DescribeConfigsResult describeConfigsResult = client.describeConfigs(Collections.singletonList(resource));
describeConfigsResult.all().get().forEach((key,config) -> {
log.info("{} =>",key);
config.entries().forEach(entry->{
log.info("\t{} => {}",entry.name(),entry.value());
});
});
}
}
}