简体   繁体   中英

NestJS - Combine HTTP with MQTT and TCP in microservices

According to NestJS documentation for a hybrid application:

To connect multiple microservice instances, simply pass additional microservice configuration objects as arguments in a comma-separated list.

Is this the right way of implementing more than one microservice instance?

app.connectMicroservice({
        transport:  Transport.TCP,
        options: {
             port: 3000
         },
         {
           transport: Transport.MQTT,
           options: {
               url: process.env.MQTT_CLIENT + ':1883'
           }
         }
    })

Yes it is. And you can connect multiple microservices, even if they are using the same Transport strategy (eg 3 Redis microservices, each using its own instance of Redis).

Here's an example from one of my working/production project:

const app = await NestFactory.create(rootModuleClass);

app.connectMicroservice({
  strategy: new CloudServerPubSub({
    clientConfig: {
      keyFile: configService.get('EVENT_BUS_CREDENTIALS_FILEPATH'),
    },
  }),
});

app.startAllMicroservices();
await app.listen(3000);

This allows the app to both listen to Google Cloud Pub/Sub messages and respond to HTTP requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM