Egg: 写了一个rabbitmq的插件,有问题

Created on 8 May 2019  ·  2Comments  ·  Source: eggjs/egg

What happens?

const amqp = require('amqplib')
module.exports = app => {
    app.addSingleton('rabbitmq', createRabbitMQConnection);
}

async function createRabbitMQConnection(config, app) {
    assert(config.hostname && config.port && config.username && config.password)

    app.coreLogger.info('[egg-rabbitmqHelper] connecting %s"//%s:***@%s:%s',
        config.protocol, config.username, config.hostname, config.port)

    const conn = await amqp.connect({
       // 配置
    })
    return conn
}

使用发现this.app.rabbitmq.createChannel is not a function 初始化方法不是支持Async

Context

  • Node Version: 10.15
  • Egg Version: 2.15
  • Plugin Name: egg-rabbitmq-helper
  • Plugin Version: 1.0.0
  • Platform: Linux

Most helpful comment

把config.default.js改成这样试一下:

module.exports=(appInfo)=>{
   config.keys='123456';
   config.rabbitmq= {
    client: {
      protocol: 'amqp',
      hostname: 'my_rabbitmq_server',
      port: 5672,
      username: 'test',
      password: 'test',
      frameMax: 0,
      heartbeat: 2, // 心跳
      vhost: '/',
      })
    };
    return config;
}


config.rabbitmq

rabbitmq 这个是app.addSingleton() 第一个参数的那个key的值。

All 2 comments

把config.default.js改成这样试一下:

module.exports=(appInfo)=>{
   config.keys='123456';
   config.rabbitmq= {
    client: {
      protocol: 'amqp',
      hostname: 'my_rabbitmq_server',
      port: 5672,
      username: 'test',
      password: 'test',
      frameMax: 0,
      heartbeat: 2, // 心跳
      vhost: '/',
      })
    };
    return config;
}


config.rabbitmq

rabbitmq 这个是app.addSingleton() 第一个参数的那个key的值。

@salamander-mh 有完整的demo吗?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

occultskyrong picture occultskyrong  ·  35Comments

popomore picture popomore  ·  38Comments

popomore picture popomore  ·  47Comments

fengmk2 picture fengmk2  ·  44Comments

andylei18 picture andylei18  ·  35Comments