Introduction
In modern microservices architectures, real-time event-driven communication is essential for building responsive applications. This post walks through implementing Kafka-based admin notifications across two independent Spring Boot microservices: a Todo App (handling user management) and a Blog service (handling content and comments).
Architecture Overview
Our setup consists of:
- Todo App Microservice (Port: 8081) - Manages users and authentication
- Blog Microservice (Port: 8083) - Manages blog posts and comments
- Kafka - Message broker for inter-service communication
- WebSocket - Real-time push notifications to frontend
- React Frontend - Displays toast and bell notifications
Both services run independently but communicate through Kafka topics to deliver real-time notifications to administrators.
Use Cases
- Todo App: Admin receives notification when any user logs in
- Blog Service: Admin receives notification when any user posts a comment (since comments require moderation)
Implementation Steps
Step 1: Create BlogProducer.java
The producer is responsible for publishing events to Kafka when significant actions occur in your application.
Step 2: Create BlogConsumer.java
The consumer listens to Kafka messages and forwards them to WebSocket clients in real-time.
Step 3: Create WebSocketConfig.java
Configure WebSocket endpoints to enable real-time communication with the frontend.
Step 4: Update CommentController/Service
Integrate the producer into your existing comment creation logic.
Step 5: Update Frontend - AdminNotifications.js
Connect to the WebSocket endpoint and display notifications.
Step 6: Required Dependencies
Add dependencies to your pom.xml.
Add to configure
application.properties.
The Complete Flow
- User posts a comment on a blog post
- CommentController saves the comment and calls
BlogProducer
- BlogProducer publishes message to Kafka topic
blog-notifications
- BlogConsumer (listening to Kafka) receives the message
- BlogConsumer forwards message to WebSocket endpoint
/topic/admin-notifications
- Frontend (connected via WebSocket) receives the notification
- Toast notification appears on screen
- Bell icon updates with unread count
Testing Your Implementation
- Start Kafka and Zookeeper
- Start both microservices (Todo App and Blog)
- Login as an admin user
- Open browser console to verify WebSocket connection
- Post a comment (either as admin or regular user)
- Verify toast notification appears
- Verify bell icon shows unread count
Benefits of This Architecture
- Decoupled Services: Todo and Blog services remain independent
- Scalability: Kafka handles high message throughput
- Real-time Updates: WebSocket provides instant notifications
- Event-Driven: Easy to add more notification types
- Fault Tolerance: Kafka persists messages if consumers are temporarily down
Extending the System
You can easily extend this pattern for additional notifications:
- User registration events
- Post publication notifications
- Comment approval/rejection alerts
- System-wide announcements
Simply create new Kafka topics and add corresponding producers/consumers for each event type.
Conclusion
By combining Kafka for inter-service communication and WebSocket for real-time frontend updates, we've built a robust notification system across independent microservices. This architecture scales well and provides the foundation for building more complex event-driven features in your application.
No comments:
Post a Comment