nsq无法通过连接到nsqlookupd来使用消息

时间:2018-01-31 07:43:18

标签: docker go docker-compose nsq go-nsq

我尝试使用docker-compose运行nsq,docker-compose.yml如下:

version: '3'
services:
  nsqlookupd:
    image: nsqio/nsq
    command: /nsqlookupd
    ports:
      - "4160:4160"
      - "4161:4161"
  nsqd:
    image: nsqio/nsq
    command: /nsqd --lookupd-tcp-address=nsqlookupd:4160
    depends_on:
      - nsqlookupd
    ports:
      - "4150:4150"
      - "4151:4151"
  nsqadmin:
    image: nsqio/nsq
    command: /nsqadmin --lookupd-http-address=nsqlookupd:4161
    depends_on:
      - nsqlookupd
    ports:
      - "4171:4171"

我使用nsq客户端go-nsq来生成和使用消息,可以通过直接连接到nsqd来消费消息,但是不能通过连接到nsqlookupd来消费:

consumer.ConnectToNSQD("127.0.0.1:4150")  # success (output the consumed messages)

consumer.ConnectToNSQLookupd("127.0.0.1:4161")   # failed

2018/01/31 16:39:12 ERR    1 [test/liu] (967fcc2c88ae:4150) error connecting to nsqd - dial tcp: i/o timeout

我可以连接到nsqlookup实例:

➜  test_nsq curl http://127.0.0.1:4161/ping
OK%
➜  test_nsq curl http://127.0.0.1:4161/nodes
{"producers":[{"remote_address":"172.22.0.3:59988","hostname":"967fcc2c88ae","broadcast_address":"967fcc2c88ae","tcp_port":4150,"http_port":4151,"version":"1.0.0-compat","tombstones":[false],"topics":["test"]}]}%

源代码链接:

https://gist.github.com/liuzxc/1baf85cff7db8dee8c26b8707fc48799

ENV:

OS: Mac EI Capitan 10.11.6
go version: 1.9.2
nsq: 1.0.0-compat(latest)

对此有何想法?

1 个答案:

答案 0 :(得分:2)

我输出go-nsq日志,然后查找根本原因,应该为nsqd命令添加 if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty() && !(name.length() < 4) && !(password.length()<5) && email.lastIndexOf(".") - email.indexOf("@") > 2 && email.contains(".") && email.lastIndexOf("@") < email.lastIndexOf(".") && email.contains("@") ) { registerUser(name, email, password); } else { Toast.makeText(getApplicationContext(), "Something is Wrong! ", Toast.LENGTH_LONG) .show(); } ,否则,nsqd会将其主机名注册到nsqlookupd,客户端无法解析它。