A production-ready, enterprise-grade microservices toolkit built with NestJS and Nx. NexusKit provides a robust, scalable, and maintainable foundation for building distributed systems with built-in service discovery, load balancing, dynamic scaling, multi-tenancy support, and extensible libraries for CQRS, gRPC, GraphQL, resilience, and more.
- ποΈ Monorepo Structure: Built with Nx for efficient development and build processes
- π Service Discovery: Automatic service registration and discovery using Consul or ZooKeeper
- βοΈ Load Balancing: Intelligent load balancing with configurable strategies
- π Dynamic Scaling: Support for multiple instances of the same service with dynamic port allocation
- π HTTP Client: Declarative HTTP service clients with automatic service resolution
- π Health Checks: Built-in health monitoring with configurable timeouts
- π‘οΈ Graceful Shutdown: Proper application lifecycle management
- π Real-time Monitoring: Live service status and health tracking
- π‘ Service Registry: Centralized service registration and management via Consul or ZooKeeper
- π§ Configuration Management: Flexible configuration loading from multiple sources
- π Service Monitoring: Real-time service health and status tracking
- π Retry Mechanisms: Robust error handling with configurable retry policies
- π― Metadata Management: Comprehensive service metadata and tagging system
- π Auto-scaling: Dynamic service instance management with load balancing
- π§ TypeScript First: Full TypeScript support with strict typing
- π¦ Modular Design: Clean separation of concerns with reusable libraries
- π§ͺ Testing Ready: Built-in testing infrastructure with Jest
- π Code Quality: ESLint and Prettier integration for consistent code style
- π Hot Reload: Development server with automatic reloading
- π οΈ Management Scripts: Automated scripts for service lifecycle management
This project follows a modular microservice architecture with the following key components:
βββ apps/ # Application services
β βββ service-1/ # Example microservice 1 (port 3333)
β βββ service-2/ # Example microservice 2 (port 3334)
βββ libs/ # Shared libraries
β βββ bootstrap/ # Configuration management
β βββ client/ # HTTP client framework
β βββ cloud/ # Cloud-native abstractions
β βββ common/ # Shared utilities and interfaces
β βββ consul/ # Service discovery and registry
β βββ loadbalancer/ # Load balancing strategies
β βββ zookeeper/ # ZooKeeper service discovery and registry
βββ scripts/ # Service management scripts
β βββ start-service.sh # Dynamic service startup
β βββ stop-service.sh # Service shutdown
β βββ status.sh # Service status monitoring
β βββ test-loadbalancer.sh # Load balancing tests
@nexuskit/bootstrap: Configuration management with file-based loading@nexuskit/client: Declarative HTTP service clients with service discovery@nexuskit/cloud: Cloud-native service abstractions and registry management@nexuskit/common: Shared utilities, interfaces, and common functionality@nexuskit/consul: Consul integration for service discovery and health checks@nexuskit/loadbalancer: Configurable load balancing with multiple strategies@nexuskit/zookeeper: ZooKeeper integration for service discovery and registry
| Component | Technology | Version |
|---|---|---|
| Runtime | Node.js | 18+ |
| Framework | NestJS | 10.0+ |
| Language | TypeScript | 5.0+ |
| Build System | Nx | 17.0+ |
| Service Registry | Consul/ZooKeeper | 1.21+/3.8+ |
| HTTP Client | Got | 11.8.2 |
| Testing | Jest | 29.0+ |
| Linting | ESLint | 8.0+ |
| Formatting | Prettier | 3.0+ |
- Node.js 18 or higher
- npm or yarn package manager
- Docker (for Consul)
-
Clone the repository
git clone https://github.com/your-username/mt-microservices-arch.git cd mt-microservices-arch -
Install dependencies
npm install # or yarn install -
Start Service Registry (required for service discovery)
# Using Consul (recommended) docker run -d --name consul -p 8500:8500 hashicorp/consul:latest # Or using ZooKeeper docker run -d --name zookeeper -p 2181:2181 zookeeper:3.8 # Or install locally # Follow instructions at https://www.consul.io/docs/install or https://zookeeper.apache.org/doc/current/zookeeperStarted.html
-
Start the development servers
# Start all services npm run start # Start specific service npm run start service-1 npm run start service-2
The project includes powerful scripts for managing multiple service instances:
# Start 3 instances of service-2 starting from port 3334
./scripts/start-service.sh service-2 3 3334
# Start 2 instances of service-1 starting from port 3333
./scripts/start-service.sh service-1 2 3333
# Start 1 instance of service-2 on default port 3334
./scripts/start-service.sh service-2# Check all services
./scripts/status.sh
# Check specific service
./scripts/status.sh service-2# Test load balancing for service-2 with 10 requests
./scripts/test-loadbalancer.sh service-2 10
# Test load balancing for service-1 with 5 requests
./scripts/test-loadbalancer.sh service-1 5# Test ZooKeeper service discovery and registration
./scripts/test-zookeeper.sh# Stop all service-2 instances
./scripts/stop-service.sh service-2
# Stop all service-1 instances
./scripts/stop-service.sh service-1-
Generate a new service using Nx
npx nx generate @nx/nest:application my-service
-
Configure the service module
import { Module } from '@nestjs/common'; import { BootstrapModule } from '@nexuskit/bootstrap'; import { ClientModule } from '@nexuskit/client'; import { CloudModule } from '@nexuskit/cloud'; import { ConsulModule } from '@nexuskit/consul'; import { ZookeeperModule } from '@nexuskit/zookeeper'; import { LoadBalancerModule } from '@nexuskit/loadbalancer'; @Module({ imports: [ BootstrapModule.forRoot(), // Using Consul ConsulModule.forRoot({ host: 'localhost', port: '8500', promisify: true, secure: false, }), // Or using ZooKeeper ZookeeperModule.forRoot({ host: 'localhost:2181', retryAttempts: 6000, }), CloudModule.forRoot({ registry: { discoverer: 'consul', // or 'zookeeper' service: { name: 'my-service', address: 'localhost', port: parseInt(process.env.PORT) || 3000, }, discovery: { type: 'http', http: `http://192.168.1.201:${process.env.PORT || 3000}/api/health`, interval: 10, timeout: '5s', failFast: false, scheme: 'http', }, heartbeat: { enabled: false, ttlInSeconds: 30, }, }, }), ClientModule.forRoot(), LoadBalancerModule.forRoot(), ], }) export class AppModule {}
import { Controller, Get } from '@nestjs/common';
import { HttpClient, HttpServiceClient } from '@nexuskit/client';
@Controller()
export class AppController {
@HttpServiceClient('service-2', {
timeout: 5000, // 5 second timeout
retry: 2, // retry 2 times
})
serviceInstance: HttpClient;
@Get('/service-2')
async getServiceData() {
try {
const svcData = await this.serviceInstance.get('api/');
return svcData.body;
} catch (error) {
return {
error: 'Service-2 is not available',
message: error.message,
timestamp: new Date().toISOString(),
};
}
}
}Create a bootstrap.yaml file in your service directory:
config:
service:
name: 'My Service'| Variable | Description | Default |
|---|---|---|
PORT |
Service port | 3333 |
NODE_ENV |
Environment | development |
CONSUL_HOST |
Consul host | localhost |
CONSUL_PORT |
Consul port | 8500 |
ZOOKEEPER_HOST |
ZooKeeper host | localhost |
ZOOKEEPER_PORT |
ZooKeeper port | 2181 |
Each service can be configured through:
- Environment variables
- Bootstrap configuration files (
bootstrap.yaml,bootstrap.json) - Runtime configuration injection
discovery: {
type: 'http',
http: `http://192.168.1.201:${process.env.PORT || 3333}/api/health`,
interval: 10, // Check every 10 seconds
timeout: '5s', // 5 second timeout
failFast: false, // Don't fail fast on health check failure
scheme: 'http',
}# Run all tests
npm run test
# Run tests for specific service
npm run test service-1
# Run e2e tests
npm run e2e
# Run affected tests
npm run affected:test# Test load balancing with multiple instances
./scripts/test-loadbalancer.sh service-2 20
# Expected output shows requests distributed across instances:
# Request 1: abc123 (port 3334)
# Request 2: def456 (port 3335)
# Request 3: abc123 (port 3334)
# ...The framework provides comprehensive health monitoring:
- Service Health: Automatic health check registration with Consul
- Dynamic Timeouts: Configurable health check timeouts (5s default)
- Service Discovery: Real-time service availability tracking
- Load Balancer Health: Health-aware load balancing across instances
- Instance Monitoring: Individual instance health tracking
Each service automatically exposes a health endpoint:
# Check service health
curl http://localhost:3333/api/health
# Response: {"status":"ok","timestamp":"2025-07-05T00:30:00.000Z"}
# Check service-2 health
curl http://localhost:3334/api/health
# Response: {"status":"ok","timestamp":"2025-07-05T00:30:00.000Z"}# View all services in Consul
curl http://localhost:8500/v1/agent/services | jq
# View health checks
curl http://localhost:8500/v1/agent/checks | jq
# View healthy service instances
curl "http://localhost:8500/v1/health/service/service-2?passing=true" | jq# View all services in ZooKeeper
docker exec zookeeper zkCli.sh -server localhost:2181 ls /swft-mt-service
# View service details
docker exec zookeeper zkCli.sh -server localhost:2181 get /swft-mt-service/service__service-2__1.0.0-<uuid>
# View service instances
docker exec zookeeper zkCli.sh -server localhost:2181 ls /swft-mt-service/service__service-2__1.0.0-<uuid>- Graceful Shutdown: Proper signal handling and cleanup
- Error Handling: Comprehensive error handling with retry mechanisms
- Service Isolation: Service-level isolation and security boundaries
- Configuration Security: Secure configuration management
- Health Check Security: Configurable health check endpoints
# Build services
npx nx build service-1
npx nx build service-2
# Run with Docker Compose
docker-compose up -dapiVersion: apps/v1
kind: Deployment
metadata:
name: service-1
spec:
replicas: 3
selector:
matchLabels:
app: service-1
template:
metadata:
labels:
app: service-1
spec:
containers:
- name: service-1
image: your-registry/service-1:latest
ports:
- containerPort: 3333
env:
- name: PORT
value: '3333'We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project uses:
- ESLint for code linting
- Prettier for code formatting
- TypeScript strict mode
- Conventional commit messages
This project is licensed under the MIT License - see the LICENSE file for details.
- Documentation: Wiki
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- NestJS - Progressive Node.js framework
- Nx - Build system with first-class support for many frontend and backend technologies
- Consul - Service mesh solution providing a full featured control plane
- ZooKeeper - Distributed coordination service for distributed applications
- TypeScript - Typed JavaScript at scale
Built with β€οΈ for the microservices community