0Pricing
NestJS Enterprise Backend APIs · درس

الاتصال بين الخدمات

نفّذ أنماطًا للاتصال الفعال بين الخدمات المصغرة، بما في ذلك الآليات القائمة على الأحداث وآليات الطلب والاستجابة.

الاتصال بين الخدمات درس مجاني في NestJS Enterprise Backend APIs على CoddyKit. هذا هو الدرس 3 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في NestJS Enterprise Backend APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة NestJS Enterprise Backend APIs 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Microservices Need to Talk

Microservices are designed to be independent, but they often need to collaborate to complete complex tasks. This means they must communicate with each other!

In this lesson, we'll explore the main patterns for how your NestJS microservices can send messages and share data effectively.

Sync vs. Async Communication

Understanding the difference between synchronous and asynchronous communication is crucial for microservices:

  • Synchronous: The sender waits for an immediate reply. It's like a direct phone call where you expect an answer right away.
  • Asynchronous: The sender doesn't wait for a reply. It's like sending an email – you send it and continue with other tasks, expecting a reply later (or not at all).

Both patterns have distinct use cases in a distributed system.

Request-Response Pattern

The Request-Response pattern is a synchronous communication method. One service (the client) sends a request to another service (the server) and pauses its own execution, waiting for a direct response.

  • Ideal for operations needing an immediate result.
  • Similar to how a typical web client interacts with a REST API.
  • Often implemented using HTTP, RPC, or message brokers configured for synchronous replies.

NestJS: Receiving Requests

In NestJS, a microservice listens for incoming requests using the @MessagePattern() decorator. This pattern must match the one sent by the client.

The decorated handler function receives the payload and returns a response. Try running this Product Service microservice:

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { Module, Controller, MessagePattern } from '@nestjs/common';

@Controller()
class ProductController {
  @MessagePattern('get_product_details')
  getProductDetails(id: string): any {
    console.log(`Product Service: Request for ID: ${id}`);
    const products = {
      '1': { id: '1', name: 'Laptop', price: 1200 },
      '2': { id: '2', name: 'Mouse', price: 25 },
    };
    return products[id] || { id, name: 'Product Not Found', price: 0 };
  }
}

@Module({
  controllers: [ProductController],
})
class AppModule {}

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.TCP,
      options: { host: '127.0.0.1', port: 8877 },
    },
  );
  await app.listen();
  console.log('Product Microservice is listening on port 8877');
}

bootstrap();

NestJS: Sending Requests

To send a request to the 'Product Service' (from the previous scene), another microservice or an API Gateway uses a ClientProxy.

The client.send() method dispatches a message with a specific pattern and payload, returning an RxJS Observable that resolves with the response.

This snippet shows how a client service would initiate the request:

// product-client.service.ts in 'Order Service' microservice
import { Injectable, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { lastValueFrom } from 'rxjs'; // For awaiting observable

@Injectable()
export class ProductClientService {
  constructor(@Inject('PRODUCT_SERVICE') private client: ClientProxy) {}

  async fetchProduct(productId: string): Promise<any> {
    console.log(`Order Service: Fetching product ID: ${productId}`);
    // 'get_product_details' must match the pattern in Product Service
    const product = await lastValueFrom(
      this.client.send('get_product_details', productId),
    );
    console.log('Received product:', product);
    return product;
  }
}

// To make this work, 'PRODUCT_SERVICE' needs to be registered
// in an AppModule using ClientsModule.register():
// imports: [
//   ClientsModule.register([
//     {
//       name: 'PRODUCT_SERVICE',
//       transport: Transport.TCP,
//       options: { host: '127.0.0.1', port: 8877 },
//     },
//   ]),
// ],

When to Use Request-Response

The Request-Response pattern is best when:

  • You need an immediate answer from the target service.
  • The operation is critical and requires direct feedback (e.g., payment processing, user authentication).
  • You are fetching specific data from another service.
  • The interaction is a clear client-server relationship.

Be aware that this pattern introduces direct coupling between services.

Event-Based Communication

Event-based communication is an asynchronous pattern that uses a 'publish-subscribe' model. Services don't directly call each other; instead, they publish events to a message broker (like RabbitMQ) when something notable happens.

  • A service publishes an event (e.g., 'OrderCreated').
  • Other services subscribe to events they are interested in.
  • This pattern greatly decouples services, making them more independent.

NestJS: Publishing Events

To publish an event in NestJS, you use the client.emit() method of a ClientProxy. Unlike send(), emit() sends the event and immediately returns, not waiting for a response.

This 'fire-and-forget' approach is ideal for notifying other services about state changes. Run this Order Service that emits an event:

import { NestFactory } from '@nestjs/core';
import { Module, Controller, Post, Body, Inject } from '@nestjs/common';
import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices';

interface OrderCreatedEvent {
  orderId: string;
  userId: string;
  totalAmount: number;
}

@Controller('orders')
class OrderController {
  constructor(@Inject('NOTIFICATION_SERVICE') private client: ClientProxy) {}

  @Post()
  async createOrder(@Body() orderData: any) {
    const orderId = `ORD-${Date.now()}`;
    const event: OrderCreatedEvent = {
      orderId,
      userId: orderData.userId || 'user-123',
      totalAmount: orderData.amount || 100,
    };
    this.client.emit('order_created', event);
    console.log(`Order ${orderId} created, 'order_created' event emitted.`);
    return { message: 'Order created and event sent', orderId };
  }
}

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'NOTIFICATION_SERVICE',
        transport: Transport.TCP,
        options: { host: '127.0.0.1', port: 8878 },
      },
    ]),
  ],
  controllers: [OrderController],
})
class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
  console.log('Order Service (HTTP) is listening on port 3000');
}

