0

我在集群模式下运行 nats 服务器,配置如下:

port: 4222
net: '0.0.0.0'

cluster {
  listen: 0.0.0.0:6222
  routes: ["nats://0.0.0.0:6222"]
}

# NATS Streaming specific configuration
streaming {
  id: test_cluster
  store: file
  dir: "./cluster"
  cluster {
    log_path: /cluster/
    node_id: "a"
  }
}

我的应用程序尝试使用 nats-c 库连接到该集群,如下所示:

  stanConnection* connection = nullptr;
  stanConnOptions* options = nullptr;
  natsOptions* nats_options = nullptr;

  natsOptions_Create(&nats_options);
  natsOptions_SetURL(nats_options, "nats://localhost:4222");

  stanConnOptions_Create(&options);      
  stanConnOptions_SetNATSOptions(options, nats_options);

  natsStatus staus = stanConnection_Connect(&connection, "test_cluster", "service", options);

此调用的返回值为 NATS_TIMEOUT。从服务器的调试日志中,我可以看到客户端已连接但很快断开连接。

[64465] 2019/05/03 12:18:05.884075 [INF] Starting nats-server version 1.4.1
[64465] 2019/05/03 12:18:05.884164 [DBG] Go build version go1.11.5
[64465] 2019/05/03 12:18:05.884169 [INF] Git commit [not set]
[64465] 2019/05/03 12:18:05.884359 [INF] Listening for client connections on 0.0.0.0:4222
[64465] 2019/05/03 12:18:05.884396 [DBG] Server id is Pp7TGOtwoXdn0P2gVkzuHa
[64465] 2019/05/03 12:18:05.884400 [INF] Server is ready
[64465] 2019/05/03 12:18:05.884810 [INF] Listening for route connections on 0.0.0.0:6222
[64465] 2019/05/03 12:18:05.884951 [DBG] Trying to connect to route on 0.0.0.0:6222
[64465] 2019/05/03 12:18:05.885432 [INF] 127.0.0.1:38780 - rid:1 - Route connection created
[64465] 2019/05/03 12:18:05.886053 [DBG] 127.0.0.1:6222 - rid:2 - Route connect msg sent
[64465] 2019/05/03 12:18:05.887177 [DBG] 127.0.0.1:6222 - rid:2 - Router connection closed
[64465] 2019/05/03 12:18:05.887424 [DBG] Detected route to self, ignoring "nats://0.0.0.0:6222"
[64465] 2019/05/03 12:18:05.887471 [INF] 127.0.0.1:6222 - rid:2 - Route connection created
[64465] 2019/05/03 12:18:05.887555 [DBG] 127.0.0.1:38780 - rid:1 - Router connection closed
[64465] 2019/05/03 12:18:13.232390 [DBG] ::1:59868 - cid:3 - Client connection created
[64465] 2019/05/03 12:18:15.240495 [DBG] ::1:59868 - cid:3 - Client connection closed

知道为什么会这样吗?

4

1 回答 1

0

抱歉回复晚了,我建议你加入 Slack 或直接在 github repo ( https://github.com/nats-io )中提问

这里的问题是您以集群模式启动单个服务器,它将无法成为领导者并接受任何客户端连接。

客户端能够创建 NATS 连接,但流连接将失败,因为没有流服务器“接受”它。

此外,您显示的日志似乎只是 NATS,而不是流服务器。

于 2019-11-22T19:45:31.003 回答