javascript 将Express连接到Rabbit MQ时遇到问题

wixjitnu  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(186)

在访问本地主机3000时,我得到了在使用 Postman 之前无法获得/。
在postman中发送POST请求到本地主机的过程中,我的程序失败,并出现此错误
'节点:内部/进程/承诺:288触发未捕获异常(错误,真/* 来自承诺 */);^
错误:在TCP连接环绕器上连接ECONREFUSED::1:5672。连接后[作为完成时](节点:网络:1487:16){错误号:-61,代码:'ECONNREFUSED',系统调用:“连接”,地址:“::1”,端口:小行星5672

config.js
----------

module.exports = {
  rabbitMQ: {
    url: "amqp://localhost",
    exchangeName: "logExchange",
  },
};

Producer.js
-----------

const amqp = require("amqplib");
const config = require("./config");

//step 1 : Connect to the rabbitmq server
//step 2 : Create a new channel on that connection
//step 3 : Create the exchange
//step 4 : Publish the message to the exchange with a routing key

class Producer {
  channel;

  async createChannel() {
    const connection = await amqp.connect(config.rabbitMQ.url);
    this.channel = await connection.createChannel();
  }

  async publishMessage(routingKey, message) {
    if (!this.channel) {
      await this.createChannel();
    }

    const exchangeName = config.rabbitMQ.exchangeName;
    await this.channel.assertExchange(exchangeName, "direct");

    const logDetails = {
      logType: routingKey,
      message: message,
      dateTime: new Date(),
    };
    await this.channel.publish(
      exchangeName,
      routingKey,
      Buffer.from(JSON.stringify(logDetails))
    );

    console.log(
      `The new ${routingKey} log is sent to exchange ${exchangeName}`
    );
  }
}

module.exports = Producer;



Server.js
----------

`const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const Producer = require("./producer");
const producer = new Producer();

app.use(bodyParser.json("application/json"));

app.post("/sendLog", async (req, res, next) => {
  await producer.publishMessage(req.body.logType, req.body.message);
  res.send();
});

app.listen(3000, () => {
  console.log("Server started...");
});`

我是一个初级实习生广告是沿着一个youtube视频由https://www.youtube.com/watch?v=igaVS0S1hA4由Computerix,以帮助我理解的概念
我尝试重新启动rabbitMQ和express服务器并在不同的端口上运行它,但没有解决问题

nx7onnlm

nx7onnlm1#

这是全局图

Postman -〉快递服务器-〉RabbitMQ-〉node.js

1 Postman 通过POST呼叫发送消息到Express,带有Message Typespayload

2 Express服务器通过publish将消息转发到Rabbit MQ

3 RabbitMQ-将消息推送到自己的message queue

4每个consumer都从RabbitMQ接收到自己的消息。它之前已被装箱。

有许多复杂的选项(顺序,优先级),但在这里谈论的基本和简单的模型。
步骤
我们需要遵循以下三个步骤。

1:安装

1.1 docker(运行RabbitMQ的简单容器方法)
1.2 node
1.3演示源

git clone https://github.com/charbelh3/RabbitMQ-Logger-Example.git

文件结构

1.4安装节点库
位于RabbitMQ-Logger-Example\infoMS目录

npm install amqplib

位于RabbitMQ-Logger-Example\loggerMS目录

npm install amqplib express body-parser

位于RabbitMQ-Logger-Example\warningAndErrorMS目录

npm install amqplib

1.5安装Postman
1.6安装Git Bash-我将使用git bash终端。它类似于Linux中的bash终端-如果你使用Linux,跳过这个。

2:绑定

2.1运行RabbitMQ
将此代码另存为docker-compose.yml

version: "3.8"
services:
  rabbitmq:
    image: rabbitmq:management-alpine
    container_name: 'rabbitmq'
    ports:
        - 5672:5672
        - 15672:15672
    volumes:
        - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
        - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
    networks:
        - rabbitmq_go_net

networks:
  rabbitmq_go_net:
    driver: bridge

运行它

docker compose up

如果Docker合成版本1.x

docker-compose up

使用此URL打开浏览器

http://localhost:15672/

2.2运行express和所有node.js
位于RabbitMQ-Logger-Example\infoMS目录

node app.js

位于RabbitMQ-Logger-Example\loggerMS目录

node server.js

位于RabbitMQ-Logger-Example\warningAndErrorMS目录

node app.js

现在准备出发

3:消息

快跑 Postman ,

1制作自己的收藏

2提出三个要求

请求之一-Send Info Message

1选择POST方法

2输入网址

localhost:3000/sendLog

3单击Body选项卡

4选择Raw单选按钮

5在body中输入消息

{
    "logType" : "Info",
    "message" : "I am Info message"
}

6选择JSON

7按下Send按钮

如果您看到

您就成功了!
其他两个不同类型的消息内容相同,只是logType的值不同。
Send Warning Message请求
对于Warning

{
    "logType" : "Warning",
    "message" : "I am Warning message"
}

Send Error Message请求
对于Error

{
    "logType" : "Error",
    "message" : "I am Error message"
}

这是真正的屏幕上的所有东西!

如果你浏览RabbitMQ的内容,你会发现很多东西。
这是Info通道信息的其中一个。

如果要终止RabbitMQ

CTRL+C
docker compose down

享受RabbitMQ!

相关问题