导致会话0x0关闭的异常:null - Java + Kafka + Zookeeper + Docker

时间:2018-03-28 09:49:32

标签: java docker apache-kafka docker-compose apache-zookeeper

我正在尝试使用KafkaProducer对象,然后我按照this guide来实现此目的。以下是使用的代码:

private final KafkaProducer producer;
private final Serializer serializer;

public KafkaEventPublisher(String zookeeper, Serializer serializer) {
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, zookeeper);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.CLIENT_ID_CONFIG, "KafkaProducerTest");

    this.producer = new KafkaProducer<>(props);
    this.serializer = serializer;
}

@Override
public <T extends Event> void publish(String streamName, T event) {
    try {
        String eventEnvelopeJson = serializer.serialize(event);

        ProducerRecord<String, String> record = new ProducerRecord<>(streamName, event.getId().toString(), eventEnvelopeJson);
        this.producer.send(record);
    } finally {
        this.producer.flush();
        this.producer.close();
    }
}

提供的zookeeper字符串的值为localhost:2181。下面是我的docker-compose文件:

version: '3'

services:
  rabbitmq:
    image: rabbitmq:management-alpine
    ports:
    - "5672:5672"
    - "15672:15672"

  zookeeper:
    image: zookeeper:3.4
    ports:
      - "2181:2181"

  kafka-manager:
    image: sheepkiller/kafka-manager
    ports:
      - "9000:9000"
    environment:
      - ZK_HOSTS=zookeeper:2181
    depends_on:
      - zookeeper
    links:
      - zookeeper

当我尝试运行测试用例并测试上面的代码时,我的docker-compose视图会收到此错误消息,并最终导致测试用例超时。

keeper_1      | 2018-03-28 09:05:34,828 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37638
zookeeper_1      | 2018-03-28 09:05:34,829 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:34,829 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37638 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:35,689 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37640
zookeeper_1      | 2018-03-28 09:05:35,692 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:35,693 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37640 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:36,772 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37642
zookeeper_1      | 2018-03-28 09:05:36,773 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:36,773 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37642 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:37,772 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37644
zookeeper_1      | 2018-03-28 09:05:37,773 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:37,773 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37644 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:38,877 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37646
zookeeper_1      | 2018-03-28 09:05:38,880 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:38,880 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37646 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:40,061 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37648
zookeeper_1      | 2018-03-28 09:05:40,062 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:40,062 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37648 (no session established for client)
zookeeper_1      | 2018-03-28 09:05:41,017 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@215] - Accepted socket connection from /172.20.0.1:37650
zookeeper_1      | 2018-03-28 09:05:41,019 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@383] - Exception causing close of session 0x0: null
zookeeper_1      | 2018-03-28 09:05:41,019 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1040] - Closed socket connection for client /172.20.0.1:37650 (no session established for client)

我无法在网上找到有关此错误的任何答案。我希望有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

问题来自这个错误的配置:

props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, zookeeper);

BOOTSTRAP_SERVERS_CONFIG指的是经纪人,而非动物园管理员。

您可以使用以下命令从zookeeper获取BOOTSTRAP_SERVERS_CONFIG

val zkUtils = ZkUtils(zookeeper, 3000, 3000, JaasUtils.isZkSecurityEnabled)
val brokers = zkUtils.getAllBrokersInCluster()
 .map(_.getBrokerEndPoint(ListenerName.forSecurityProtocol(SecurityProtocol.PLAINTEXT)).connectionString())
 .mkString(",")
...
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokers)

答案 1 :(得分:-1)

在文件kafka * \ config \ Consumer.properties和producer.properties中检查'bootstrap.servers'的值。

生产者和消费者港口的价值应该相同。默认情况下,通常为9092