Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate test app for Kafka using KafkaContainer and apache/kafka-native #1597

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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)));
}
}
};
}
Expand Down