bootstrap();

NestJS: Listening to Events

Other microservices can subscribe to events using the @EventPattern() decorator. When an event with a matching pattern is emitted, the decorated method is automatically triggered.

This allows multiple services to react to the same event independently. Run this Notification Service to receive the event:

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { Module, Controller, EventPattern } from '@nestjs/common';

interface OrderCreatedEvent {
  orderId: string;
  userId: string;
  totalAmount: number;
}

@Controller()
class NotificationController {
  @EventPattern('order_created')
  handleOrderCreated(data: OrderCreatedEvent) {
    console.log('Notification Service received OrderCreated event:', data);
    console.log(`Sending notification for Order ${data.orderId} to User ${data.userId}`);
  }
}

@Module({
  controllers: [NotificationController],
})
class AppModule {}

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.TCP,
      options: { host: '127.0.0.1', port: 8878 },
    },
  );
  await app.listen();
  console.log('Notification Microservice is listening on port 8878');
}

bootstrap();

When to Use Event-Driven

Event-based communication is highly beneficial for:

  • Decoupling: Services don't need to know about each other, reducing dependencies.
  • Broadcasting: Notifying multiple consumers about a single event (e.g., 'UserRegistered' event).
  • Long-running processes: Initiating tasks that don't require an immediate response.
  • Resilience: Services can process events when they are available, improving fault tolerance.

It adds complexity but offers great flexibility and scalability.

Communication Patterns Quiz

Consider a scenario where an 'Order Service' needs to inform a 'Shipping Service' that an order is ready to be shipped, but the 'Order Service' doesn't need to wait for the shipping confirmation immediately. Which communication pattern is best suited for this?

Recap: Talking Services

Congratulations! You've learned about the fundamental patterns for inter-service communication in NestJS microservices.

  • Request-Response (using client.send() and @MessagePattern()) is for immediate, direct interactions where a reply is expected.
  • Event-Based (using client.emit() and @EventPattern()) is for asynchronous, decoupled communication via events, ideal for notifications and broadcasting.

Choosing the right pattern depends on your specific needs for coupling, responsiveness, and scalability in your distributed system.

الأسئلة الشائعة

هل درس «الاتصال بين الخدمات» مجاني؟

نعم — نص درس «الاتصال بين الخدمات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة NestJS Enterprise Backend APIs، انتقل إلى CoddyKit PRO. تتضمن دورة NestJS Enterprise Backend APIs 3 دروس في المجموع.

ماذا ستتعلم في «الاتصال بين الخدمات»؟

نفّذ أنماطًا للاتصال الفعال بين الخدمات المصغرة، بما في ذلك الآليات القائمة على الأحداث وآليات الطلب والاستجابة. تتمرن على NestJS Enterprise Backend APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ NestJS Enterprise Backend APIs؟

لا تُشترط خبرة سابقة. NestJS Enterprise Backend APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 3.

كم من الوقت يستغرق درس «الاتصال بين الخدمات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس NestJS Enterprise Backend APIs هذا؟

نعم. كل درس في NestJS Enterprise Backend APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نظرة عامة على الخدمات المصغرة في NestJS
  2. دمج RabbitMQ
  3. الاتصال بين الخدمات
← العودة إلى NestJS Enterprise Backend APIs