Microservices Example

Build distributed systems with MoroJS microservices. Docker-based architecture with service discovery, API gateway, and inter-service communication.

Microservices Architecture

This example demonstrates a complete microservices architecture with multiple MoroJS services communicating through events and HTTP APIs.

Service Architecture

typescript

1microservices/
2├── api-gateway/
3│   ├── src/
4│   │   ├── routes/
5│   │   ├── middleware/
6│   │   └── server.ts
7│   ├── Dockerfile
8│   └── package.json
9├── user-service/
10│   ├── src/
11│   │   ├── controllers/
12│   │   ├── services/
13│   │   └── server.ts
14│   ├── Dockerfile
15│   └── package.json
16├── order-service/
17│   ├── src/
18│   │   ├── controllers/
19│   │   ├── services/
20│   │   └── server.ts
21│   ├── Dockerfile
22│   └── package.json
23├── payment-service/
24│   ├── src/
25│   │   ├── controllers/
26│   │   ├── services/
27│   │   └── server.ts
28│   ├── Dockerfile
29│   └── package.json
30├── notification-service/
31│   ├── src/
32│   │   ├── handlers/
33│   │   ├── templates/
34│   │   └── server.ts
35│   ├── Dockerfile
36│   └── package.json
37├── docker-compose.yml
38├── k8s/
39│   ├── api-gateway.yaml
40│   ├── user-service.yaml
41│   ├── order-service.yaml
42│   └── ingress.yaml
43└── nginx.conf
44
45# Services:
46🔹 API Gateway - Route requests and handle authentication
47🔹 User Service - User management and profiles  
48🔹 Order Service - Order processing and management
49🔹 Payment Service - Payment processing and billing
50🔹 Notification Service - Email, SMS, and push notifications

Service Communication

  • • HTTP API calls
  • • Event-driven messaging
  • • Service discovery
  • • Circuit breakers

Data Management

  • • Database per service
  • • Event sourcing
  • • Distributed transactions
  • • Data consistency

Infrastructure

  • • Docker containers
  • • Kubernetes orchestration
  • • Load balancing
  • • Auto-scaling

Docker Compose Setup

Complete Docker Compose Configuration

typescript

1# docker-compose.yml
2version: '3.8'
3
4services:
5  # API Gateway
6  api-gateway:
7    build: ./api-gateway
8    ports:
9      - "3000:3000"
10    environment:
11      - NODE_ENV=production
12      - USER_SERVICE_URL=http://user-service:3001
13      - ORDER_SERVICE_URL=http://order-service:3002
14      - PAYMENT_SERVICE_URL=http://payment-service:3003
15      - NOTIFICATION_SERVICE_URL=http://notification-service:3004
16      - REDIS_URL=redis://redis:6379
17      - JWT_SECRET=your-jwt-secret
18    depends_on:
19      - user-service
20      - order-service
21      - payment-service
22      - notification-service
23      - redis
24    restart: unless-stopped
25
26  # User Service
27  user-service:
28    build: ./user-service
29    ports:
30      - "3001:3001"
31    environment:
32      - NODE_ENV=production
33      - DATABASE_URL=postgresql://postgres:password@postgres:5432/users_db
34      - REDIS_URL=redis://redis:6379
35      - JWT_SECRET=your-jwt-secret
36    depends_on:
37      postgres:
38        condition: service_healthy
39      redis:
40        condition: service_healthy
41    restart: unless-stopped
42
43  # Order Service
44  order-service:
45    build: ./order-service
46    ports:
47      - "3002:3002"
48    environment:
49      - NODE_ENV=production
50      - DATABASE_URL=postgresql://postgres:password@postgres:5432/orders_db
51      - USER_SERVICE_URL=http://user-service:3001
52      - PAYMENT_SERVICE_URL=http://payment-service:3003
53      - REDIS_URL=redis://redis:6379
54    depends_on:
55      - postgres
56      - redis
57      - user-service
58    restart: unless-stopped
59
60  # Payment Service
61  payment-service:
62    build: ./payment-service
63    ports:
64      - "3003:3003"
65    environment:
66      - NODE_ENV=production
67      - DATABASE_URL=postgresql://postgres:password@postgres:5432/payments_db
68      - STRIPE_SECRET_KEY=your-stripe-secret
69      - REDIS_URL=redis://redis:6379
70    depends_on:
71      - postgres
72      - redis
73    restart: unless-stopped
74
75  # Notification Service
76  notification-service:
77    build: ./notification-service
78    ports:
79      - "3004:3004"
80    environment:
81      - NODE_ENV=production
82      - REDIS_URL=redis://redis:6379
83      - EMAIL_SERVICE_URL=your-email-service
84      - SMS_SERVICE_URL=your-sms-service
85    depends_on:
86      - redis
87    restart: unless-stopped
88
89  # Shared Database
90  postgres:
91    image: postgres:15-alpine
92    environment:
93      - POSTGRES_MULTIPLE_DATABASES=users_db,orders_db,payments_db
94      - POSTGRES_USER=postgres
95      - POSTGRES_PASSWORD=password
96    volumes:
97      - postgres_data:/var/lib/postgresql/data
98      - ./scripts/create-multiple-postgresql-databases.sh:/docker-entrypoint-initdb.d/create-multiple-postgresql-databases.sh
99    ports:
100      - "5432:5432"
101    healthcheck:
102      test: ["CMD-SHELL", "pg_isready -U postgres"]
103      interval: 10s
104      timeout: 5s
105      retries: 5
106
107  # Redis for caching and messaging
108  redis:
109    image: redis:7-alpine
110    ports:
111      - "6379:6379"
112    volumes:
113      - redis_data:/data
114    healthcheck:
115      test: ["CMD", "redis-cli", "ping"]
116      interval: 10s
117      timeout: 3s
118      retries: 3
119
120  # Nginx Load Balancer
121  nginx:
122    image: nginx:alpine
123    ports:
124      - "80:80"
125    volumes:
126      - ./nginx.conf:/etc/nginx/nginx.conf
127    depends_on:
128      - api-gateway
129    restart: unless-stopped
130
131volumes:
132  postgres_data:
133  redis_data:

Next Steps