NestJS-在微服务中将HTTP与RabbitMQ结合 [英] NestJS - Combine HTTP with RabbitMQ in microservices

查看:1401
本文介绍了NestJS-在微服务中将HTTP与RabbitMQ结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些微服务,这些服务通过API网关公开.网关负责处理身份验证和路由到系统中.网关后面的服务主要是简单的CRUD服务.每个服务都公开其自己的API,并且它们通过HTTP同步通信.所有这些服务,包括API-Gateway,都是默认" NestJS应用程序.

I have a few microservices, which are exposed through an API-Gateway. The gateway takes care of handling authentication and routing into the system. The services behind the gateway are mostly simple CRUD-Services. Each service exposes its own API and they communicate synchronously via HTTP. All of these services, including the API-Gateway, are "default" NestJS applications.

让我们坚持猫"的例子.每当Cat-Service更新或创建新的Cat时,我都希望使用CatCreatedEventCatUpdatedEvent.该事件应该被推送到某些消息代理中,例如RabbitMQ,而另一个服务应侦听此事件并异步处理该事件.

Let's stick with the Cats example. Whenever the Cat-Service updates or creates a new Cat, I want an CatCreatedEvent or CatUpdatedEvent to be emmited. The event should be pushed into some message broker like RabbitMQ and another service should listen to this event and process the event asynchronously.

就如何以正确的方式注入" RabbitMQ而言,我不确定如何实现这一目标,我想知道这种方法是否合乎常理.我已经看到了NestJS的CQRS模块,但是我认为CQRS对于该域来说有点太多了.尤其是因为在此域中没有好处,无法拆分读取和写入模型.也许我完全走错了道路,所以希望您能给我一些建议.

I am not sure how to achive this, in terms of how to "inject" RabbitMQ the right way and I am wondering if this approach makes sense in generel. I have seen the CQRS Module for NestJS, but i think CQRS is a bit too much for this domain. Especially because there is no benefit in this domain to split read- and write-models. Maybe I am totally on the wrong track, so I hope you can give me some advises.

推荐答案

RabbitMQ 受支持nestjs作为微服务.如果您希望您的应用程序同时支持http请求和消息代理,则可以创建混合应用程序.

RabbitMQ is supported in nestjs as a microservice. If you want your application to support both http requests and a message broker, you can create a hybrid application.

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

// Then combine it with a RabbitMQ microservice
const microservice = app.connectMicroservice({
  transport: Transport.RMQ,
  options: {
    urls: [`amqp://localhost:5672`],
    queue: 'my_queue',
    queueOptions: { durable: false },
  },
});

await app.startAllMicroservicesAsync();
await app.listen(3001);

这篇关于NestJS-在微服务中将HTTP与RabbitMQ结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