如何使用Node.js在RabbitMQ中实现远程过程调用(RPC)

时间:2019-03-25 11:29:55

标签: node.js rabbitmq rpc amqp

所以我想使用一个Json并将其解析为Object,然后实现RPC RabbitMQ Server,以便我可以通过RabbitMQ将对象发送到Server,然后该Object将继续保存在本地Array和可以通过RPC从服务器返回到客户端的通用唯一ID,该ID会告诉您对象的确切存储位置。

官方网站显示了RabbitMQ中RPC的一些实现,在这里您可以找到其实现https://www.rabbitmq.com/tutorials/tutorial-six-javascript.html,在教程中,他们发送一个数字,服务器将计算斐波纳契数列并将结果返回给客户端。相反,我想发送一个对象,而不是一个数字,并且我想接收该对象的通用唯一id(uuid),并将其存储在程序中的全局数组中,我更改了代码,以便它可以发送一个对象并返回uuid,但没有成功。我会很感谢你们的帮助

    //this is my server_rpc.js code : 
   const amqp = require('amqplib/callback_api');
   const uuid = require("uuid/v1");

  amqp.connect('here is the url: example: localhost', (err, conn) => {

   conn.createChannel( (err, ch) => {

    let q = 'rpc_queue';

    ch.assertQueue(q, {durable: false});

    ch.prefetch(10);

    console.log(' [x] Waiting RPC requests');

    ch.consume(q, function reply(msg) {

        console.log("corralation key is: ", msg.properties.correlationId);
        let n = uuid();


        console.log(" data received ",JSON.parse(JSON.stringify(msg.content.toString())));

        console.log("corralation key is: ", msg.properties.correlationId);

        ch.sendToQueue(msg.properties.replyTo, Buffer.from(n.toString()), {correlationId: msg.properties.correlationId});

        ch.ack(msg);
    });
});

});

    // and this is my client_rpc.js code : 
    const amqp = require('amqplib/callback_api');
    const uuid = require("uuid/v1");
     const express = require("express");

let data = {
"name" : "hil01", 
"region" : "weissach",
"ID" : "1",
"version" : "0.0.1"
           } 



      amqp.connect('url: example localhost ', (err, conn) => {

        conn.createChannel( (err, ch) => {

         ch.assertQueue('', {exclusive: true}, (err, q) => {

        var corr = generateUuid();
        var newHil = JSON.stringify(data);

        console.log(" [x] Requesting uuid for the registered HIL: ", newHil );

        console.log("corralation key is: ", corr);

        ch.consume(q.queue, function(msg) {

            if(msg.properties.correlationId == corr) {

                console.log(" [.] Got %s", msg.content.toString());
                setTimeout(() => { conn.close(); process.exit(0) }, 100);
            }
        }, {noAck: true});

        ch.sendToQueue('rpc_queue', Buffer.from(newHil, {correlationId: corr, replyTo: q.queue }));
    });
});

});

    //method to generate the uuid, later will be replaced with the real 
   uuid function 
      var generateUuid = () => Math.random().toString() + 
      Math.random().toString() + Math.random().toString()  ;

当我运行server_rpc时,应打印[x]等待请求,然后在单独的cmd中运行client_rpc.js,然后应发送对象,服务器执行并将uuid返回给我。

>

1 个答案:

答案 0 :(得分:0)

似乎您需要的是对RPC模式的直接答复:您要发送消息并获得响应。

以下是RabbitMQ上有关直接回复的文档:https://www.rabbitmq.com/direct-reply-to.html

TL; TR

这是一个客户端和服务器的示例,它们可以在框中工作:

https://github.com/Igor-lkm/node-rabbitmq-rpc-direct-reply-to

内部内容

安装RabbitMQ后,您将有2个文件要执行:

server.js

{{1}}

client.js

{{1}}

您将得到一个结果,例如:

enter image description here