Skip to content

GH-2391: Add Kraft Version of EmbeddedKafkaBroker #2820

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

Merged
merged 3 commits into from
Oct 2, 2023
Merged
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
21 changes: 18 additions & 3 deletions spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

The `spring-kafka-test` jar contains some useful utilities to assist with testing your applications.

[[ekb]]
== EmbeddedKafkaBroker

Two implementations are provided:

* `EmbeddedKafkaZKBroker` - legacy implementation which starts an embedded `Zookeeper` instance.
* `EmbeddedKafkaKraftBroker` - (default) uses Kraft instead of `Zookeeper` in combined controller and broker modes (since 3.1)

There are several techniques to configure the broker as discussed in the following sections.

[[ktu]]
== KafkaTestUtils

Expand Down Expand Up @@ -47,7 +57,7 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
Since it does not have access to the consumer properties, you must use the overloaded method that takes a `seekToEnd` boolean parameter to seek to the end instead of the beginning.
====

A JUnit 4 `@Rule` wrapper for the `EmbeddedKafkaBroker` is provided to create an embedded Kafka and an embedded Zookeeper server.
A JUnit 4 `@Rule` wrapper for the `EmbeddedKafkaZKBroker` is provided to create an embedded Kafka and an embedded Zookeeper server.
(See xref:testing.adoc#embedded-kafka-annotation[@EmbeddedKafka Annotation] for information about using `@EmbeddedKafka` with JUnit 5).
The following listing shows the signatures of those methods:

Expand All @@ -72,6 +82,8 @@ public EmbeddedKafkaRule(int count, boolean controlledShutdown, String... topics
public EmbeddedKafkaRule(int count, boolean controlledShutdown, int partitions, String... topics) { ... }
----

NOTE: The `EmbeddedKafkaKraftBroker` is not supported with JUnit4.

The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
The following example shows how to use it:

Expand Down Expand Up @@ -162,7 +174,7 @@ You can use the same broker for multiple test classes with something similar to
----
public final class EmbeddedKafkaHolder {

private static EmbeddedKafkaBroker embeddedKafka = new EmbeddedKafkaBroker(1, false)
private static EmbeddedKafkaBroker embeddedKafka = new EmbeddedKafkaZKBroker(1, false)
.brokerListProperty("spring.kafka.bootstrap-servers");

private static boolean started;
Expand Down Expand Up @@ -216,7 +228,8 @@ In addition, these properties can be provided:
- `spring.kafka.embedded.ports` - ports (comma-separated value) for every Kafka broker to start, `0` if random port is a preferred; the number of values must be equal to the `count` mentioned above;
- `spring.kafka.embedded.topics` - topics (comma-separated value) to create in the started Kafka cluster;
- `spring.kafka.embedded.partitions` - number of partitions to provision for the created topics;
- `spring.kafka.embedded.broker.properties.location` - the location of the file for additional Kafka broker configuration properties; the value of this property must follow the Spring resource abstraction pattern.
- `spring.kafka.embedded.broker.properties.location` - the location of the file for additional Kafka broker configuration properties; the value of this property must follow the Spring resource abstraction pattern;
- `spring.kafka.embedded.kraft` - when false, use an `EmbeddedKafkaZKBroker` instead of an `EmbeddedKafkaKraftBroker`.

Essentially these properties mimic some of the `@EmbeddedKafka` attributes.

Expand Down Expand Up @@ -283,6 +296,8 @@ public class KafkaStreamsTests {

Starting with version 2.2.4, you can also use the `@EmbeddedKafka` annotation to specify the Kafka ports property.

Starting with version 3.1; set the `kraft` property to `false` to use an `EmbeddedKafkaZKBroker` instead of an `EmbeddedKafkaKraftBroker`.

The following example sets the `topics`, `brokerProperties`, and `brokerPropertiesLocation` attributes of `@EmbeddedKafka` support property placeholder resolutions:

[source, java]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
This section covers the changes made from version 3.0 to version 3.1.
For changes in earlier version, see xref:appendix/change-history.adoc[Change History].

[[x30-kafka-client]]
[[x31-kafka-client]]
=== Kafka Client Version

This version requires the 3.5.1 `kafka-clients`.

[[x31-ekb]]
=== EmbeddedKafkaBroker

An additional implementation is now provided to use `Kraft` instead of Zookeeper.
See <<ekb>> for more information.
Loading