Skip to content

A production-ready, enterprise-grade toolkit for building scalable and maintainable microservices with NestJS, TypeScript, and Nx. NexusKit offers built-in service discovery, load balancing, dynamic scaling, and robust developer tooling β€” everything you need to accelerate distributed systems development.

Notifications You must be signed in to change notification settings

puneetkakkar/nexuskit

Repository files navigation

NexusKit: The Complete Microservices Toolkit

License: MIT Node.js TypeScript NestJS Nx

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.

πŸš€ Features

Core Architecture

  • πŸ—οΈ 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 Infrastructure

  • πŸ“‘ 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

Development Experience

  • πŸ”§ 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

πŸ—οΈ Architecture Overview

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

Key Libraries

  • @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

πŸ› οΈ Technology Stack

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+

πŸ“¦ Installation

Prerequisites

  • Node.js 18 or higher
  • npm or yarn package manager
  • Docker (for Consul)

Quick Start

  1. Clone the repository

    git clone https://github.com/your-username/mt-microservices-arch.git
    cd mt-microservices-arch
  2. Install dependencies

    npm install
    # or
    yarn install
  3. 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
  4. Start the development servers

    # Start all services
    npm run start
    
    # Start specific service
    npm run start service-1
    npm run start service-2

πŸš€ Usage

Dynamic Service Management

The project includes powerful scripts for managing multiple service instances:

Start 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

Monitor Service Status

# Check all services
./scripts/status.sh

# Check specific service
./scripts/status.sh service-2

Test Load Balancing

# 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 Integration

# Test ZooKeeper service discovery and registration
./scripts/test-zookeeper.sh

Stop Services

# Stop all service-2 instances
./scripts/stop-service.sh service-2

# Stop all service-1 instances
./scripts/stop-service.sh service-1

Creating a New Service

  1. Generate a new service using Nx

    npx nx generate @nx/nest:application my-service
  2. 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 {}

Using HTTP Service Clients

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(),
      };
    }
  }
}

Configuration Management

Create a bootstrap.yaml file in your service directory:

config:
  service:
    name: 'My Service'

πŸ”§ Configuration

Environment Variables

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

Service Configuration

Each service can be configured through:

  • Environment variables
  • Bootstrap configuration files (bootstrap.yaml, bootstrap.json)
  • Runtime configuration injection

Health Check Configuration

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',
}

πŸ§ͺ Testing

Unit Tests

# 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

Load Balancing Tests

# 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)
# ...

πŸ“Š Monitoring and Health Checks

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

Health Check Endpoints

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"}

Service Registry Health Monitoring

Consul Health Monitoring

# 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

ZooKeeper Health Monitoring

# 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>

πŸ”’ Security

  • 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

πŸš€ Production Deployment

Docker Deployment

# Build services
npx nx build service-1
npx nx build service-2

# Run with Docker Compose
docker-compose up -d

Kubernetes Deployment

apiVersion: 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'

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

Code Style

This project uses:

  • ESLint for code linting
  • Prettier for code formatting
  • TypeScript strict mode
  • Conventional commit messages

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

πŸ™ Acknowledgments

  • 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

About

A production-ready, enterprise-grade toolkit for building scalable and maintainable microservices with NestJS, TypeScript, and Nx. NexusKit offers built-in service discovery, load balancing, dynamic scaling, and robust developer tooling β€” everything you need to accelerate distributed systems development.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published