nodejs rabbitmq protobuf

nodejs rabbitmq protobuf,第1张

nodejs rabbitmq protobuf 一:安装erlang

       参考Installing on Debian and Ubuntu — RabbitMQ

二:安装rabbitmq

        Installing on Debian and Ubuntu — RabbitMQ

         rabbitmq server 下载地址:Releases · rabbitmq/rabbitmq-server (github.com)

         sudo dpkg -i rabbitmq-server_3.9.8-1_all.deb

三:安装nodejs

        Download | Node.js (nodejs.org)

下载完成后通过XFtp 或者其他类似软件传到服务器上,然后解压

tar -xJf node-v8.5.0-linux-x64.tar.xz  -C /opt   //解压到opt目录下

 建立链接到 /usr/local/bin/ 目录

sudo ln -s /opt/node-v8.5.0-linux-x64/bin/node /usr/local/bin/node    

然后跟npm建立执行链接

sudo ln -s /opt/node-v8.5.0-linux-x64/bin/npm /usr/local/bin/npm

验证安装是否正确

 npm更换国内源

命令
npm config set registry https://registry.npm.taobao.org
验证命令
npm config get registry

三:安装amqplib

        squaremo/amqp.node: AMQP 0-9-1 library and client for Node.JS (github.com)

        npm install amqplib

四:安装co

        npm install co

五:安装protobuf并测试

npm install google-protobuf

touch proto_exam.proto

syntax = "proto3";                                                                                                                     
package ccprotos;
message requestModel
{
     string msg = 10; 
     string code = 11; 
}

        生成proto_exam_pb.js文件

        protoc --js_out=import_style=commonjs,binary:. ./proto_exam.proto 

        probuf测试脚本

var basepb = require('./proto_exam_pb');//引用proto_exam_pb.js文件                                                                                               
var message = new basepb.requestModel();//创建requestModel对象
message.setMsg("hello"); 
message.setCode("200");
var bytes = message.serializeBinary();//序列化 
console.log(bytes);
var message2 = basepb.requestModel.deserializeBinary(bytes);//反序列化
console.log(message2);

        执行结果:

六:修改amqplib example源码,实现消息接收与解析
#!/usr/bin/env node

'use strict';
const co = require('co');
const amqp = require('amqplib');
const readline = require('readline');

co(function* () {
  const myConsumer = (msg) => {
    if (msg !== null) {
      // console.log(msg);
      var basepb = require('./trafficLight_pb');
      var message2 = basepb.LampRealInfo.deserializeBinary(msg.content);
      console.log(message2);
      console.log(message2.getLampcode());
    }
  };
  const conn = yield amqp.connect('amqp://localhost');
  try {
    const q = 'my-queue';
    const channel = yield conn.createChannel();
    yield channel.assertQueue(q, {
      durable: false
    });

    yield channel.consume(q, myConsumer, { noAck: true, durable: true });
  }
  catch (e) {
    throw e;
  }
}).catch(err => {
  console.warn('Error:', err);
});

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// pend until message is consumed
rl.question('newline to exit', () => process.exit());

七:执行测试脚本

node receive_generator.js

 

        

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5656466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存