【问题标题】:Call my NestJs microservice with nodeJS app使用 nodeJS 应用程序调用我的 NestJs 微服务
【发布时间】:2019-09-20 09:44:22
【问题描述】:

我想我可以说我是一个微服务的菜鸟。所以,这就是我想玩它的原因。我使用 NestJs,因为它看起来很简单

首先我使用nest new myservice 创建了一个新应用程序 然后我从微服务文档中将示例 main.ts 和 controller.ts 复制到项目中:

main.ts:

import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
    const app = await NestFactory.createMicroservice(AppModule, {
        transport: Transport.TCP,
        options: { host: 'localhost', port: 3005 },
    });
    app.listen(() => console.log('Microservice is listening'));
}
bootstrap();

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
    imports: [],
    controllers: [AppController],
    providers: [AppService],
})
export class AppModule {

controoler.ts

import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
    @MessagePattern({ cmd: 'sum' })
    accumulate(data: number[]): number {
        return (data || []).reduce((a, b) => a + b);
    }
}

现在当我启动它时,一切看起来都很好:

✗ yarn start
yarn run v1.13.0
$ ts-node -r tsconfig-paths/register src/main.ts
[Nest] 45783   - 05/01/2019, 11:08 PM   [NestFactory] Starting Nest application...
[Nest] 45783   - 05/01/2019, 11:08 PM   [InstanceLoader] AppModule dependencies initialized +17ms
[Nest] 45783   - 05/01/2019, 11:08 PM   [NestMicroservice] Nest 
microservice successfully started 
Microservice is listening

所以,如果这里有什么问题,请告诉我!但是知道我想编写一个可以调用/与这个微服务通信的小型测试 nodejs 应用程序。任何建议从哪里开始。例如,我可以使用 axios 还是应该使用其他东西。任何帮助将不胜感激!

【问题讨论】:

  • 我不明白,什么不起作用?应在Code Review 上征求批评意见,Stack Overflow 适用于不起作用的代码。如果您只是询问应该编写什么代码的建议,那也是题外话,因为它太宽泛了。

标签: node.js microservices nestjs


【解决方案1】:

您需要执行以下操作。

import { ClientTCP } from '@nestjs/microservices';

(async () => {
    const client = new ClientTCP({
        host: 'localhost',
        port: 3005,
    });

    await client.connect();

    const pattern = { cmd: 'sum' };
    const data = [2, 3, 4, 5];

    const result = await client.send(pattern, data).toPromise();
    console.log(result);
})();

【讨论】:

  • 如何从 Angular 应用程序或任何前端框架中调用它?
  • 是否总是需要一个nestjs客户端应用程序来联系nestjs微服务应用程序?
  • 是的。从 Angular 应用程序通过 TCP 直接调用后端似乎是不可能的。您必须始终使用 WebSocket 或 HTTP 作为“桥梁”。
【解决方案2】:

我尝试在上一篇文章(Aleksandr Yatsenko)之后将 express 应用程序与 nodejs 微服务通信,并且只有在你的 express 应用程序上安装时才能正常工作:

  • @nestjs/微服务
  • @nestjs/commons
  • @nestjs/core
  • rxjs

代码如下:

const express = require('express')
const app = express()
const port = 3002
const { ClientTCP } = require('@nestjs/microservices');
const { lastValueFrom } = require('rxjs');

(async () => {
    const client = new ClientTCP({
        host: 'localhost',
        port: 3001,
    });

    await client.connect();

    app.get('/', async (req, res) => {
        const pattern = { cmd: 'sum' };
        const data = JSON.parse(req.query.data)

        const result = await lastValueFrom(client.send(pattern, data))

        res.json({ result })
    })

    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })

})();

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2021-02-24
    • 2017-08-14
    • 1970-01-01
    • 2022-12-17
    • 2019-06-14
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多