This project demonstrates a microservices-based architecture built using Spring Boot, where two services — User Service and Email Notification Service — communicate asynchronously using Apache Kafka.
When a user registers through the User Service, the service:
- Saves the user data into MySQL database.
- Publishes a message to a Kafka topic.
The Email Notification Service listens to that Kafka topic as a consumer and sends an email notification to the registered user.
Both services are containerized using Docker and integrated with:
- Kafka (via Confluent Kafka image)
- Zookeeper
- MySQL
All services communicate within a single Docker network.
┌───────────────────────────┐
│ User Service │
│ (Spring Boot + MySQL) │
└──────────────┬─────────────┘
│
Kafka Producer (topic: user_registration)
│
▼
┌───────────────────────────┐
│ Kafka Broker + Zookeeper │
└──────────────┬─────────────┘
│
Kafka Consumer (topic: user_registration)
│
▼
┌───────────────────────────┐
│ Email Notification Service │
│ (Spring Boot + Mail) │
└────────────────────────────┘
- Backend: Spring Boot (Java 17)
- Message Broker: Apache Kafka
- Database: MySQL
- Containerization: Docker, Docker Compose
- Mailing: Spring Boot Starter Mail
- ORM: Spring Data JPA, Hibernate
- Build Tool: Maven
In your docker-compose.yml:
mysql-db:
image: mysql:8
container_name: mysql-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: userdb
ports:
- "3307:3306"
networks:
- microservice-network
zookeeper:
image: confluentinc/cp-zookeeper:7.4.0
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ports:
- "2181:2181"
networks:
- microservice-network
kafka:
image: confluentinc/cp-kafka:7.4.0
container_name: kafka
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
networks:
- microservice-network
server.port=8081
spring.application.name=user-service
# Database Configuration
spring.datasource.url=jdbc:mysql://mysql-db:3306/userdb?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# Kafka Configuration
spring.kafka.bootstrap-servers=kafka:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.topic.name=user_registration
# Use OpenJDK 17 base image
FROM openjdk:17-jdk-alpine
# Set working directory
WORKDIR /app
# Copy Maven jar file
COPY target/*.jar app.jar
# Expose port
EXPOSE 8080
# Run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
mvn clean package -DskipTests
docker-compose build --no-cache
docker-compose up