A microservices-based patient management system built with Spring Boot, implementing modern distributed architecture patterns including REST APIs, gRPC, event-driven communication, and API Gateway routing.
- Overview
- Architecture
- Services
- Technology Stack
- Prerequisites
- Getting Started
- API Documentation
- Development
- Deployment
- Testing
- Future Improvements
The Patient Management System is a distributed microservices application designed to handle patient data, authentication, billing, and analytics. The system follows microservices best practices with service separation, event-driven communication, and centralized API gateway routing.
- Patient Management: CRUD operations for patient records
- Authentication & Authorization: JWT-based authentication with role-based access
- Billing Integration: gRPC-based billing account creation
- Event-Driven Analytics: Real-time event processing via Kafka
- API Gateway: Centralized routing and JWT validation
- OpenAPI Documentation: Swagger/OpenAPI documentation for all REST services
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Applications โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ HTTP/REST
โ
โโโโโโโโโโผโโโโโโโโโ
โ API Gateway โ
โ (Port: 4004) โ
โ Spring Cloud โ
โ Gateway โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โ โ โ
โโโโโโโโโผโโโโโโโโ โโโโโโโโโโผโโโโโโโโโ โโโโโโโโโผโโโโโโโโ
โ Auth Service โ โ Patient Service โ โBilling Serviceโ
โ (Port: 4005) โ โ (Port: 4000) โ โ (Port: 4001) โ
โ โ โ โ โ gRPC: 9001 โ
โ - Login โ โ - CRUD Patients โ โ โ
โ - Validate โ โ - PostgreSQL โ โ - gRPC Server โ
โ - JWT โ โ โ โ โ
โ - PostgreSQL โ โ โ โ โ
โโโโโโโโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโ โโโโโโโโโฒโโโโโโโโ
โ โ
โ gRPC โ
โ โ
โ โ
โโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ
โ Kafka Message Broker โ
โ (Topic: patient) โ
โโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ
โ Event Consumption
โ
โโโโโโโโโผโโโโโโโโโ
โAnalytics Serviceโ
โ (Port: 4002) โ
โ โ
โ - Event Consumerโ
โ - Analytics โ
โโโโโโโโโโโโโโโโโโโ
- Synchronous REST: Client โ API Gateway โ Services
- Synchronous gRPC: Patient Service โ Billing Service
- Asynchronous Events: Patient Service โ Kafka โ Analytics Service
- JWT Validation: API Gateway โ Auth Service
1. Client โ API Gateway (POST /api/patients)
2. API Gateway โ Auth Service (JWT Validation)
3. API Gateway โ Patient Service (Create Patient)
4. Patient Service โ PostgreSQL (Save Patient)
5. Patient Service โ Billing Service (gRPC - Create Billing Account)
6. Patient Service โ Kafka (Publish Patient Event)
7. Analytics Service โ Kafka (Consume Event)
- Port: 4004
- Technology: Spring Cloud Gateway (WebFlux)
- Responsibilities:
- Route requests to appropriate services
- JWT token validation via Auth Service
- API documentation aggregation
- Routes:
/auth/**โ Auth Service/api/patients/**โ Patient Service (with JWT validation)/api-docs/patientsโ Patient Service Swagger/api-docs/authโ Auth Service Swagger
- Port: 4005
- Technology: Spring Boot, Spring Security, JWT
- Database: PostgreSQL
- Responsibilities:
- User authentication (login)
- JWT token generation and validation
- User management
- Endpoints:
POST /login- Authenticate user and generate JWTGET /validate- Validate JWT token
- Port: 4000
- Technology: Spring Boot, Spring Data JPA
- Database: PostgreSQL
- Responsibilities:
- Patient CRUD operations
- Integration with Billing Service (gRPC)
- Publishing patient events to Kafka
- Endpoints:
GET /patients- Get all patientsPOST /patients- Create new patientPUT /patients/{id}- Update patientDELETE /patients/{id}- Delete patient
- Port: 4001 (HTTP), 9001 (gRPC)
- Technology: Spring Boot, gRPC
- Responsibilities:
- Billing account management
- gRPC service for billing operations
- gRPC Service:
CreateBillingAccount- Create billing account for patient
- Port: 4002
- Technology: Spring Boot, Spring Kafka
- Responsibilities:
- Consume patient events from Kafka
- Process and analyze patient data
- Real-time analytics
- Java: 21
- Spring Boot: 3.5.7
- Spring Cloud: 2025.0.0
- Maven: Build tool
- Spring Cloud Gateway: API Gateway
- Spring Security: Authentication & Authorization
- Spring Data JPA: Database access
- gRPC: Inter-service communication
- Apache Kafka: Event streaming
- PostgreSQL: Relational database
- JWT (JJWT): Token-based authentication
- OpenAPI/Swagger: API documentation
- Protobuf: Data serialization for gRPC and events
- Docker: Containerization
- PostgreSQL: Database
- Kafka: Message broker
Before you begin, ensure you have the following installed:
- Java 21 or higher
- Maven 3.6+
- Docker and Docker Compose (for running infrastructure)
- PostgreSQL (or use Docker)
- Apache Kafka (or use Docker)
- Git
git clone <repository-url>
cd Patient-ManagementYou'll need to start PostgreSQL and Kafka. You can use Docker Compose:
# Create a docker-compose.yml in the root directory (see Deployment section)
docker-compose up -d postgres kafkaOr start them individually:
- PostgreSQL: Ensure PostgreSQL is running on port 5432
- Kafka: Ensure Kafka is running and accessible
# Build all services
mvn clean install
# Or build individual services
cd auth-service && mvn clean install
cd ../patient-service && mvn clean install
cd ../billing-service && mvn clean install
cd ../analytics-service && mvn clean install
cd ../api-gateway && mvn clean installStart services in the following order:
-
Auth Service:
cd auth-service mvn spring-boot:run -
Billing Service:
cd billing-service mvn spring-boot:run -
Analytics Service:
cd analytics-service mvn spring-boot:run -
Patient Service:
cd patient-service mvn spring-boot:run -
API Gateway:
cd api-gateway mvn spring-boot:run
- API Gateway: http://localhost:4004
- Auth Service: http://localhost:4005
- Patient Service: http://localhost:4000
- Billing Service: http://localhost:4001
- Analytics Service: http://localhost:4002
Access API documentation through the API Gateway:
- Patient Service API Docs: http://localhost:4004/api-docs/patients
- Auth Service API Docs: http://localhost:4004/api-docs/auth
Login
POST http://localhost:4004/auth/login
Content-Type: application/json
{
"email": "testuser@test.com",
"password": "password123"
}Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}Get All Patients (Requires JWT)
GET http://localhost:4004/api/patients
Authorization: Bearer <token>Create Patient (Requires JWT)
POST http://localhost:4004/api/patients
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "John Doe",
"email": "john.doe@example.com",
"address": "123 Main St",
"dateOfBirth": "1990-01-01",
"registeredDate": "2024-01-01"
}Update Patient (Requires JWT)
PUT http://localhost:4004/api/patients/{id}
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "John Doe Updated",
"email": "john.doe@example.com",
"address": "456 New St",
"dateOfBirth": "1990-01-01",
"registeredDate": "2024-01-01"
}Delete Patient (Requires JWT)
DELETE http://localhost:4004/api/patients/{id}
Authorization: Bearer <token>Pre-configured HTTP request files are available in the api-requests/ directory:
api-requests/auth-service/- Auth service requestsapi-requests/patient-service/- Patient service requestsgrpc-requests/billing-service/- gRPC requests
Patient-Management/
โโโ api-gateway/ # API Gateway service
โโโ auth-service/ # Authentication service
โโโ patient-service/ # Patient management service
โโโ billing-service/ # Billing service (gRPC)
โโโ analytics-service/ # Analytics service
โโโ integration-tests/ # Integration tests
โโโ api-requests/ # HTTP request files for testing
โโโ grpc-requests/ # gRPC request files
- Follow Java naming conventions
- Use meaningful variable and method names
- Add JavaDoc comments for public APIs
- Keep methods focused and single-purpose
- Create a new Spring Boot project
- Add service configuration in
api-gateway/src/main/resources/application.yml - Update this README with service details
- Add Dockerfile if needed
- Update docker-compose.yml if using Docker
gRPC services use Protocol Buffers for data serialization. Proto files are located in:
patient-service/src/main/proto/billing-service/src/main/proto/analytics-service/src/main/proto/
After modifying .proto files, rebuild the project to regenerate Java classes:
mvn clean compileEach service includes a Dockerfile. Build and run services individually:
# Build Docker image
docker build -t patient-service:latest ./patient-service
# Run container
docker run -p 4000:4000 patient-service:latestCreate a docker-compose.yml in the root directory to orchestrate all services:
version: '3.8'
services:
postgres:
image: postgres:15
environment:
POSTGRES_DB: db
POSTGRES_USER: admin_user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
kafka:
image: confluentinc/cp-kafka:latest
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
ports:
- "9092:9092"
depends_on:
- zookeeper
zookeeper:
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- "2181:2181"
# Add your services here...
volumes:
postgres_data:Run unit tests for each service:
cd <service-name>
mvn testIntegration tests are located in the integration-tests/ module:
cd integration-tests
mvn testUse the HTTP request files in api-requests/ directory or tools like:
- Postman
- cURL
- REST Client (VS Code extension)
- Add comprehensive unit and integration tests
- Implement database migrations (Flyway/Liquibase)
- Add distributed tracing (Zipkin/Jaeger)
- Implement circuit breakers for resilience
- Add health checks and metrics (Actuator, Prometheus)
- Externalize configuration (Config Server)
- Add service discovery (Eureka/Consul)
- Implement API versioning
- Add rate limiting
- Implement caching strategies
- Add correlation IDs for request tracking
- Create parent POM for dependency management
- Add comprehensive error handling
- Implement retry mechanisms
- Add CI/CD pipeline (GitHub Actions/Jenkins)
- Kubernetes deployment manifests
- Performance testing and optimization
- Security enhancements (OAuth2, RBAC)
- API documentation improvements
- Monitoring dashboards (Grafana)
Note: This is a development project. For production deployment, ensure proper security configurations, environment variable management, and infrastructure setup.