diff --git a/backend/services/authorization-service/src/main.ts b/backend/services/authorization-service/src/main.ts index d66da528..4050a98d 100644 --- a/backend/services/authorization-service/src/main.ts +++ b/backend/services/authorization-service/src/main.ts @@ -1,6 +1,7 @@ import { NestFactory } from '@nestjs/core' import { ValidationPipe, Logger } from '@nestjs/common' import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger' +import { MicroserviceOptions, Transport } from '@nestjs/microservices' import { AppModule } from './app.module' import { GlobalExceptionFilter } from '@/shared/filters' import { TransformInterceptor } from '@/shared/interceptors' @@ -47,6 +48,26 @@ async function bootstrap() { const document = SwaggerModule.createDocument(app, config) SwaggerModule.setup('api/docs', app, document) + // Kafka 微服务 - 用于 @EventPattern 消费认种事件 + const kafkaBrokers = process.env.KAFKA_BROKERS?.split(',') || ['localhost:9092'] + const kafkaGroupId = process.env.KAFKA_GROUP_ID || 'authorization-service-group' + + app.connectMicroservice({ + transport: Transport.KAFKA, + options: { + client: { + clientId: 'authorization-service', + brokers: kafkaBrokers, + }, + consumer: { + groupId: kafkaGroupId, + }, + }, + }) + + await app.startAllMicroservices() + logger.log('Kafka microservice started for planting event consumption') + const port = parseInt(process.env.APP_PORT || '3009', 10) await app.listen(port)