⚙️ Spring Cloud Streams with Apache Kafka
Tags: webdev, programming, tutorial
Published: 2021-11-16 16:02:02

#### 📘 Learn how to process streams of data from Apache Kafka topics using Spring Cloud Streams.

### <br>

**TL;DR:** Have you ever wondered how features like Google Maps' live traffic work? These systems have to gather and process data in real-time. The architecture of these systems generally involves a data pipeline that processes and transfers data to be processed further until it reaches the clients. In this article, we will see something similar with a simple example using Kafka Streams. The sample app can be found [here](https://github.com/auth0-blog/spring-cloud-stream-sample).

## Introduction to Spring Cloud Stream

Spring Cloud Stream is a framework designed to support stream processing provided by various messaging systems like Apache Kafka, RabbitMQ, etc. The framework allows you to create processing logic without having to deal with any specific platform. It helps you build highly scalable event-driven microservices connected using these messaging systems.

The framework provides a flexible programming model built on already established and familiar Spring idioms and best practices. The way it works is simple; you have to provide implementations (called Binder implementations)for the messaging system that you are using. Spring cloud stream supports:

* [RabbitMQ](https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit)
* [Apache Kafka](https://github.com/spring-cloud/spring-cloud-stream-binder-kafka)
* [Kafka Streams](https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/tree/main/spring-cloud-stream-binder-kafka-streams)
* [Amazon Kinesis](https://github.com/spring-cloud/spring-cloud-stream-binder-aws-kinesis)

And a few others. The links above will take you to the binder implementations. In this article, we will look into a simple application that uses Kafka Streams as a stream processor listening to events on a topic, processing the data, and publishing it to the outgoing topic.

## Introduction to Apache Kafka

Apache Kafka is a distributed publish-subscribe messaging system. It is a system that publishes and subscribes to a stream of records, similar to a message queue. Kafka is suitable for both offline and online message consumption. It is fault-tolerant, robust, and has a high throughput. Kafka is run as a cluster on one or more servers that can span multiple data centers. The Kafka cluster stores stream of records in categories called topics. Each record consists of a key, a value, and a timestamp. For more information on topics, Producer API, Consumer API, and event streaming, please visit this [link](https://kafka.apache.org/intro).

## Introduction to Kafka Streams

Kafka Streams is a library that can be used to consume data, process it, and produce new data, all in real-time. It works on a continuous, never-ending stream of data. Consider an example of the stock market. The stock prices fluctuate every second, and to be able to provide real-time value to the customer, you would use something like Kafka streams.

## Pre-requisites:

1. Basic knowledge of [Java 11](https://jdk.java.net/archive/).
2. Basic knowledge of [Spring Boot](https://spring.io/projects/spring-boot).
3. A basic understanding of [Apache Kafka](https://kafka.apache.org/intro).
4. [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) for running Kafka locally.

## Setting up Spring Boot App

Let us first create a Spring Boot project with the help of the [Spring boot Initializr](https://start.spring.io/#!type=mavenproject&language=java&platformVersion=2.4.5.RELEASE&packaging=jar&jvmVersion=11&groupId=io.pratik&artifactId=springcloudsqs&name=springcloudsqs&description=Demo%20project%20for%20Spring%20cloud%20sqs&packageName=io.pratik.springcloudsqs&dependencies=web,lombok), and then open the project in our favorite IDE. Select Gradle project and Java language. Last but not least, select Spring boot version `2.5.4`. Fill in the project metadata and click generate.

[Read more](https://auth0.com/blog/spring-cloud-streams-with-apache-kafka/?utm_source=content_synd&utm_medium=sc&utm_campaign=spring)