×
☰ See All Chapters

Kafka Producer Spring Boot Example

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
     
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <
modelVersion>4.0.0</modelVersion>
  <
parent>
     <
groupId>org.springframework.boot</groupId>
     <
artifactId>spring-boot-starter-parent</artifactId>
     <
version>2.1.6.RELEASE</version>
     <
relativePath/> <!-- lookup parent from repository, not local -->
 
</parent>
  <
groupId>com.example</groupId>
  <
artifactId>KafkaProducer</artifactId>
  <
version>0.0.1-SNAPSHOT</version>
  <
name>KafkaProducer</name>
  <
description>Kafka Producer Demo Project</description>
  <
properties>
     <
java.version>11</java.version>
     <
project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <
project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <
spring-boot.version>2.1.6.RELEASE</spring-boot.version>
  </
properties>
  <
dependencies>
     <
dependency>
        <
groupId>org.springframework.boot</groupId>
        <
artifactId>spring-boot-starter</artifactId>
     </
dependency>
     <
dependency>
        <
groupId>org.springframework.boot</groupId>
        <
artifactId>spring-boot-starter-web</artifactId>
     </
dependency>
     <
dependency>
        <
groupId>org.springframework.kafka</groupId>
        <
artifactId>spring-kafka</artifactId>
     </
dependency>

     <
dependency>
        <
groupId>org.springframework.boot</groupId>
        <
artifactId>spring-boot-starter-test</artifactId>
        <
scope>test</scope>
     </
dependency>
  </
dependencies>
  <
dependencyManagement>
     <
dependencies>
        <
dependency>
           <
groupId>org.springframework.boot</groupId>
           <
artifactId>spring-boot-dependencies</artifactId>
           <
version>${spring-boot.version}</version>
           <
type>pom</type>
           <
scope>import</scope>
        </
dependency>
     </
dependencies>
  </
dependencyManagement>

  <
build>
     <
plugins>
        <
plugin>
           <
groupId>org.springframework.boot</groupId>
           <
artifactId>spring-boot-maven-plugin</artifactId>
           <version>${spring-boot.version}</version>
           <
executions>
              <
execution>
                 <
id>build-info</id>
                 <
goals>
                    <
goal>build-info</goal>
                    <
goal>repackage</goal>
                 </
goals>
              </
execution>
           </
executions>
        </
plugin>
     </
plugins>
  </
build>
  <
repositories>
     <
repository>
        <
id>mdcm</id>
        <
name>mdcm</name>
        <
url>https://nexuscimgmt.sgp.dbs.com:8443/nexus/content/repositories/MDCM</url>
        <
snapshots>
           <
enabled>false</enabled>
        </
snapshots>
     </
repository>
  </
repositories>
  <
pluginRepositories>
     <
pluginRepository>
        <
id>mdcm</id>
        <
name>mdcm</name>
        <
url>https://nexuscimgmt.sgp.dbs.com:8443/nexus/content/repositories/MDCM</url>
        <
snapshots>
           <
enabled>false</enabled>
        </
snapshots>
     </
pluginRepository>
  </
pluginRepositories>

</
project>

 

application.properties

spring.kafka.producer.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

 

KafkaProducer.java

package com.java4coding.kafka;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducer {

   
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaProducer.class);

   
@Autowired
   
private KafkaTemplate<String, String> kafkaTemplate;

   
public void sendMessage(String message) {
       LOGGER.info(String.format("Message sent -> %s", message));
       
kafkaTemplate.send("demotopic", message);
   }
}

 

KafkaProducerController.java

package com.java4coding.restcontroller;

import com.java4coding.kafka.KafkaProducer;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class KafkaProducerController {

   
private KafkaProducer kafkaProducer;

   
public KafkaProducerController(KafkaProducer kafkaProducer) {
       
this.kafkaProducer = kafkaProducer;
   }

   
@GetMapping("/publish")
   
public ResponseEntity<String> publish(@RequestParam("message") String message) {
       
kafkaProducer.sendMessage(message);
       
return ResponseEntity.ok("Message sent to kafka topic");
   }
}

 

DemoProducerApplication.java

package com.java4coding.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
("com.java4coding")
public class DemoProducerApplication {

   
public static void main(String[] args) {
       
SpringApplication.run(DemoProducerApplication.class, args);
   }

}

 

Project Directory Structure

kafka-producer-spring-boot-example-0
 

Test the example application

Make sure kafka zookeeper, server are running. Kafka topic “demotopic” created before running the application. Follow the instructions from the previous chapter on installing and setting up Kafka on Windows to run Kafka and create a topic.

kafka-producer-spring-boot-example-1
 

All Chapters
Author