From adc063eb7a44938083a57ccc0d27de4a7f5f3114 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 10 Dec 2025 10:08:55 -0800 Subject: [PATCH] fix(authorization): add Kafka microservice for @EventPattern to work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit authorization-service uses @EventPattern('planting-events') to consume planting events but was missing connectMicroservice() configuration. Without this, the service could not receive planting events, causing: - Province team benefits (20 USDT) not distributed - City team benefits (40 USDT) not distributed - Community benefits (80 USDT) not distributed - Monthly assessment data not updated 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../authorization-service/src/main.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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)