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

Spring Boot Kafka Producer Example

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.

1. Dependencies:

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>

2. Configuration:

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

3. Kafka Producer:

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);
    }
}

4. REST Controller:

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);
    }
}

5. Main Application:

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);
    }
}

6. Running the Application:

  • 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
  • If you have a Kafka consumer listening to "myTopic", you should see the message being received.

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.

  1. Configuring Kafka producer in Spring Boot:

    • Description: Setting up a Kafka producer in a Spring Boot application involves configuring the necessary properties and dependencies.
    • Code:
      // Application properties
      spring.kafka.producer.bootstrap-servers=your-kafka-server
      
  2. Serializing messages with Kafka producer in Spring Boot:

    • Description: Serialization is important to convert objects into Kafka messages before sending them.
    • Code:
      // Producer configuration
      @Bean
      public ProducerFactory<String, YourMessageClass> producerFactory() {
          return new DefaultKafkaProducerFactory<>(producerConfigs(), new StringSerializer(),
                  new JsonSerializer<>(YourMessageClass.class));
      }
      
  3. Producing different data formats with Kafka producer in Spring Boot:

    • Description: Supporting various data formats like JSON, Avro, or others in a Kafka producer.
    • Code:
      // Producer configuration for Avro
      @Bean
      public ProducerFactory<String, SpecificRecord> avroProducerFactory() {
          // configure Avro serializer
      }
      
  4. Error handling in Spring Boot Kafka producer:

    • Description: Implementing error handling mechanisms to deal with exceptions and errors during Kafka message production.
    • Code:
      // 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
          }
      }
      
  5. Partitioning and key-based routing in Kafka producer with Spring Boot:

    • Description: Understanding how to route messages to specific partitions based on keys for better control and parallel processing.
    • Code:
      // Producing message with key
      kafkaTemplate.send("your-topic", "your-key", message);
      
  6. Customizing Kafka producer properties in Spring Boot:

    • Description: Fine-tuning Kafka producer properties to match the specific requirements of your application.
    • Code:
      // Customizing producer properties
      spring.kafka.producer.linger-ms=100
      spring.kafka.producer.batch-size=16384
      
  7. Asynchronous and synchronous Kafka message production in Spring Boot:

    • Description: Exploring both asynchronous and synchronous approaches to produce Kafka messages based on application needs.
    • Code:
      // Asynchronous message production
      kafkaTemplate.send("your-topic", message);
      
      // Synchronous message production
      kafkaTemplate.send("your-topic", message).get();
      
  8. Integration testing for Kafka producer in Spring Boot:

    • Description: Writing integration tests to ensure the correct functioning of the Kafka producer in a Spring Boot application.
    • Code:
      // Kafka producer integration test
      @SpringBootTest
      public class KafkaProducerIntegrationTest {
          // Test methods
      }