-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : upgrade to spring boot 3.4 and polish tc config
- Loading branch information
1 parent
5f6bb9f
commit f264514
Showing
6 changed files
with
82 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 3 additions & 101 deletions
104
...test/java/com/example/springbootkafkaavro/TestSpringBootKafkaAvroProducerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,13 @@ | ||
package com.example.springbootkafkaavro; | ||
|
||
import com.example.springbootkafkaavro.containers.KafkaRaftWithExtraListenersContainer; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
import com.example.springbootkafkaavro.containers.KafkaContainersConfig; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.KafkaContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class TestSpringBootKafkaAvroProducerApplication { | ||
|
||
private static final String KAFKA_NETWORK = "kafka-network"; | ||
|
||
Network network = getNetwork(); | ||
|
||
static Network getNetwork() { | ||
Network defaultDaprNetwork = | ||
new Network() { | ||
@Override | ||
public String getId() { | ||
return KAFKA_NETWORK; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public Statement apply(Statement base, Description description) { | ||
return null; | ||
} | ||
}; | ||
|
||
List<com.github.dockerjava.api.model.Network> networks = | ||
DockerClientFactory.instance() | ||
.client() | ||
.listNetworksCmd() | ||
.withNameFilter(KAFKA_NETWORK) | ||
.exec(); | ||
if (networks.isEmpty()) { | ||
Network.builder() | ||
.createNetworkCmdModifier(cmd -> cmd.withName(KAFKA_NETWORK)) | ||
.build() | ||
.getId(); | ||
} | ||
return defaultDaprNetwork; | ||
} | ||
|
||
@Bean | ||
@ServiceConnection | ||
KafkaContainer kafkaContainer() { | ||
return new KafkaRaftWithExtraListenersContainer("confluentinc/cp-kafka:7.7.1") | ||
.withAdditionalListener(() -> "kafka:19092") | ||
.withKraft() | ||
.withNetwork(network) | ||
.withNetworkAliases("kafka") | ||
.withReuse(true); | ||
} | ||
|
||
@Bean | ||
@DependsOn("kafkaContainer") | ||
GenericContainer<?> schemaregistry(DynamicPropertyRegistry dynamicPropertyRegistry) { | ||
GenericContainer<?> schemaRegistry = | ||
new GenericContainer<>("confluentinc/cp-schema-registry:7.7.1") | ||
.withExposedPorts(8085) | ||
.withNetworkAliases("schemaregistry") | ||
.withNetwork(network) | ||
.withEnv( | ||
"SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", | ||
"PLAINTEXT://kafka:19092") | ||
.withEnv("SCHEMA_REGISTRY_LISTENERS", "http://0.0.0.0:8085") | ||
.withEnv("SCHEMA_REGISTRY_HOST_NAME", "schemaregistry") | ||
.withEnv("SCHEMA_REGISTRY_KAFKASTORE_SECURITY_PROTOCOL", "PLAINTEXT") | ||
.waitingFor(Wait.forHttp("/subjects")) | ||
.withStartupTimeout(Duration.of(120, ChronoUnit.SECONDS)) | ||
.withLabel("com.testcontainers.desktop.service", "cp-schema-registry"); | ||
dynamicPropertyRegistry.add( | ||
"spring.kafka.producer.properties.schema.registry.url", | ||
() -> | ||
"http://%s:%d" | ||
.formatted( | ||
schemaRegistry.getHost(), | ||
schemaRegistry.getMappedPort(8085))); | ||
dynamicPropertyRegistry.add( | ||
"spring.kafka.properties.schema.registry.url", | ||
() -> | ||
"http://%s:%d" | ||
.formatted( | ||
schemaRegistry.getHost(), | ||
schemaRegistry.getMappedPort(8085))); | ||
|
||
return schemaRegistry; | ||
} | ||
class TestSpringBootKafkaAvroProducerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.from(SpringBootKafkaAvroProducerApplication::main) | ||
.with(TestSpringBootKafkaAvroProducerApplication.class) | ||
.with(KafkaContainersConfig.class) | ||
.run(args); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ducer/src/test/java/com/example/springbootkafkaavro/containers/KafkaContainersConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.example.springbootkafkaavro.containers; | ||
|
||
import java.time.Duration; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.test.context.DynamicPropertyRegistrar; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.kafka.ConfluentKafkaContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class KafkaContainersConfig { | ||
|
||
private final Network network = Network.newNetwork(); | ||
private final String CONFLUENT_VERSION = "7.7.2"; | ||
|
||
@Bean | ||
@ServiceConnection | ||
ConfluentKafkaContainer kafkaContainer() { | ||
ConfluentKafkaContainer confluentKafkaContainer = | ||
new ConfluentKafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag(CONFLUENT_VERSION)) | ||
.withListener("tc-kafka:19092") // Internal alias and port | ||
.withNetwork(network) // Shared network for communication | ||
.withNetworkAliases("tc-kafka") // Alias to match Schema Registry | ||
.withReuse(true); | ||
confluentKafkaContainer.start(); | ||
return confluentKafkaContainer; | ||
} | ||
|
||
@Bean | ||
@DependsOn("kafkaContainer") | ||
GenericContainer<?> schemaRegistry() { | ||
GenericContainer<?> schemaRegistry = | ||
new GenericContainer<>(DockerImageName.parse("confluentinc/cp-schema-registry").withTag(CONFLUENT_VERSION)) | ||
.withExposedPorts(8085) | ||
.withNetworkAliases("schemaregistry") // Alias for Schema Registry | ||
.withNetwork(network) // Use the same network as Kafka | ||
.withEnv( | ||
"SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", | ||
"PLAINTEXT://tc-kafka:19092") // Match Kafka alias and port | ||
.withEnv("SCHEMA_REGISTRY_LISTENERS", "http://0.0.0.0:8085") | ||
.withEnv("SCHEMA_REGISTRY_HOST_NAME", "schemaregistry") | ||
.withEnv("SCHEMA_REGISTRY_KAFKASTORE_SECURITY_PROTOCOL", "PLAINTEXT") | ||
.waitingFor(Wait.forHttp("/subjects").forStatusCode(200)) | ||
.withStartupTimeout(Duration.ofSeconds(60)); | ||
schemaRegistry.start(); | ||
return schemaRegistry; | ||
} | ||
|
||
@Bean | ||
DynamicPropertyRegistrar dynamicPropertyRegistrar(GenericContainer<?> schemaRegistry) { | ||
return dynamicProperty -> { | ||
dynamicProperty.add( | ||
"spring.kafka.producer.properties.schema.registry.url", | ||
() -> | ||
"http://%s:%d" | ||
.formatted( | ||
schemaRegistry.getHost(), | ||
schemaRegistry.getMappedPort(8085))); | ||
dynamicProperty.add( | ||
"spring.kafka.properties.schema.registry.url", | ||
() -> | ||
"http://%s:%d" | ||
.formatted( | ||
schemaRegistry.getHost(), | ||
schemaRegistry.getMappedPort(8085))); | ||
}; | ||
} | ||
} |
61 changes: 0 additions & 61 deletions
61
...java/com/example/springbootkafkaavro/containers/KafkaRaftWithExtraListenersContainer.java
This file was deleted.
Oops, something went wrong.