Giao thức RabbitMQ
Giao thức AMQP 0-9-1 cho RabbitMQ.
Thiết lập máy chủ
typescript
import { BunFactory } from '@galaxy-stack/orbit-core';
import { Transport } from '@galaxy-stack/orbit-microservices';
const app = await BunFactory.createMicroservice(AppModule, {
transport: Transport.RMQ,
options: {
urls: ['amqp://localhost:5672'],
queue: 'orders_queue',
queueOptions: {
durable: true,
},
},
});
await app.listen();Trình xử lý tin nhắn
typescript
@Controller()
export class OrdersHandler {
@MessagePattern('order.create')
async createOrder(@Payload() data: CreateOrderDto, @Ctx() context: RmqContext) {
const channel = context.getChannelRef();
const originalMsg = context.getMessage();
try {
const order = await this.ordersService.create(data);
channel.ack(originalMsg);
return order;
} catch (error) {
channel.nack(originalMsg, false, true);
throw error;
}
}
}Cấu hình Client
typescript
@Module({
imports: [
ClientsModule.register([
{
name: 'ORDERS_SERVICE',
transport: Transport.RMQ,
options: {
urls: ['amqp://localhost:5672'],
queue: 'orders_queue',
queueOptions: {
durable: true,
},
prefetchCount: 10,
},
},
]),
],
})
export class AppModule {}Cấu hình Exchange
typescript
{
transport: Transport.RMQ,
options: {
urls: ['amqp://localhost:5672'],
queue: 'orders_queue',
exchange: 'orders_exchange',
exchangeType: 'topic',
routingKey: 'orders.*',
},
}Xác nhận thủ công
typescript
@MessagePattern('tasks.process')
async processTask(@Payload() data: Task, @Ctx() ctx: RmqContext) {
const channel = ctx.getChannelRef();
const msg = ctx.getMessage();
await this.taskService.process(data);
channel.ack(msg);
}