From 9414aa4902acad35670bad549c05ee9ebec455ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 25 Sep 2024 05:55:52 -0600 Subject: [PATCH] Generate test app for Kafka using KafkaContainer and apache/kafka-native Spring Boot 3.4.0-M2 supports org.testcontainers.kafka.KafkaContainer. It also provides Testcontainers 1.20.1, which supports the apache/kafka-native image. See gh-1597 --- .../SimpleDockerServiceResolver.java | 8 ++++++ ...ngKafkaProjectGenerationConfiguration.java | 26 ++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java index 4a1cab951ab..e7c66f71685 100644 --- a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java +++ b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java @@ -42,6 +42,7 @@ public SimpleDockerServiceResolver() { this.dockerServices.put("chroma", chroma()); this.dockerServices.put("elasticsearch", elasticsearch()); this.dockerServices.put("kafka", kafka()); + this.dockerServices.put("kafka-native", kafkaNative()); this.dockerServices.put("mariaDb", mariaDb()); this.dockerServices.put("milvus", milvus()); this.dockerServices.put("mongoDb", mongoDb()); @@ -118,6 +119,13 @@ private static DockerService kafka() { .build(); } + private static DockerService kafkaNative() { + return DockerService.withImageAndTag("apache/kafka-native") + .website("https://hub.docker.com/r/confluentinc/cp-kafka") + .ports(9092) + .build(); + } + private static DockerService mariaDb() { return DockerService.withImageAndTag("mariadb").website("https://hub.docker.com/_/mariadb").ports(3306).build(); } diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java index 66d84f0f870..3431c1bf67b 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java @@ -18,7 +18,10 @@ import io.spring.initializr.generator.buildsystem.Build; import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; +import io.spring.initializr.generator.project.ProjectDescription; import io.spring.initializr.generator.project.ProjectGenerationConfiguration; +import io.spring.initializr.generator.version.VersionParser; +import io.spring.initializr.generator.version.VersionRange; import io.spring.start.site.container.DockerServiceResolver; import io.spring.start.site.container.ServiceConnections.ServiceConnection; import io.spring.start.site.container.ServiceConnectionsCustomizer; @@ -34,7 +37,17 @@ @ProjectGenerationConfiguration class SpringKafkaProjectGenerationConfiguration { - private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.containers.KafkaContainer"; + private static final String TESTCONTAINERS_CONFLUENT_CLASS_NAME = "org.testcontainers.containers.KafkaContainer"; + + private static final String TESTCONTAINERS_APACHE_CLASS_NAME = "org.testcontainers.kafka.KafkaContainer"; + + private static final VersionRange SPRING_BOOT_3_4_M2_OR_LATER = VersionParser.DEFAULT.parseRange("3.4.0-M2"); + + private final boolean isSpringBoot34OrLater; + + SpringKafkaProjectGenerationConfiguration(ProjectDescription projectDescription) { + this.isSpringBoot34OrLater = SPRING_BOOT_3_4_M2_OR_LATER.match(projectDescription.getPlatformVersion()); + } @Bean @ConditionalOnRequestedDependency("kafka") @@ -47,8 +60,15 @@ SpringKafkaBuildCustomizer springKafkaBuildCustomizer() { ServiceConnectionsCustomizer kafkaServiceConnectionsCustomizer(Build build, DockerServiceResolver serviceResolver) { return (serviceConnections) -> { if (isKafkaEnabled(build)) { - serviceResolver.doWith("kafka", (service) -> serviceConnections.addServiceConnection( - ServiceConnection.ofContainer("kafka", service, TESTCONTAINERS_CLASS_NAME, false))); + if (this.isSpringBoot34OrLater) { + serviceResolver.doWith("kafka-native", (service) -> serviceConnections.addServiceConnection( + ServiceConnection.ofContainer("kafka", service, TESTCONTAINERS_APACHE_CLASS_NAME, false))); + } + else { + serviceResolver.doWith("kafka", + (service) -> serviceConnections.addServiceConnection(ServiceConnection.ofContainer("kafka", + service, TESTCONTAINERS_CONFLUENT_CLASS_NAME, false))); + } } }; }