An open API service indexing awesome lists of open source software.

https://github.com/asirwad/reactive-restapi-with-kafka-and-webflux


https://github.com/asirwad/reactive-restapi-with-kafka-and-webflux

apache-kafka spring-boot spring-boot-reactive webflux

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# 🚀 Spring Boot Kafka Reactive Project

![Java Version](https://img.shields.io/badge/Java-17+-orange)
![Kafka](https://img.shields.io/badge/Apache_Kafka-2.13-000?logo=apachekafka)
![Maven](https://img.shields.io/badge/Maven-3.8+-blueviolet)

A modern Spring Boot implementation demonstrating Kafka integration with Reactive Streams. Features real-time Wikimedia data processing with DynamoDB persistence.

## 📚 Table of Contents
- [✨ Features](#-features)
- [⚙️ Requirements](#️-requirements)
- [🚦 Getting Started](#-getting-started)
- [🧠 Key Concepts](#-key-concepts)
- [🎯 Demo Application](#-demo-application)

## ✨ Features
- Real-time data streaming from Wikimedia
- Reactive Kafka producers/consumers
- DynamoDB integration for data persistence
- Custom serialization/deserialization
- Consumer group management
- Offset tracking implementation

## ⚙️ Requirements
- ![Java](https://img.shields.io/badge/Java-17+-orange)
- ![Kafka](https://img.shields.io/badge/Apache_Kafka-3.5+-000?logo=apachekafka)
- ![Maven](https://img.shields.io/badge/Maven-3.8+-blueviolet)

## 🚦 Getting Started

### 🐳 Kafka Setup

Start Kafka Services

```bash
# Start ZooKeeper
$ bin/zookeeper-server-start.sh config/zookeeper.properties

# Start Kafka Broker (in new terminal)
$ bin/kafka-server-start.sh config/server.properties
```

### 🛠️ Project Setup

Clone and Run

```bash
$ git clone https://github.com/Asirwad/Reactive-RESTAPI-with-Kafka-and-Webflux
$ cd Reactive-RESTAPI-with-Kafka-and-Webflux
$ ./mvnw spring-boot:run
```

## 🧠 Key Concepts

📦 Kafka Architecture Overview

![Kafka Architecture](img/kafka_overview.png)

| Component | Description |
|-----------------|----------------------------------------------|
| **Producer** | Publishes messages to topics |
| **Consumer** | Subscribes and processes messages |
| **Broker** | Manages data storage and distribution |
| **ZooKeeper** | Handles cluster coordination |

🏗️ Kafka Cluster

![Kafka Cluster](img/kafka_cluster.png)

- Distributed message broker system
- Horizontal scaling capabilities
- Automatic failover handling

📮 Kafka Producer

![Producer Flow](img/kafka_producer.png)

```java
// Example Reactive Producer
public Mono> sendMessage(String topic, String message) {
return kafkaSender.send(
Mono.just(SenderRecord.create(topic, null, null, null, message, null))
);
}
```

📥 Kafka Consumer

![Consumer Flow](img/kafka_consumer.png)

```java
// Example Reactive Consumer
@Bean
public ReceiverOptions receiverOptions() {
return ReceiverOptions.create(consumerProps())
.subscription(Collections.singleton("wikimedia.recentchange"));
}
```

📂 Topics & Partitions

![Topic Structure](img/kafka_topic.png)

| Feature | Benefit |
|-----------------|----------------------------------------------|
| Partitions | Enable parallel processing |
| Replication | Ensure data redundancy |
| Retention | Configurable message persistence |

🔢 Offsets & Consumer Groups

![Offset Management](img/kafka_offset.png)

- **Offset Tracking**: Consumer position management
- **Group Coordination**: Parallel message processing
- **Rebalancing**: Automatic partition redistribution

![Consumer Groups](img/kafka_consumer_groups.png)

## 🎯 Demo Application

### 🔥 Real-time Pipeline
![Application Flow](img/kafka_demo_application.png)

📡 Producer Implementation

**Wikimedia Stream Processor**
- Reactive HTTP client for stream consumption
- Kafka Template for message publishing
- Backpressure management
- Error handling with retries

```java
webClient.get()
.uri("/v2/stream/recentchange")
.retrieve()
.bodyToFlux(String.class)
.doOnNext(event -> kafkaTemplate.send("wikimedia.recentchange", event))
.subscribe();
```

💾 Consumer Implementation

**DynamoDB Persistence**
- Batch record processing
- Exponential backoff strategy
- Consumer group management
- Offset commit strategies

```java
@Bean
public Consumer>> dynamoDbSaver() {
return flux -> flux
.bufferTimeout(100, Duration.ofMillis(500))
.flatMap(batch -> dynamoService.saveBatch(batch))
.subscribe();
}
```

## 🛠️ Troubleshooting

Common Issues

**⚠️ ZooKeeper Connection Problems**
- Verify zookeeper.properties configuration
- Check for port conflicts (default 2181)

**⚠️ Consumer Lag**
- Monitor with `kafka-consumer-groups.sh`
- Adjust `max.poll.records` if needed

**⚠️ Serialization Errors**
- Validate message formats
- Check key/value serializer configurations