代码之家  ›  专栏  ›  技术社区  ›  JAN

卡夫卡。JS拒绝连接<<[BrokerPool]无法连接到种子代理,正在尝试列表中的另一个代理>>

  •  0
  • JAN  · 技术社区  · 5 年前

    想想Kafka Producer:

    const { Kafka, logLevel } = require('kafkajs');
    
    const kafka = new Kafka({
      clientId: 'my-app',
      brokers: ['kafka1:9092', 'kafka2:9092'],
      logLevel: logLevel.ERROR,
    });
    
    const run = async () => {
      const producer = kafka.producer();
    
      await producer.connect();
      await producer.send({
        topic: 'test-topic',
        messages: [{ value: 'Hello KafkaJS user!' }],
      });
    
      await producer.disconnect();
    };
    
    run();
    

    每当我点击:node producer。js,答案是:

    C:\Development-T410\Kafka>node producer
    {"level":"ERROR","timestamp":"2020-10-09T08:24:07.646Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka1:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:07.674Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":0,"retryTime":325}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:09.004Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka2:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:09.006Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":1,"retryTime":630}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:10.639Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka1:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:10.640Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":2,"retryTime":1228}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:12.871Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka2:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:12.872Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":3,"retryTime":2104}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:13.464Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka1","broker":"kafka1:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka1\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:14.789Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka2","broker":"kafka2:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka2\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:15.722Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka1","broker":"kafka1:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka1\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:15.978Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka1:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:15.979Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":4,"retryTime":4610}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:17.059Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka2","broker":"kafka2:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka2\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:17.980Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka1","broker":"kafka1:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka1\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:21.593Z","logger":"kafkajs","message":"[Connection] Connection timeout","broker":"kafka2:9092","clientId":"my-app"}
    {"level":"ERROR","timestamp":"2020-10-09T08:24:21.594Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection timeout","retryCount":5,"retryTime":8310}
    (node:9624) UnhandledPromiseRejectionWarning: KafkaJSNonRetriableError
      Caused by: KafkaJSConnectionError: Connection timeout
        at Timeout.onTimeout [as _onTimeout] (C:\Development-T410\Kafka\node_modules\kafkajs\src\network\connection.js:165:23)
        at listOnTimeout (internal/timers.js:549:17)
        at processTimers (internal/timers.js:492:7)
    (Use `node --trace-warnings ...` to show where the warning was created)
    (node:9624) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
    (node:9624) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    {"level":"ERROR","timestamp":"2020-10-09T08:24:22.857Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka2","broker":"kafka2:9092","clientId":"my-app","stack":"Error: getaddrinfo ENOTFOUND kafka2\n    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"}
    

    知道怎么解决吗?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Ravi    5 年前

    我试图重现同样的问题。但这段代码可以正常工作 brokers: ['localhost:9092'] 。此代码没有问题。您应该检查卡夫卡服务器。可能是卡夫卡服务器配置不正确。

    尝试从终端创建一个主题。您可以在Kafka安装文件夹的bin目录中找到所有脚本。

    kafka-topics.sh --bootstrap-server localhost:9092 --create --topic test-topic --partitions 1 --replication-factor 1
    

    检查主题是否已成功创建。如果没有,你的卡夫卡经纪人也有问题。

        2
  •  0
  •   karan shah    5 年前

    错误消息表明无法访问代理,因此您的nodejs代码无法建立连接。 检查

    • 卡夫卡经纪人和动物园管理员已经开始运作。
    • 仔细检查卡夫卡运行的端口。
    • 您使用的DNS/主机名是可解析的。

    您可以使用cli首先确保设置正确,然后运行producer&消费者

    经历 this 教程可能会有所帮助。

    推荐文章