Spring Boot Kafka Producer Consumer Example Tutorial

 

Spring Boot Kafka Producer Consumer Example Tutorial


The Spring  provides Spring for Apache Kafka dependency to work with the development of Kafka-based messaging solutions. 

In this tutorial, we use Kafka as a messaging system to send messages between Producers and Consumers.


1. Install and Setup Apache Kafka

1. Download Kafka from the official website 

2. Extract Kafka zip in the local file system

Run the following commands in order to start all services in the correct order:

3. Start Zookeeper service. 

Use the below command to start the Zookeeper service:

# Start the ZooKeeper service # Note: Soon, ZooKeeper will no longer be required by Apache Kafka. $ bin/zookeeper-server-start.sh config/zookeeper.properties --For Linux

bin\windows\zookeeper-server-start.bat config\zookeeper.properties --window user


4. Start Kafka Broker

Command for linux
bin\windows\zookeeper-server-start.sh config\server.properties

For Window user
bin\windows\zookeeper-server-start.bat config\server.properties



Create a Spring boot project using https://start.spring.io/

Add 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>

Configure Kafka Producer and Consumer in an application. Properties File

In the application.properties file, add Kafka broker address as well as Consumer and Producer related configuration.

Open the application.properties file and the following content to it:


spring.kafka.consumer.bootstrap-servers: localhost:9092
spring.kafka.consumer.group-id: group-id
spring.kafka.consumer.auto-offset-reset: earliest
spring.kafka.consumer.key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

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


Let's understand the above spring boot provided Kafka properties:

spring.kafka.consumer.group-id - specifies a unique string that identifies the consumer group this consumer belongs to.



spring.kafka.consumer.auto-offset-reset property - specifies what to do when there is no initial offset in Kafka or if the current offset does not exist anymore on the server (e.g. because that data has been deleted):
  • earliest: automatically reset the offset to the earliest offset
  • latest: automatically reset the offset to the latest offset
  • none: throw an exception to the consumer if no previous offset is found for the consumer’s group
  • anything else: throw an exception to the consumer.


spring.kafka.consumer.key-deserializer - specifies the deserializer class for keys.

spring.kafka.consumer.value-deserializer - specifies the deserializer class for values.

spring.kafka.producer.key-deserializer - specifies the serializer class for keys.

spring.kafka.producer.value-deserializer - specifies the serializer class for values.




Serialization generally refers to creating a version of the data (rather than the objects) that can be used for storage (perhaps in a file), for transfer over a network, or perhaps just for transfer between processes / AppDomains /etc on a single machine.


Serialization typically means writing the data as a string (think: xml / json) or as raw binary (a byte[] etc). 

Deserialization is the reverse process; taking the raw data (from a file, from an incoming network socket, etc) and reconstructing the object model.







Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence