![]() |
VOOZH | about |
In modern applications, processing real-time data efficiently is key. This tutorial shows how to integrate Spring Boot with Kafka for message consumption, Elasticsearch for storing and indexing data, and Grafana for real-time visualization.
Version compatibility is of paramount importance in the case of Elastic and Spring Boot. If your elastic-search version doesn't match with the spring boot version or vice versa, then you face problems in configuring both. Below is the list of version compatibility :
Spring Data Elasticsearch | Elastic-Search | Spring framework | Spring Boot |
|---|---|---|---|
4.4.X | 7.17.3 | 5.3.X | 2.7.X |
4.3.X | 7.15.2 | 5.3.X | 2.6.X |
4.2.X | 7.12.0 | 5.3.X | 2.5.X |
4.1.X | 7.9.3 | 5.3.2 | 2.4.X |
4.0.X | 7.6.2 | 5.2.12 | 2.3.X |
3.2.X | 6.8.12 | 5.2.12 | 2.2.X |
3.1.X | 6.2.2 | 5.1.19 | 2.1.X |
3.0.X | 5.5.0 | 5.0.13 | 2.0.X |
2.1.X | 2.4.0 | 4.3.25 | 1.5.X |
Download the required software:
Download and extract from your system.
pom.xml – Contains required dependencies.
application.properties
server.port=1234
Create a spring boot project named Producer.ProducerApplication.java -> Main class to run the application.
ProducerApplication.class
In this class, we have provided the configuration of Kafka. KafkaProducerConfig.java -> Contains Kafka configuration.
KafkaProducerConfig.java
Model class where we store the user information.
User.java
Create Service class to write the logic. Service class uses kafka template to send the data to the consumer.
Uses KafkaTemplate to send data to Kafka.
KafkaService.java
Create the Controller class. REST controller for sending API requests.
ProducerController.java
Create another spring boot application named ElasticConsumer.
Create Application class ->Main class to run the consumer application.
ComsumerApplication.java
This class Contains Kafka consumer configuration.
KafkaConsumerConfig.java
Model class used for storing data in Elasticsearch.
Important annotations:
@Document : Defines the Elasticsearch index@Id : Unique identifier for each document@Field :Specifies field type inside the documentRepository interface used to perform operations with Elasticsearch.
KafkaUserRepository.java
Service class that consumes Kafka messages and stores them in Elasticsearch.
KafkaUserService.java
Open Command Prompt and navigate to the Elasticsearch bin folder.
Run the following command:
elasticsearch.bat
After starting, open the browser and check:
http://localhost:9200
If Elasticsearch is running successfully, it will display cluster information in JSON format.
Send JSON data using postman. Here data is sent by the Producer app to ElasticConsumer. And in the ElasticConsumer console data would be printed and saved into ElasticSearchDB in the form of JSON.
Producer App APIs:
ElasticConsumer app APIs
Grafana dashboard is running on http://localhots:3000. Watch the configuration video below.
Output Video 1:
Output Video 2: