This project demonstrates how to create a Kafka producer in Java and send messages to a Kafka topic.
- Java 11 or higher
- Apache Kafka
- Maven
-
Install Apache Kafka: Follow the Kafka Quickstart guide to install and run Kafka on your local machine.
-
Create a Kafka Topic: Open a terminal and create a topic named
demo_java
with the following command:kafka-topics --create --topic demo_java --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Build the project:
mvn clean install
-
Run the producer:
mvn exec:java -Dexec.mainClass="code.with.vanilson.produtor.ProducerDemo"
To verify that the messages were sent successfully, you can use the Kafka console consumer:
kafka-console-consumer --bootstrap-server localhost:9092 --topic demo_java --from-beginning
The producer properties are configured in the ProducerDemo class. Here are the key configurations:
- bootstrap.servers: The address of the Kafka broker.
- key.serializer: The serializer class for the key.
- value.serializer: The serializer class for the value.
- Reset Offsets
kafka-consumer-groups --reset-offsets --group my-group --topic demo_java --to-earliest --execute --bootstrap-server localhost:9092
- Delete Consumer Group
kafka-consumer-groups --delete --group my-group --bootstrap-server localhost:9092
- Preferred Replica Election
kafka-preferred-replica-election --bootstrap-server localhost:9092
- Set Minimum In-Sync Replica
kafka-configs --alter --entity-type topics --entity-name demo_java --add-config min.insync.replicas=2 --bootstrap-server localhost:9092
Acks All:
kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=all
Acks 1:
kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=1
Acks 0:
kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=0