A 类 鸟巢 谷歌PubSub微服务.
NestJS Google Cloud PubSub 微服务
习惯 NestJS 微服务 运输战略 Google Cloud PubSub (Google Cloud PubSub) (英语)。 。 。 .
安装
npm install --save @algoan/pubsub @algoan/nestjs-google-pubsub-microservice
使用量
要启动一个新的 Google Cloud PubSub 服务器:
服务器设置数字 :
// main.ts
import { GooglePubSubOptions } from '@algoan/pubsub';
import { INestMicroservice } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { GCPubSubServer } from '@algoan/nestjs-google-pubsub-microservice';
import { AppModule } from './app.module';
async function bootstrap() {
const options: GooglePubSubOptions = {
projectId: 'test',
subscriptionsPrefix: 'test-app',
}
const app: INestMicroservice = await NestFactory.createMicroservice(AppModule, {
strategy: new GCPubSubServer(options)
})
await app.listen();
console.log('Server running!')
}
bootstrap()
主计长数字 :
import { EmittedMessage } from '@algoan/pubsub';
import { Controller } from '@nestjs/common';
import { EventPattern, Payload } from '@nestjs/microservices';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
/**
* Handle the test event
* @param data Payload sent
*/
@EventPattern('test_event')
public async handleTestEvent(@Payload() data: EmittedMessage<{ hello: string }>): Promise<void> {
/**
* Handle data emitted by Google PubSub
*/
this.appService.handleTestEvent(data);
}
}
事件临时额外项目
您可以添加 ListenOptions 添加 extras 页:1 EventPattern 装饰. 例如,如果想要不同的主题名称:
/**
* Handle the test event
* @param data Payload sent
*/
@EventPattern('test_event', { topicName: 'different_topic' })
public async handleTestEvent(@Payload() data: EmittedMessage<{ hello: string }>): Promise<void> {
/**
* Handle data emitted by Google PubSub
*/
this.appService.handleTestEvent(data);
}
当应用程序启动并查找事件模式时,它将获得或创建 different_topic 来自Google Cloud的话题.
API 密码
此模块使用 @algoan/pubsub (英语) 默认自动确认已发送信件的库.
GCPubSubServer(options)
创建 Google PubSub 的新服务器实例 。 它检索所有信件处理器模式并创建 订阅。 。 。 .
options: Algoan PubSub 选项. 更多信息 这里。 。 。 .options.listenOptions: 将适用于所有订阅的全球选项.options.topicsNames: 只订阅此白名单中包含的主题 .
其他 NestJS Google Cloud PubSub 服务器
使用 NestJS 微服务实施 Google Cloud PubSub 的其他模块: