官方给的教程很简单,如下:
//初始化项目目录
npm init -y
//安装依赖
npm install zeromq@4
然后就没有然后了。
结果发现问题真多
首先npm安装的时候出现第一个问题:
npm ERR! ----------------------------------------------------------------------
npm ERR! Libraries have been installed in:
npm ERR! /root/github/nodejs-course/third-party-package/zeromq-demo/node_modules/zeromq/zmq/lib
npm ERR!
npm ERR! If you ever happen to want to link against installed libraries
npm ERR! in a given directory, LIBDIR, you must either use libtool, and
npm ERR! specify the full pathname of the library, or use the `-LLIBDIR'
npm ERR! flag during linking and do at least one of the following:
npm ERR! - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
npm ERR! during execution
npm ERR! - add LIBDIR to the `LD_RUN_PATH' environment variable
npm ERR! during linking
npm ERR! - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
npm ERR! - have your system administrator add LIBDIR to `/etc/ld.so.conf'
npm ERR!
npm ERR! See any operating system documentation about shared libraries for
npm ERR! more information, such as the ld(1) and ld.so(8) manual pages.
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! g++: 错误:unrecognized command line option ‘-std=gnu++14’
npm ERR! make: *** [Release/obj.target/zmq/binding.o] 错误 1
npm ERR! gyp ERR! build error
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
一大批报错的,认真看,错误在这里:
npm ERR! g++: 错误:unrecognized command line option ‘-std=gnu++14
这个问题是因为你的系统的gcc版本过低。
因为CentOS目前自带的gcc是4.8.5的,需要升级gcc来解决这个问题。当然也有建议使用低版本的node环境重新安装node包的。
因为我用的是是node v16, 不想降级了。于是只能升级gcc.
centos使用yum升级gcc
yum -y install centos-release-scl
yum -y install devtoolset-8-gcc devtoolset-8-gcc-c++ devtoolset-8-binutils
scl enable devtoolset-8 bash
最后一个命令是在当前的bash下使用gcc8的环境。
如果你想一直用最新的gcc8,可以这样:
echo "source /opt/rh/devtoolset-8/enable" >> /etc/bashrc
source /etc/bashrc
然后输入命令看看目前的gcc版本:
这个好了之后,就可以直接安装zeromq了。
重新在目录下面运行:
npm install zeromq@4
然后就可以正常使用zeromq了。
附录zeromq的生产者与消费者的基础代码:
生产者producer.js
// producer.js
var zmq = require("zeromq"),
sock = zmq.socket("push");
sock.bindSync("tcp://127.0.0.1:3000");
console.log("Producer bound to port 3000");
setInterval(function() {
console.log("sending work");
sock.send("some work");
}, 500);
消费者worker.js
// worker.js
var zmq = require("zeromq"),
sock = zmq.socket("pull");
sock.connect("tcp://127.0.0.1:3000");
console.log("Worker connected to port 3000");
sock.on("message", function(msg) {
console.log("work: %s", msg.toString());
});
发布者 publisher.js
// pubber.js
var zmq = require("zeromq"),
sock = zmq.socket("pub");
sock.bindSync("tcp://127.0.0.1:3000");
console.log("Publisher bound to port 3000");
setInterval(function() {
console.log("sending a multipart message envelope");
sock.send(["kitty cats", "meow!"]);
}, 500);
订阅者sub.js
// subber.js
var zmq = require("zeromq"),
sock = zmq.socket("sub");
sock.connect("tcp://127.0.0.1:3000");
sock.subscribe("kitty cats");
console.log("Subscriber connected to port 3000");
sock.on("message", function(topic, message) {
console.log(
"received a message related to:",
topic,
"containing message:",
message
);
});