Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Let's set up a basic Spring Boot Kafka Producer using the Spring Kafka project. This guide will show you how to produce string messages to a Kafka topic.
Start by adding necessary dependencies to your Maven pom.xml
:
<!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Kafka --> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
Inside your application.properties
or application.yml
, add Kafka configurations:
# Kafka spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
Create a Kafka producer service to send messages to a specific topic:
package com.example.demo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @Service public class KafkaProducerService { @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void sendMessage(String message) { kafkaTemplate.send("myTopic", message); } }
To demonstrate the producer in action, let's create a simple REST controller:
package com.example.demo.controller; import com.example.demo.service.KafkaProducerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/kafka") public class KafkaController { @Autowired private KafkaProducerService producerService; @PostMapping("/publish") public void publishMessage(@RequestBody String message) { producerService.sendMessage(message); } }
Your main Spring Boot application class remains unchanged:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class KafkaProducerDemoApplication { public static void main(String[] args) { SpringApplication.run(KafkaProducerDemoApplication.class, args); } }
Make sure your Kafka server is running. If you're running Kafka locally, it should be accessible at localhost:9092
.
Start your Spring Boot application.
Use tools like Postman or curl
to hit the /api/kafka/publish
endpoint with a POST request and a string body to send a message to the Kafka topic "myTopic".
Example with curl
:
curl -X POST -d "This is a test message" http://localhost:8080/api/kafka/publish
With these steps, you have a basic Spring Boot Kafka Producer set up. The producer will send messages to the "myTopic" topic when the REST endpoint is invoked.
Configuring Kafka producer in Spring Boot:
// Application properties spring.kafka.producer.bootstrap-servers=your-kafka-server
Serializing messages with Kafka producer in Spring Boot:
// Producer configuration @Bean public ProducerFactory<String, YourMessageClass> producerFactory() { return new DefaultKafkaProducerFactory<>(producerConfigs(), new StringSerializer(), new JsonSerializer<>(YourMessageClass.class)); }
Producing different data formats with Kafka producer in Spring Boot:
// Producer configuration for Avro @Bean public ProducerFactory<String, SpecificRecord> avroProducerFactory() { // configure Avro serializer }
Error handling in Spring Boot Kafka producer:
// Kafka producer with error handling @Autowired private KafkaTemplate<String, YourMessageClass> kafkaTemplate; public void produceMessage(YourMessageClass message) { try { kafkaTemplate.send("your-topic", message); } catch (Exception e) { // Handle the exception } }
Partitioning and key-based routing in Kafka producer with Spring Boot:
// Producing message with key kafkaTemplate.send("your-topic", "your-key", message);
Customizing Kafka producer properties in Spring Boot:
// Customizing producer properties spring.kafka.producer.linger-ms=100 spring.kafka.producer.batch-size=16384
Asynchronous and synchronous Kafka message production in Spring Boot:
// Asynchronous message production kafkaTemplate.send("your-topic", message); // Synchronous message production kafkaTemplate.send("your-topic", message).get();
Integration testing for Kafka producer in Spring Boot:
// Kafka producer integration test @SpringBootTest public class KafkaProducerIntegrationTest { // Test methods }