84 lines
2.2 KiB
Markdown
84 lines
2.2 KiB
Markdown
# Microservices Architecture Example
|
|
|
|
## Service Decomposition
|
|
|
|
### 1. Sensor Data Service
|
|
**Responsibility**: Sensor data ingestion, validation, and storage
|
|
```
|
|
Port: 8001
|
|
Database: sensor_db (MongoDB)
|
|
Endpoints:
|
|
- POST /sensors/data # Ingest sensor readings
|
|
- GET /sensors/{id}/data # Get sensor history
|
|
- GET /sensors # List sensors
|
|
```
|
|
|
|
### 2. Room Management Service
|
|
**Responsibility**: Room metrics, aggregations, and space management
|
|
```
|
|
Port: 8002
|
|
Database: room_db (MongoDB)
|
|
Endpoints:
|
|
- GET /rooms # List rooms
|
|
- GET /rooms/{id}/metrics # Current room metrics
|
|
- GET /rooms/{id}/history # Historical room data
|
|
```
|
|
|
|
### 3. Analytics Service
|
|
**Responsibility**: Data analysis, reporting, and insights
|
|
```
|
|
Port: 8003
|
|
Database: analytics_db (PostgreSQL/ClickHouse)
|
|
Endpoints:
|
|
- GET /analytics/summary # Dashboard summary
|
|
- GET /analytics/trends # Trend analysis
|
|
- GET /analytics/reports/{id} # Generated reports
|
|
```
|
|
|
|
### 4. Notification Service
|
|
**Responsibility**: Alerts, events, and real-time notifications
|
|
```
|
|
Port: 8004
|
|
Database: events_db (MongoDB)
|
|
Message Queue: RabbitMQ/Kafka
|
|
Endpoints:
|
|
- POST /notifications/send # Send notification
|
|
- GET /events # System events
|
|
- WebSocket: /ws/notifications # Real-time alerts
|
|
```
|
|
|
|
### 5. API Gateway
|
|
**Responsibility**: Request routing, authentication, rate limiting
|
|
```
|
|
Port: 8000
|
|
Routes all requests to appropriate services
|
|
Handles CORS, authentication, logging
|
|
```
|
|
|
|
## Inter-Service Communication
|
|
|
|
### Synchronous (HTTP/REST)
|
|
```python
|
|
# Analytics Service calling Sensor Service
|
|
import httpx
|
|
|
|
async def get_sensor_data(sensor_id: str):
|
|
async with httpx.AsyncClient() as client:
|
|
response = await client.get(f"http://sensor-service:8001/sensors/{sensor_id}/data")
|
|
return response.json()
|
|
```
|
|
|
|
### Asynchronous (Message Queue)
|
|
```python
|
|
# Sensor Service publishes event
|
|
await message_queue.publish("sensor.data.received", {
|
|
"sensor_id": "sensor_001",
|
|
"timestamp": datetime.utcnow(),
|
|
"data": sensor_reading
|
|
})
|
|
|
|
# Room Service subscribes to event
|
|
@message_queue.subscribe("sensor.data.received")
|
|
async def handle_sensor_data(message):
|
|
await room_service.update_room_metrics(message.data)
|
|
``` |