安装
brew install nsq
启动
启动nsqlookupd
nsqlookupd

启动nsqd
nsqd --lookupd-tcp-address=127.0.0.1:4160


但是安装官网上这样操作在后面的步骤中遇到了问题,具体的稍后再讲。
后来我用的是:
nsqd --lookupd-tcp-address=127.0.0.1:4160 --broadcast-address=127.0.0.1
后面再讲原因
启动nsqadmin
nsqadmin --lookupd-http-address=127.0.0.1:4161
启动之后就可以访问http://127.0.0.1:4171/
,看到图形化界面了

建立topic
curl 'http://127.0.0.1:4161/topic/create?topic=test' -d ''

建立channel
这里建立3个channel
curl 'http://127.0.0.1:4161/channel/create?topic=test&channel=ch1' -d ''
curl 'http://127.0.0.1:4161/channel/create?topic=test&channel=ch2' -d ''
curl 'http://127.0.0.1:4161/channel/create?topic=test&channel=ch3' -d ''

启动nsq_to_file接收指定的topic
nsq_to_file --topic=test --channel=ch1 --output-dir=/tmp/ch1 --lookupd-http-address=127.0.0.1:4161
nsq_to_file --topic=test --channel=ch2 --output-dir=/tmp/ch2 --lookupd-http-address=127.0.0.1:4161
nsq_to_file --topic=test --channel=ch3 --output-dir=/tmp/ch3 --lookupd-http-address=127.0.0.1:4161
到这里就报如下错:

并且访问http://127.0.0.1:4171/时点击topic也会报错。

看上面的报错信息,应该就是连接不上nsqd.注意到nsqd的命令行参数。
-broadcast-address string
address that will be registered with lookupd (defaults to the OS hostname) (default "PROSNAKES.local")
默认是操作系统主机名,客户端无法识别。
在Mac上应该显示指定
nsqd --lookupd-tcp-address=127.0.0.1:4160 --broadcast-address=127.0.0.1

这样就可以连接成功了。
向指定topic发送消息
curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test'


网友评论