最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Expose normal http endpoint in NestJS Microservices - Stack Overflow

programmeradmin1浏览0评论

I have this microservice written with NestJs:

async function bootstrap() {
  const port = parseInt(process.env.PORT || '5000', 10);

  const app = await NestFactory.createMicroservice(ApplicationModule, {
    transport: Transport.TCP,
    options: { host: '0.0.0.0', port }
  });
  app.listen(() => console.log(`Microservice is listening on port ${port}`));
}
bootstrap();

But now I need to implement an endpoint /metrics for Prometheus. So the question is how do I do this with NestJs microservice

From this issue I get the impression that it is not possible. Is this true and if so, is there a workaround I can use?

I tried to apply middleware as follows

Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService, DBService],
})
export class ApplicationModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(MetricsMiddleware)
      .forRoutes('/metrics') // Also tried '*'
  }
}

But when I do curl http://localhost:5000/metrics nothing happens, the middleware is not called

@Injectable()
export class MetricsMiddleware implements NestMiddleware {
    constructor() {}

    use(req, res, next) {
        console.log('yes');
        next();
    }
}

Update: This issue will also not help :(

I have this microservice written with NestJs:

async function bootstrap() {
  const port = parseInt(process.env.PORT || '5000', 10);

  const app = await NestFactory.createMicroservice(ApplicationModule, {
    transport: Transport.TCP,
    options: { host: '0.0.0.0', port }
  });
  app.listen(() => console.log(`Microservice is listening on port ${port}`));
}
bootstrap();

But now I need to implement an endpoint /metrics for Prometheus. So the question is how do I do this with NestJs microservice

From this issue I get the impression that it is not possible. Is this true and if so, is there a workaround I can use?

I tried to apply middleware as follows

Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService, DBService],
})
export class ApplicationModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(MetricsMiddleware)
      .forRoutes('/metrics') // Also tried '*'
  }
}

But when I do curl http://localhost:5000/metrics nothing happens, the middleware is not called

@Injectable()
export class MetricsMiddleware implements NestMiddleware {
    constructor() {}

    use(req, res, next) {
        console.log('yes');
        next();
    }
}

Update: This issue will also not help :(

Share Improve this question edited Sep 12, 2019 at 15:45 Kim Kern 60.4k20 gold badges216 silver badges212 bronze badges asked Sep 12, 2019 at 14:38 Jeanluca ScaljeriJeanluca Scaljeri 29.1k66 gold badges231 silver badges379 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 27

If you want your application to support both http requests and a microservice, you can create a hybrid application.

// Create your regular nest application.
const app = await NestFactory.create(ApplicationModule);

// Then combine it with your microservice
const microservice = app.connectMicroservice({
  transport: Transport.TCP,
  options: { host: '0.0.0.0', port: 5000 }
});

await app.startAllMicroservices();
await app.listen(3001);
发布评论

评论列表(0)

  1. 暂无评论