Created
February 24, 2022 05:40
-
-
Save thiagotigaz/d742743fdd2ea4540301792de8021a8f to your computer and use it in GitHub Desktop.
NodeJS Microservices with NestJS, Kafka, and Kubernetes - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl --location --request POST 'http://localhost:3000/services' \ | |
--header 'Content-Type: application/json' \ | |
--data-raw '{ | |
"name": "Web Development", | |
"description": "description for Web Development" | |
}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# make sure you are on the root of the repository | |
nest new -g microservices | |
cd microservices | |
npm i @nestjs/microservices kafkajs @nestjs/mapped-types class-transformer class-validator @nestjs/event-emitter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IsNotEmpty, IsString, MaxLength } from 'class-validator'; | |
import { EventDto } from './event.dto'; | |
import { PartialType } from '@nestjs/mapped-types'; | |
export class CreateServiceDto extends PartialType(EventDto) { | |
@MaxLength(64) | |
@IsString() | |
@IsNotEmpty() | |
name: string; | |
@MaxLength(255) | |
@IsString() | |
@IsNotEmpty() | |
description: string; | |
tags: string[]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PartialType } from '@nestjs/mapped-types'; | |
import { IsNotEmpty } from 'class-validator'; | |
import { EventDto } from './event.dto'; | |
export class DeleteServiceDto extends PartialType(EventDto) { | |
@IsNotEmpty() | |
id: number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IsNotEmpty, IsString } from 'class-validator'; | |
export class EventDto { | |
@IsString() | |
@IsNotEmpty() | |
eventType: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Type } from 'class-transformer'; | |
import { IsNotEmpty, ValidateNested } from 'class-validator'; | |
import { CreateServiceDto } from './create-service.dto'; | |
import { UpdateServiceDto } from './update-service.dto'; | |
import { DeleteServiceDto } from './delete-service.dto'; | |
import { EventDto } from './event.dto'; | |
export class KafkaServiceDto { | |
@Type(() => EventDto, { | |
discriminator: { | |
property: 'eventType', | |
subTypes: [ | |
{ value: CreateServiceDto, name: 'ServiceCreated' }, | |
{ value: UpdateServiceDto, name: 'ServiceUpdated' }, | |
{ value: DeleteServiceDto, name: 'ServiceDeleted' }, | |
], | |
}, | |
keepDiscriminatorProperty: true, | |
}) | |
@ValidateNested() | |
@IsNotEmpty() | |
value: CreateServiceDto | UpdateServiceDto | DeleteServiceDto; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nest g co services |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Logger, ValidationPipe } from '@nestjs/common'; | |
import { EventPattern, Payload } from '@nestjs/microservices'; | |
import { KafkaServiceDto } from './dto/kafka-service.dto'; | |
import { CreateServiceDto } from './dto/create-service.dto'; | |
import { UpdateServiceDto } from './dto/update-service.dto'; | |
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; | |
import { DeleteServiceDto } from './dto/delete-service.dto'; | |
@Controller() | |
export class ServicesController { | |
constructor(private eventEmitter: EventEmitter2) {} | |
@EventPattern('services') | |
serviceEvent(@Payload(new ValidationPipe()) { value }: KafkaServiceDto) { | |
Logger.debug(value, 'ServicesController - serviceEvent'); | |
this.eventEmitter.emit(value.eventType, value); | |
} | |
@OnEvent('ServiceCreated') | |
handleServiceCreatedEvent(createServiceDto: CreateServiceDto) { | |
Logger.debug( | |
createServiceDto, | |
'ServicesController - handleServiceCreatedEvent', | |
); | |
} | |
@OnEvent('ServiceUpdated') | |
handleServiceUpdatedEvent(updateServiceDto: UpdateServiceDto) { | |
Logger.debug( | |
updateServiceDto, | |
'ServicesController - handleServiceUpdatedEvent', | |
); | |
} | |
@OnEvent('ServiceDeleted') | |
handleServiceDeletedEvent(deleteServiceDto: DeleteServiceDto) { | |
Logger.debug( | |
deleteServiceDto, | |
'ServicesController - handleServiceDeletedEvent', | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PartialType } from '@nestjs/mapped-types'; | |
import { CreateServiceDto } from './create-service.dto'; | |
import { IsNotEmpty } from 'class-validator'; | |
export class UpdateServiceDto extends PartialType(CreateServiceDto) { | |
@IsNotEmpty() | |
id: number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment