1. 프로젝트 설정
ConfigModule을 활용해 전역 환경 변수를 설정합니다.
HTTP_PORT, DB_URL, GRPC_URL 등을 Joi 스키마로 검증.
Mongoose 연결: Notification 데이터 저장소로 MongoDB 사용.
ClientsModule.registerAsync를 통해 Order 서비스와 gRPC 통신할 수 있도록 클라이언트 설정.
order.proto를 로드하고 GRPC_URL을 이용해 Order 서비스와 연결.
ClientsModule.registerAsync({
clients: [
{
name: ORDER_SERVICE,
useFactory: (configService: ConfigService) => ({
transport: Transport.GRPC,
options: {
package: OrderMicroservice.protobufPackage,
protoPath: join(process.cwd(), 'proto/order.proto'),
url: configService.getOrThrow('GRPC_URL'),
},
}),
inject: [ConfigService],
},
],
isGlobal: true,
}),
2. Notification Microservice 부트스트랩
main.ts에서 gRPC 기반 Notification 마이크로서비스를 등록.
Notification 서비스의 프로토콜 버퍼(notification.proto)를 로드.
app.startAllMicroservices()로 실행.
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
package: NotificationMicroservice.protobufPackage,
protoPath: join(process.cwd(), 'proto/notification.proto'),
url: configService.getOrThrow('GRPC_URL'),
},
});
3. NotificationController
Notification 서비스의 핵심 컨트롤러.
gRPC 인터페이스(NotificationServiceController)를 구현.
sendPaymentNotification 메서드에서 결제 완료 시 사용자에게 알림 전송.
요청(SendPaymentNotificationDto)을 받고, 응답은 JSON 변환 후 반환.
async sendPaymentNotification(@Payload() payload: SendPaymentNotificationDto) {
console.log('✔️ NotificationController sendPaymentNotification', payload);
const resp = (await this.notificationService.sendPaymentNotification(payload)).toJSON();
return { ...resp, status: resp.status.toString() };
}
4. Proto 정의
Notification 마이크로서비스는 notification.proto 파일을 통해 gRPC 통신을 정의합니다.
syntax ="proto3";
package notification;
service NotificationService{
rpc SendPaymentNotification(SendPaymentNotificationRequest) returns(SendPaymentNotificationResponse);
}
message SendPaymentNotificationRequest{
string to =1;
string orderId=2;
}
message SendPaymentNotificationResponse{
string from =1;
string to=2;
string subject =3;
string content =4;
string status =5;
}
Request: 수신자(to), 주문 ID(orderId)
Response: 발신자, 수신자, 제목, 본문, 상태 등 메일 전송 결과 반환
5. 핵심 학습 포인트
마이크로서비스 간 통신: gRPC 기반 서비스 정의 및 호출.
NestJS gRPC 설정: ClientsModule, connectMicroservice, .proto 파일 활용.
실제 Notification 로직 분리: Controller → Service 레이어 구조.
확장성 고려: 추후 결제 알림 외 다른 알림(회원가입, 배송 등)도 쉽게 확장 가능.













댓글 ( 0)
댓글 남기기