admin管理员组文章数量:1332865
Good Day!
I'm trying to implement 2 microservices that municate with each other through a message broker
. But one of them should accept Http requests via REST-Api
. Unfortunately, I don't understand how to make the microservice listen to both the message queue and ining HTTP requests. Perhaps I don’t understand something in the paradigm of munication through a message broker, but how then to receive requests from the client and forward them to the microservice architecture?
Main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {Transport, MicroserviceOptions} from '@nestjs/microservices'
async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.RMQ,
options: {
urls: ['amqp://rabbitmq:5672'],
queue: 'hello_world',
queueOptions: {
durable: false
},
},
});
await app.listen();
}
bootstrap();
As you can see, now the application is not listening on port 3000 as in the standard approach. What should be done?
Good Day!
I'm trying to implement 2 microservices that municate with each other through a message broker
. But one of them should accept Http requests via REST-Api
. Unfortunately, I don't understand how to make the microservice listen to both the message queue and ining HTTP requests. Perhaps I don’t understand something in the paradigm of munication through a message broker, but how then to receive requests from the client and forward them to the microservice architecture?
Main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {Transport, MicroserviceOptions} from '@nestjs/microservices'
async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.RMQ,
options: {
urls: ['amqp://rabbitmq:5672'],
queue: 'hello_world',
queueOptions: {
durable: false
},
},
});
await app.listen();
}
bootstrap();
As you can see, now the application is not listening on port 3000 as in the standard approach. What should be done?
Share asked Jan 21, 2022 at 4:32 MiyRonMiyRon 4367 silver badges20 bronze badges1 Answer
Reset to default 9The answer turned out to be quite simple. NEST js has hybrid apps. You can get to know them here https://docs.nestjs./faq/hybrid-application#hybrid-application. Thanks everyone
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {Transport, MicroserviceOptions} from '@nestjs/microservices'
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const microservice = app.connectMicroservice({
transport: Transport.RMQ,
options: {
urls: ['amqp://rabbitmq:5672'],
queue: 'hello_world',
queueOptions: {
durable: false
},
},
});
await app.startAllMicroservices();
await app.listen(3000);
}
bootstrap();
本文标签: javascripthow to use RabbitMQ and RESTAPI in Nest AppStack Overflow
版权声明:本文标题:javascript - how to use RabbitMQ and REST-API in Nest App? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292495a2448067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论