Skip to content

This repository demonstrates the implementation of Nestjs micro-services with RabbitMQ in nx workspaces. Postgresql is used for data persistency.

Notifications You must be signed in to change notification settings

arifshariati/nx-nestjs-microservices

Repository files navigation

Nestjs Microservices with nx workspaces

nx workspaces comes with many benifits, once of which is scalibility and development speed. In this repo i have demonstrated, how microservices can be developed/created in Nestjs using nx workspaces.

Tech stack for this project is below;

  1. nx workspaces
  2. Nestjs
  3. Graphql
  4. Postgresql
  5. Docker
  6. Microservices
  7. RabbitMQ

Architecture

Let's have a look at the architecture for this project.

Microservices Architecture

RabbitMQ as message broker comes between microservices and our API server. In real world projects, microservices implementation focus on loosely code base and database dependency.

Entities

Though entity definition does not add much value to your understanding, however i feel it is good to share for better understanding.

User Entity

import { ObjectType, Field } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
@ObjectType()
class User {
    @PrimaryGeneratedColumn('uuid')
    @Field()
    id: string;

    @Column()
    @Field()
    firstName: string;

    @Column()
    @Field()
    lastName: string;

    @Column()
    @Field()
    email: string;

    @Column()
    @Field()
    password: string;

    @Column()
    @Field()
    createdAt: Date;

    @Column({ nullable: true })
    @Field({ nullable: true })
    updatedAt?: Date;
}

export default User;

Post Entity

import { ObjectType, Field } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
@ObjectType()
class Post {
    @PrimaryGeneratedColumn('uuid')
    @Field()
    id: string;

    @Column()
    @Field()
    title: string;

    @Column()
    @Field()
    body: string;

    @Column()
    @Field()
    createdAt: Date;

    @Column({ nullable: true })
    @Field({ nullable: true })
    updatedAt?: Date;
}

export default Post;

RabbitMQ as message broker

RabbitMQ plays vital role in implementing microservices while it keeps communication between our producers and consumers. You can try our Redis as well.

Producer Configuration

ClientsModule.register([
{
  name: 'USER_SERVICE',
  transport: Transport.RMQ,
  options: {
    urls: [process.env.RABBITMQ_URI],
    queue: 'users',
    queueOptions: {
      durable: true
    }
  }
}])

Consumer Configuration

const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.RMQ,
  options: {
    urls: ['amqp://guest:guest@127.0.0.1:5672/vhost'],
    queue: 'users',
    queueOptios: {
      durable: true
    }
  }
});

Below are RabbitMQ Connection and Queue Dashboard. RabbitMQ Queue Graph RabbitMQ Connection

Docker

Docker containers are used to spin RabbitMQ and Postgresql.

version: "3"

services:
# postgresql
# ***************************************
  postgres:
    image: postgres:latest
    container_name: nx-nestjs-microservice-pg
    restart: always
    ports:
      - "5432:5432"
    volumes:
      - ./pg/pgData:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    networks:
      - nx-nestjs-microservice
# rabbitmq 
# ***************************************
  rabbitmq:
    image: rabbitmq:3-management
    container_name: rabbitmq
    environment:
      RABBITMQ_DEFAULT_VHOST: vhost
    ports:
      - 5672:5672
      - 15672:15672
    networks:
      - nx-nestjs-microservice
networks:
  nx-nestjs-microservice:

createpost

Graphql Playground

If everything is setup properly, you should be able to experiment with microservices and all should work as expected.

createpost

createpost

posts

createpost

users

createpost

How to Install ?

clone this repo on your machine, go to projec directory and install dependencies.

npm i

Once depencies are install, while being in project root folder, run below to spin docker container for RabbitMQ and Postgresql.

docker-compose up -d

Start Project

run below commands in separate terminals to spin API server, user microsevice and post microsevice.

# API server 
nx serve api 

# user microservice 
nx serve ms-user 

# post microservice 
nx serve ms-post

🚀 That's it, you have made it running project

I appreciate reading all instructions, though may seem bit complicated at first, however, will be quite easy once you get familiar with microservices implementations.

Do share your thoughts

About

This repository demonstrates the implementation of Nestjs micro-services with RabbitMQ in nx workspaces. Postgresql is used for data persistency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published