This commit is contained in:
Shikong 2023-04-25 17:27:17 +08:00
parent 2bb7c6dc65
commit 4e70a57964

View File

@ -4,10 +4,7 @@ import cn.skcks.study.springcloud.kafka.TestApplication;
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.clients.admin.*;
import org.apache.kafka.common.config.ConfigResource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -16,6 +13,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@ -79,12 +78,21 @@ public class KafkaConfigTest {
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);
describeConfigsResult.all().get().forEach((source, config) -> {
log.info("{} =>", source.name());
config.entries().forEach(entry -> {
log.info("\t{} => {}", entry.name(), entry.value());
});
});
Collection<AlterConfigOp> configs = Arrays.asList(
new AlterConfigOp(new ConfigEntry("min.cleanable.dirty.ratio", "0.5"), AlterConfigOp.OpType.SET),
new AlterConfigOp(new ConfigEntry("unclean.leader.election.enable", "false"), AlterConfigOp.OpType.SET),
new AlterConfigOp(new ConfigEntry("retention.ms","86400000"), AlterConfigOp.OpType.SET)
);
AlterConfigsResult result = client.incrementalAlterConfigs(Collections.singletonMap(resource,configs));
result.all().get();
}
}
}