Skip to content

sandhuharjodh/grpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grpc

Project: Microservices with gRPC and Node.js

This project demonstrates a microservices architecture using GRPC for communication and Node.js for server-side development. It consists of three microservices:

Understanding Microservice Communication: Enter gRPC Before delving into the User Service, let's establish a core concept: gRPC (Remote Procedure Call). Imagine you have separate programs on different machines that need to talk to each other as if they were functions within a single program. That's the magic of gRPC! gRPC is an open-source framework designed for high-performance, low-latency communication between services. It leverages features like:

• HTTP/2: This advanced transport protocol enables efficient data transfer with features like multiplexing (handling multiple requests/responses simultaneously) and header compression.

• Protocol Buffers: This data serialization format (explained next) ensures efficient and platform-neutral data exchange between services.

• Streaming: gRPC supports various data streaming patterns, allowing for real-time data flow between services.

Why Use gRPC?

Here's what makes gRPC a compelling choice for microservice communication: • Performance: gRPC's combination of HTTP/2 and Protocol Buffers leads to faster and more efficient communication compared to traditional REST APIs using JSON.

• Scalability: gRPC is well-suited for distributed systems, handling a high volume of requests efficiently.

• Strong Typing: gRPC enforces data types, leading to fewer errors and easier development

• Platform Neutrality: gRPC works seamlessly across various programming languages, making it ideal for heterogeneous microservice environments.

Introducing Protocol Buffers and Protobuf Files (.proto) gRPC relies on Protocol Buffers, a language-neutral mechanism for defining data structures. These structures are specified in .proto files. Here's how it works:

1. You define your data structures (messages and fields) in a .proto file, specifying data types (strings, integers, etc.).

2. The protoc compiler processes the .proto file and generates code for your chosen programming language (e.g., Node.js, Java, Python).

3. This generated code provides classes and functions for working with the defined data structures. Sending and receiving data between services becomes efficient and type-safe.

Protobuf vs. JSON: Choosing the Right Tool

While both JSON (JavaScript Object Notation) and Protocol Buffers are used for data exchange, there are key differences:

• Readability: JSON is human-readable, making it easier to understand message structures for debugging purposes.

• Performance and Size: Protocol Buffers are significantly smaller and faster to parse than JSON, leading to better performance in
 microservices.

• Schema Enforcement: Protocol Buffers enforce data types, preventing errors during data exchange. JSON lacks this built-in type safety.

In microservices, gRPC with Protocol Buffers is often preferred due to its performance benefits, strong typing, and smaller message sizes, leading to faster and more reliable communication.

1. User Service (user-ms): Manages user data (ID, name, department).

2. Post Service (post-ms): Manages post data (title, user ID, post ID).

3. Subpost Service (subpost-ms): Manages subpost data (content, post ID, subpost ID).

Project Structure:

    project_name/
    ├── package.json
    ├── package-lock.json
    ├── protos/
    │   ├── posts.proto
    │   ├── subposts.proto
    │   └── users.proto
    ├── user-ms/
    │   └── main.js
    ├── post-ms/
    │   └── main.js
    └── subpost-ms/
        └── main.js
    └── main.js

Starting the Project: 1. Install Dependencies:

    ◦  Open your terminal or command prompt and navigate to the project directory.
    
    ◦  Run the following command to install the required Node.js modules:
        npm install


2.  Start the Services:

    ◦  Main Server:

        ▪  Use nodemon to start the main server with live reload functionality:
        
        nodemon main.js


    ◦  Microservices:
        ▪  Each microservice has its own start script defined in the package.json file. You can use these scripts to start individual services:
            npm run start-user-ms  # Starts the User Service (user-ms)
            npm run start-post-ms  # Starts the Post Service (post-ms)
            npm run start-subpost-ms  # Starts the Subpost Service (subpost-ms)

Explanation:

•  npm install downloads and installs the dependencies listed in your package.json file, which are required for your project to function.
•  Nodemon is a useful tool that starts your Node.js application and automatically restarts it whenever you make changes to your code. This helps with development as you don't need to manually restart the server after every code change.
•  The provided npm start scripts (assuming they are defined in your package.json) will start the respective microservices.

By following these steps, you should be able to successfully set up and run your microservices project. Remember to adjust the commands if your project's structure or script names differ.

Technologies Used:

• GRPC: An RPC framework for high performance, low latency communication between services.

• Node.js: A JavaScript runtime environment used to develop server-side applications.

• Mongoose: An ODM (Object Data Modeling) library for MongoDB that simplifies data interaction.

• Express: A web framework for building APIs in Node.js.

Data Storage:

• MongoDB: A NoSQL document database used to store user, post, and subpost data.

Communication Flow:

1. REST API (main.js):
    ◦ This service acts as the entry point for the application, exposing a RESTful API at port 5001.
    ◦ It handles user requests (GET and POST) for user data and posts with subposts.
2. User Service (user-ms):
    ◦ Communicates with MongoDB via Mongoose to manage user data.
    ◦ Provides gRPC endpoints for finding and adding users.
3. Post Service (post-ms):
    ◦ Communicates with MongoDB via Mongoose to manage post data.
    ◦ Provides gRPC endpoints for finding and adding posts.
    ◦ Retrieves user data from the User Service using gRPC when fetching posts for a user.
4. Subpost Service (subpost-ms):
    ◦ Communicates with MongoDB via Mongoose to manage subpost data.
    ◦ Provides gRPC endpoints for finding and adding subposts.
    ◦ Retrieves post data from the Post Service using gRPC when fetching subposts for a post.

API Endpoints:

• GET /users/:userId
    ◦ Retrieves user data (ID, name, department) and associated posts with subposts for the given user ID.
• POST /users
    ◦ Creates a new user and associated posts with subposts.

Data Model:

• User:
    ◦ userId (string): Unique identifier for the user.
    ◦ name (string): User's name.
    ◦ department (string): User's department.
• Post:
    ◦ postId (string): Unique identifier for the post.
    ◦ title (string): Title of the post.
    ◦ userId (string): ID of the user who created the post.
    ◦ subposts (array of Subpost): An array of subposts associated with the post.
• Subpost:
    ◦ subpostId (string): Unique identifier for the subpost.
    ◦ content (string): Content of the subpost.
    ◦ postId (string): ID of the post the subpost belongs to.

Sample Request and Response:

POST Request ( http://localhost:5001/users ):

{
  "user": {
    "userId": "1234567",
    "name": "John Doe",
    "department": "IT"
  },
  "posts": [
    {
      "title": "First Post",
      "postId": "post123"
    },
    {
      "title": "Second Post",
      "postId": "post234"
    }
  ],
  "subposts": [
    {
      "content": "First subpost for post1",
      "postId": "post123",
      "subpostId": "subpost1"
    },
    {
      "content": "Second subpost for post1",
      "postId": "post123",
      "subpostId": "subpost2"
    },

GET Response ( http://localhost:5001/users/:userId ):

{
  "user": {
    "userId": "1234",
    "name": "John Doe",
    "department": "IT"
  },
  "posts": [
    {
      "title": "First Post",
      "postId": "post1",
      "subposts": [
        {
          "content": "First subpost for post1",
          "subpostId": "subpost1"
        },
        {
          "content": "Second subpost for post1",
          "subpostId": "subpost2"
        }
      ]
    },
    {
      "title": "Second Post",
      "postId": "post2",
      "subposts": [
        {
          "content": "First subpost for post2",
          "subpostId": "subpost3"
        }
      ]
    }
  ]
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published