npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-rabbitmq-plus

v1.0.19

Published

rabbitmq plgin for egg.js

Downloads

133

Readme

egg-rabbitmq-plus

egg rabbitmq插件,超级好用!!!!!

安装

$ npm i egg-rabbitmq-plus --save

多连接实例配置

// {app_root}/config/config.default.js
config.rabbitmq = {
  enable: true,
  clients: {
    messageServe: {
      url: {
        protocol: 'amqp',
        hostname: '${rabbitmq.host}',  // 根据情况配置
        port: '${rabbitmq.port}',
        username: '${rabbitmq.username}',
        password: '${rabbitmq.password}',
        locale: 'en_US',
        frameMax: 0,
        heartbeat: 0,
        vhost: '${rabbitmq.virtual-host}',
      },
      reconnectTime: 5000, // 重连时间间隔
      options: {},
      exchanges: {
        messageExchange: {
          name: 'message_exchange', // 消息推送交换机
          type: 'direct',
          options: {
            durable: true,
          },
        },
        dlxMessageExchange: {
          name: 'dlx_message_exchange', // 消息失败的死信交换机
          type: 'direct',
          options: {
            durable: true,
          },
        },
      },
      queues: {
        messageQueue: {
          // 推送消息队列
          exchange: 'message_exchange',
          name: 'message_queue',
          keys: {
            wechatTemplateMessage: 'wechat/message/template',
          },
          options: {
            exclusive: false,
            durable: true,
            maxPriority: 10,
            prefetch: 1,
            deadLetterRoutingKey: 'wechat/message/template',
            deadLetterExchange: 'dlx_message_exchange',
          },
          autoSubscribe: true, // 启动时自动开启订阅。
          subscribeOptions: {}, // 开启自动订阅时的消费者配置,不开启不用配置
        },
        dlxMessageQueue: {
          // 推送失败的队列,目前不做失败后的处理
          exchange: 'dlx_message_exchange',
          name: 'dlx_message_queue',
          keys: {
            wechatTemplateMessage: 'wechat/message/template', // 这里是deadLetterRoutingKey
          },
          options: {
            exclusive: false,
            durable: true,
            maxPriority: 10,
            prefetch: 1,
          },
          autoSubscribe: false, // 关闭自动订阅。
          subscribeOptions: {}, // 开启自动订阅时的消费者配置,不开启不用配置
        },
      },
    },
  }
};

使用

发布消息示例

class RBMQPublishService extends BaseService {
  /**
   * 获取交换机和队列配置信息
   * @param vhostName
   * @param exchangeName
   * @param queueName
   */
  private getQueueInfo(vhostName, exchangeName, queueName) {
    const { exchanges, queues } = this.app.config.rabbitmq.clients[vhostName];
    const exchange = exchanges[exchangeName].name;
    const queue = queues[queueName].name;
    const routingKeys = queues[queueName].keys;
    const { clients } = this.app.rabbitmq;
    const channel = clients.get(vhostName);
    return { channel, exchange, queue, routingKeys };
  }

  /**
   * 发布消息到队列
   * @param channel
   * @param exchange
   * @param key
   * @param data
   */
  private sendToQueue(channel, exchange, key, data, opts = {}): boolean {
    const message = {
      exchange,
      key,
      message: data,
      options: {
        priority: 10,
        persistent: true,
        mandatory: true,
        ...opts,
      },
    };
    return channel.publish(message);
  }

  /**
   * 获取消息的交换机和队列信息
   */
  public getMessageQueueInfo() {
    return this.getQueueInfo('messageServe', 'messageExchange', 'messageQueue');
  }

  /**
   * 获取失败消息的交换机和队列信息
   */
  public getFailedMessageQueueInfo() {
    return this.getQueueInfo('messageServe', 'dlxMessageExchange', 'dlxMessageQueue');
  }

  /**
   * 发布消息到message_queue
   * @param data
   */
  public sendToMessageQueue(data: object): boolean {
    const { channel, exchange, routingKeys } = this.getMessageQueueInfo();
    return this.sendToQueue(channel, exchange, routingKeys.wechatTemplateMessage, data);
  }
}

消费消息示例(注:在配置文件中开启autoSubscribe选项,会自动执行app/consumer目录下所有class的subscribe方法)

1、先在app目录下新建文件夹consumer

2、创建文件,继承BaseConsumer类

import { BaseContextClass } from 'egg';
import { BaseConsumer } from 'egg-rabbitmq-plus'

export default class SubscribePush extends BaseConsumer {
  static get config() {
    return {
      env: ['local'], // 可选,默认为所有环境
      disable: false, // 可选,默认为false
      queue: 'message_queue', // 监听的队列
      routingKey: 'wechat_message', // 绑定交换机时的routingKey,如果不指定key,会拿到队列所有的消息
    };
  }

  async subscribe(data) {
    const { channel } = this.service.message.rabbitmq.getMessageQueueInfo();
    try {
      this.ctx.logger.info(data.content.toString());
      channel.ack(data);
    } catch (err) {
      this.ctx.logger.error(err);
      channel.nack(data);
    }
  }
}