|
1 |
| -# Polling Publisher-Subscriber Microservice Pattern |
2 |
| - |
3 |
| -This project implements a **Polling Publisher-Subscriber** system using **Spring Boot** and **Apache Kafka**. It consists of two microservices: |
4 |
| - |
5 |
| -1. **Publisher Service** → Periodically polls a data source and publishes updates via Kafka. |
6 |
| -2. **Subscriber Service** → Listens to Kafka for updates and processes them. |
7 |
| - |
8 |
| -## 📌 **Project Structure** |
9 |
| -``` |
10 |
| -polling-publisher-subscriber/ |
11 |
| -│️— pom.xml (Parent POM) |
12 |
| -│️— README.md (This file) |
13 |
| -│ |
14 |
| -├── polling-service/ |
15 |
| -│ ├── src/main/java/com/iluwatar/polling/ |
16 |
| -│ ├── src/main/resources/application.yml |
17 |
| -│ ├── pom.xml |
18 |
| -│ └── README.md (Polling-specific documentation) |
19 |
| -│ |
20 |
| -├── subscriber-service/ |
21 |
| -│ ├── src/main/java/com/iluwatar/subscriber/ |
22 |
| -│ ├── src/main/resources/application.yml |
23 |
| -│ ├── pom.xml |
24 |
| -│ └── README.md (Subscriber-specific documentation) |
25 |
| -``` |
26 |
| - |
27 |
| -## 🚀 **Tech Stack** |
28 |
| -- **Spring Boot** (Microservices) |
29 |
| -- **Apache Kafka** (Messaging) |
30 |
| -- **Maven** (Build Tool) |
31 |
| - |
32 |
| -## 🛠 **Setup & Running** |
33 |
| -### 1️⃣ **Start Kafka & Zookeeper** |
34 |
| -If you don’t have Kafka installed, use Docker: |
35 |
| -```sh |
36 |
| -docker-compose up -d |
37 |
| -``` |
38 |
| - |
39 |
| -### 2️⃣ **Build the Project** |
40 |
| -```sh |
41 |
| -mvn clean install |
42 |
| -``` |
43 |
| - |
44 |
| -### 3️⃣ **Run Service** |
45 |
| -```sh |
46 |
| -mvn spring-boot:run |
47 |
| -``` |
| 1 | +## Polling-publisher |
| 2 | + |
| 3 | +--- |
| 4 | +**Title:** "Polling Publisher-Subscriber Microservice Pattern in Java: Mastering Asynchronous Messaging Elegantly" |
| 5 | +**ShortTitle:** Polling Pub/Sub |
| 6 | +**description:** "This project shows how to build a Polling Publisher-Subscriber system using Spring Boot and Apache Kafka. The Publisher Service polls data at regular intervals and sends updates to Kafka. The Subscriber Service listens to Kafka and processes the updates. It helps to separate data producers from consumers and is useful when real-time push is not possible." |
| 7 | + |
| 8 | +**Category:** Architectural |
| 9 | +**Language:** en |
| 10 | + |
| 11 | +**Tags:** |
| 12 | +- Spring Boot |
| 13 | +- Kafka |
| 14 | +- Microservices |
| 15 | +- Asynchronous Messaging |
| 16 | +- Decoupling |
| 17 | +--- |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Also Known As |
| 22 | +- Event-Driven Architecture |
| 23 | +- Asynchronous Pub/Sub Pattern |
| 24 | +- Message Queue-Based Polling System |
| 25 | + |
| 26 | +## Intent of Polling Publisher-Subscriber Pattern |
| 27 | +To decouple data producers and consumers in a distributed system, enabling asynchronous message-driven communication by polling a data source and transmitting updates via a message broker like Kafka. |
| 28 | + |
| 29 | +## Detailed Explanation of the Pattern |
| 30 | + |
| 31 | +### Real-World Analogy |
| 32 | +A news agency polls for latest updates and broadcasts them to newspapers and channels, which are then read by people independently. |
| 33 | + |
| 34 | +### In Plain Words |
| 35 | +One service polls for data and publishes messages to Kafka. Another service consumes and processes these messages asynchronously. |
| 36 | + |
| 37 | +### Wikipedia Says |
| 38 | +This pattern closely resembles the Publish–subscribe model. See: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern |
| 39 | + |
| 40 | +### Architecture |
| 41 | +Publisher → Kafka → Subscriber |
| 42 | + |
| 43 | +## Algorithm Logic (Spring Boot + Kafka) |
| 44 | + |
| 45 | +### Publisher Service |
| 46 | +- Polls data periodically using Spring Scheduler. |
| 47 | +- Publishes data to Kafka. |
| 48 | +- Exposes REST API for manual triggering. |
| 49 | + |
| 50 | +### Subscriber Service |
| 51 | +- Listens to Kafka topic using Spring Kafka. |
| 52 | +- Processes messages asynchronously. |
| 53 | + |
| 54 | +## When to Use This Pattern |
| 55 | +- When producer and consumer need decoupling. |
| 56 | +- When real-time push is unavailable, requiring polling. |
| 57 | +- When building event-driven microservices. |
| 58 | + |
| 59 | +## Java Tutorials |
| 60 | +- https://www.baeldung.com/spring-kafka |
| 61 | +- https://www.baeldung.com/spring-scheduled-tasks |
| 62 | + |
| 63 | +## Real-World Applications |
| 64 | +- Real-time reporting dashboards |
| 65 | +- System health check aggregators |
| 66 | +- IoT telemetry processing |
| 67 | +- Notification/event services |
| 68 | + |
| 69 | +## Benefits and Trade-offs |
| 70 | + |
| 71 | +### Benefits |
| 72 | +- Loose coupling between services |
| 73 | +- Scalable and asynchronous |
| 74 | +- Durable and fault-tolerant via Kafka |
| 75 | +- Extendable and modular |
| 76 | + |
| 77 | +### Trade-offs |
| 78 | +- Polling introduces a delay |
| 79 | +- Requires message broker setup |
| 80 | +- Increases deployment complexity |
| 81 | + |
| 82 | +## Related Design Patterns |
| 83 | +- Observer Pattern |
| 84 | +- Mediator Pattern |
| 85 | +- Message Queue Pattern |
| 86 | + |
| 87 | +## References and Credits |
| 88 | +- Apache Kafka Docs: https://kafka.apache.org/documentation/ |
| 89 | +- Spring Kafka Docs: https://docs.spring.io/spring-kafka |
| 90 | +- iluwatar/java-design-patterns |
0 commit comments