In this tutorial, we are going to see how to publish Kafka messages with Spring Boot Kafka Producer.

Spring Boot Kafka Producer:

As part of this example, we will see how to publish a simple string message to Kafka topic.

Technologies:

  • Spring Boot 2.1.3
  • Spring Kafka
  • Java 8
  • Maven

Pre-requisites:

Install Apache Kafka on your machine, follow below articles to install Kafka.

Install Apache Kafka on Windows 10 Operating system

Install Apache Kafka on Ubuntu Operating system

Start Zookeeper server:

Terminal
root@work:/usr/local/kafka/bin# ./zookeeper-server-start.sh ../config/zookeeper.properties 
[2019-03-28 00:22:52,195] INFO Reading configuration from: ../config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2019-03-28 00:22:52,210] INFO autopurge.snapRetainCount set to 3 (org.apache.zookeeper.server.DatadirCleanupManager)
[2019-03-28 00:22:52,210] INFO autopurge.purgeInterval set to 0 (org.apache.zookeeper.server.DatadirCleanupManager)
[2019-03-28 00:22:52,211] INFO Purge task is not scheduled. (org.apache.zookeeper.server.DatadirCleanupManager)
[2019-03-28 00:22:52,211] WARN Either no config or no quorum defined in config, running  in standalone mode (org.apache.zookeeper.server.quorum.QuorumPeerMain)

Start Kafka Server:

Terminal
root@work:/usr/local/kafka/bin# ./kafka-server-start.sh ../config/server.properties 
[2019-03-28 00:24:08,203] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2019-03-28 00:24:09,146] INFO starting (kafka.server.KafkaServer)
[2019-03-28 00:24:09,147] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
[2019-03-28 00:24:09,219] INFO [ZooKeeperClient] Initializing a new session to localhost:2181. (kafka.zookeeper.ZooKeeperClient)
[2019-03-28 00:24:09,229] INFO Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 00:39 GMT (org.apache.zookeeper.ZooKeeper)

Create a Kafka Topic:

Terminal
root@work:/usr/local/kafka/bin# ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic hello-topic
Created topic "hello-topic".

Start Kafka Consumer console:

Terminal
root@work:/usr/local/kafka/bin# ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic hello-topic --from-beginning

1. Spring Boot Kafka Producer Example:

On the above pre-requisites session, we have started zookeeper, Kafka server and created one hello-topic and also started Kafka consumer console.

Now we are going to push some messages to hello-topic through Spring boot application using KafkaTemplate and we will monitor these messages from Kafka consumer console.

1.1 Project Structure:

Spring Boot Kafka Producer Example String-min

1.2. Dependencies:

pom.xml
dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
  </dependency>
</dependencies>

2. HelloController:

Sending simple string messages to Kafka topic.

HelloController.java
package com.onlinetutorialspoint.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("producer")
public class HelloController {

    @Autowired
    KafkaTemplate<String,String> template;
    String TOPIC_NAME = "hello-topic";

    @GetMapping("/say/{msg}")
    public String postMessage(@PathVariable("msg") String msg){
        template.send(TOPIC_NAME,msg);
        return "Message published successfully";
    }
}

3. SpringBoot Main:

SpringBootKafkaProducerApplication.java
package com.onlinetutorialspoint;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootKafkaProducerApplication {

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

}

4. Run it:

Terminal
$ mvn clean install
$ mvn spring-boot:run
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-28 00:32:13.814  INFO 15921 --- [           main] c.o.SpringBootKafkaProducerApplication   : Starting SpringBootKafkaProducerApplication on work with PID 15921 (/home/cgoka/Documents/Work/Spring_Examples/Spring-Boot-Kafka-Producer/target/classes started by cgoka in /home/cgoka/Documents/Work/Spring_Examples/Spring-Boot-Kafka-Producer)
.....
.....

5. Access Application and send a message:

Spring Boot Kafka Producer Example-min(1)

 

6. Kafka Consumer console:

Spring Boot Kafka Producer Example Output-min

References:

Happy Learning 🙂

Download Example