如何使用NestJS Redis微服务? [英] how to use nestjs redis microservice?

查看:769
本文介绍了如何使用NestJS Redis微服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习nestjs微服务,

我可以使用什么命令?

const pattern = { cmd: 'get' };
this.client.send<any>(pattern, data)

如何从redis接收数据?

constructor(private readonly appService: AppService) {}
      @Client({
        transport: Transport.REDIS,
        options: {
          url: 'redis://127.0.0.1:6379',
        },
      })
      client: ClientProxy;

      @Get()
      getHello(): any {
        const pattern = { cmd: 'get foo' };  //Please write sample code here in document
        const data = '';
        return this.client.send<any>(pattern, data);
      }

解决方案

您需要分开两个方面.它们可以是一个nest.js应用程序的一部分(例如混合应用程序),也可以属于多个不同的nest.js应用程序: /p>

客户

客户端在主题/模式上广播消息,并从接收到的消息接收者的响应.

首先,您必须连接客户端.您可以在onModuleInit中执行此操作.在此示例中,ProductService在创建新产品实体时广播一条消息.

@Injectable()
export class ProductService implements OnModuleInit {

  @Client({
    transport: Transport.REDIS,
    options: {
      url: 'redis://localhost:6379',
    },
  })
  private client: ClientRedis;

  async onModuleInit() {
    // Connect your client to the redis server on startup.
    await this.client.connect();
  }

  async createProduct() {
    const newProduct = await this.productRepository.createNewProduct();
    // Send data to all listening to product_created
    const response = await this.client.send({ type: 'product_created' }, newProduct).toPromise();
    return response;
  }
}

请记住,this.client.send返回一个Observable.这意味着,直到您subscribe都不会发生任何事情(您可以通过调用toPromise()隐式执行此操作).

模式处理程序

模式处理程序使用消息并将响应发送回客户端.

@Controller()
export class NewsletterController {

  @MessagePattern({ type: 'product_created' })
  informAboutNewProduct(newProduct: ProductEntity): string {
    await this.sendNewsletter(this.recipients, newProduct);
    return `Sent newsletter to ${this.recipients.length} customers`;
  }

当然,参数处理程序也可以是客户端,因此可以接收和广播消息.

I am learning nestjs microservice,

What command can I use?

const pattern = { cmd: 'get' };
this.client.send<any>(pattern, data)

And how can I receive data from redis?

constructor(private readonly appService: AppService) {}
      @Client({
        transport: Transport.REDIS,
        options: {
          url: 'redis://127.0.0.1:6379',
        },
      })
      client: ClientProxy;

      @Get()
      getHello(): any {
        const pattern = { cmd: 'get foo' };  //Please write sample code here in document
        const data = '';
        return this.client.send<any>(pattern, data);
      }

解决方案

There are two sides you need to separate. They can be part of one nest.js application (e.g. hybrid application) or be in several different nest.js applications:

Client

The client broadcasts messages on a topic/pattern and receives a response from the receiver(s) of the broadcasted message.

First, you have to connect your client. You can do that in onModuleInit. In this example, ProductService broadcasts a message when a new product entity is created.

@Injectable()
export class ProductService implements OnModuleInit {

  @Client({
    transport: Transport.REDIS,
    options: {
      url: 'redis://localhost:6379',
    },
  })
  private client: ClientRedis;

  async onModuleInit() {
    // Connect your client to the redis server on startup.
    await this.client.connect();
  }

  async createProduct() {
    const newProduct = await this.productRepository.createNewProduct();
    // Send data to all listening to product_created
    const response = await this.client.send({ type: 'product_created' }, newProduct).toPromise();
    return response;
  }
}

Keep in mind, that this.client.send returns an Observable. This means, nothing will happen until you subscribe to it (which you can implicitly do by calling toPromise()).

Pattern Handler

The pattern handler consumes messages and sends a response back to the client.

@Controller()
export class NewsletterController {

  @MessagePattern({ type: 'product_created' })
  informAboutNewProduct(newProduct: ProductEntity): string {
    await this.sendNewsletter(this.recipients, newProduct);
    return `Sent newsletter to ${this.recipients.length} customers`;
  }

Of course, a param handler could also be a client and therewith both receive and broadcast messages.

这篇关于如何使用NestJS Redis微服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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