因为公司在业务中需要用到消息队列产品,我选用了基于golang开源的nsq产品,记录下我遇到的那些部署中的坑。
首先安装nsq,这个没什么好说的,我是直接在官网下载bin文件,直接部署的,环境是centOS 6.7,安装在/opt/nsq-0.3.7.linux-amd64.go1.6目录下;
其次是安装nodejs,我安装的是v6.1.0版本,这步也没什么好讲;
然后安装nsqjs这个遇到了些坑,这里先记录下
1、要看下gcc的版本;
1 2 3 4 5
| $ gcc -v使用内建 specs。 目标:x86_64-redhat-linux 配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux 线程模型:posix gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
|
2、因为node.js 4升级了v8引擎,要求gcc的版本在4.8以上,所以要先更新gcc版本;
1 2 3 4 5 6
| $ rpm -ivh https://www.softwarecollections.org/en/scls/rhscl/devtoolset-3/epel-6-x86_64/download/rhscl-devtoolset-3-epel-6-x86_64.noarch.rpm $ yum install devtoolset-3-gcc-c++ 临时使用最新版gcc: $ scl enable devtoolset-3 bash 系统默认使用gcc-4.9 $ echo "source /opt/rh/devtoolset-3/enable" >>/etc/profile
|
3、然后安装nsqjs,为了项目的复用,我就用了全局安装,然后把nsqjs复制到项目的node_modules中就可以了;
4、把nsqjs复制到项目的node_modules目录下;
5、在项目中建立个app.js文件,输入以下代码并保存:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| var nsq = require('nsqjs'); var reader = new nsq.Reader('sys_topic', 'sys_chan', { lookupdHTTPAddresses: '127.0.0.1:4161' }); reader.connect(); reader.on('message', function(msg) { var t = new Date(); console.log('time [%s] Received message [%s]: %s', t.Format('yyyy-MM-dd hh:mm:ss'), msg.id, msg.body.toString()); msg.finish(); }); Date.prototype.Format = function(fmt) { var o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }
|
6、在nsqadmin的页面中,创建Topic为“sys_topic”和channel为“sys_chan”;
7、在应用终端中,运行这个js文件
8、在另外一个终端中发布一个消息
1
| $ curl -d '{"aa":"text","caption":"nsq_test","bool_v":true}' 'http:
|
9、看看到我们能非常快的接收到发布的消息