Member-only story

Spring Boot — publish Kafka messages

theoneamin
3 min readJun 3, 2022

In this article, we’ll take a look at how to send messages to a Kafka topic using a REST application. We’ll also look at how to serialize an object so we can send it as a message. Be sure to have Kafka running as this article will not go into how to set that up.

New project

Below are the dependencies I’m using.

Producer

The producer is what we’ll use to send messages to a Kafka topic. When setting up the producer, we’ll define how the key and value of our message should be serialized.

Let’s add a config class and add the following to it.

With a String serializer for the key and value, we’re defining that we want to send string values for both key and value. We’ve also defined that our bootstrap server is running on localhost port 9092. Change this to whatever port you have Kafka broker running.

Next, let’s create a class that will let us send messages to any topic as long as the key and value are of the same type as the ones we defined in our KafkaProducerConfig class.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

theoneamin
theoneamin

No responses yet

Write a response