Dockerized Spring Cloud Stream服务与Kafka代理无法连接到Zookeeper

时间:2017-02-14 15:00:54

标签: docker apache-kafka apache-zookeeper spring-cloud spring-cloud-stream

我正在使用一个源服务器和一个接收器服务测试示例Spring云流应用程序(在Ubuntu linux机器上运行)。我的所有服务都是docker-containerized,我想使用kafka作为消息代理。

docker-compose.yml的相关部分:

  zookeeper:
    image: confluent/zookeeper
    container_name: zookeeper
    ports:
      - "2181:2181"


  kafka:
    image: wurstmeister/kafka:0.9.0.0-1
    container_name: kafka
    ports:
      - "9092:9092"
    links:
      - zookeeper:zk
    environment:
      - KAFKA_ADVERTISED_HOST_NAME=192.168.33.101
      - KAFKA_ADVERTISED_PORT=9092
      - KAFKA_DELETE_TOPIC_ENABLE=true
      - KAFKA_LOG_RETENTION_HOURS=1
      - KAFKA_MESSAGE_MAX_BYTES=10000000
      - KAFKA_REPLICA_FETCH_MAX_BYTES=10000000
      - KAFKA_GROUP_MAX_SESSION_TIMEOUT_MS=60000
      - KAFKA_NUM_PARTITIONS=2
      - KAFKA_DELETE_RETENTION_MS=1000

    .
    .
    .

  # not shown: eureka service registry, spring cloud config service, etc.

  myapp-service-test-source:
    container_name: myapp-service-test-source
    image: myapp-h2020/myapp-service-test-source:0.0.1
    environment:
      SERVICE_REGISTRY_HOST: 192.168.33.101
      SERVICE_REGISTRY_PORT: 8761
    ports:
      - 8081:8080

    .
    .
    .

此处application.yml服务service-test-source的相关部分:

spring:
  cloud:
    stream:
      defaultBinder: kafka
      bindings:
        output:
          destination: messages
          content-type: application/json
      kafka:
        binder:
          brokers: ${SERVICE_REGISTRY_HOST:192.168.33.101}
          zkNodes: ${SERVICE_REGISTRY_HOST:192.168.33.101}
          defaultZkPort: 2181 
          defaultBrokerPort: 9092

问题如下,如果我在docker-compose容器日志中启动上面的test-source,我注意到该服务无法连接到zookeeper,给出了一组重复的 Connection拒绝错误,并以ZkTimeoutException结束,使服务终止(见下文)。

奇怪的是,如果不是将我的源(和接收器)测试服务作为docker容器运行,而是通过maven mvn spring-boot:run <etc...>将它们作为jar文件运行,服务工作正常,并且能够通过kafka交换消息。 (请注意,kafka,zookeeper等仍作为docker容器运行。)

.
.
.

*** THE FOLLOWING REPEATED n TIMES ***
2017-02-14 14:40:09.164  INFO 1 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-02-14 14:40:09.166  WARN 1 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect

java.net.ConnectException: Connection refused
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_111]
        at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_111]
        at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[zookeeper-3.4.6.jar!/:3.4.6-1569965]
        at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ~[zookeeper-3.4.6.jar!/:3.4.6-1569965]


.
.
.

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
        at java.lang.Thread.run(Thread.java:745)
        Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'outputBindingLifecycle'; nested exception is org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 10000

知道问题可能是什么?

修改

我发现在&#34; jar&#34;执行记录test-source服务尝试通过IP 127.0.0.1连接到zookeeper,如下面的日志中所示:

2017-02-15 14:24:04.159  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-02-15 14:24:04.159  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-02-15 14:24:04.178  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to localhost/127.0.0.1:2181, initiating session
2017-02-15 14:24:04.201  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15a421fd9ec000a, negotiated timeout = 10000
2017-02-15 14:24:05.870  INFO 10348 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=localhost:2181 sessionTimeout=6000 watcher=org.I0Itec.zkclient.ZkClient@72ba68e3
2017-02-15 14:24:05.882  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-02-15 14:24:05.883  INFO 10348 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session

这解释了为什么一切都在jar执行而不是docker(zookeeper容器将其2181端口导出到主机),因此当直接在主机上运行时,它可以作为服务进程的localhost显示但是,并没有解决问题:显然春天云流kafka配置忽略spring.cloud.stream.kafka.binder.zkNodes中设置的属性application.yml(请注意,如果我记录此类环境变量的值,该服务,我看到192.168.33.101的正确值,我在那里硬编码用于调试目的。)

2 个答案:

答案 0 :(得分:1)

在尝试使用Kafka活页夹配置时,您已将defaultBinder设置为rabbit。您的应用程序的类路径中是否包含rabbitkafka绑定器?在这种情况下,您可以启用here

答案 1 :(得分:0)

动物园管理员:

image:wurstmeister / zookeeper

container_name: 'zookeeper'

ports:
  - 2181:2181

--------------------- kafka ------------------------- -------

卡夫卡:

image:  wurstmeister/kafka
container_name: 'kafka'
environment:
  - KAFKA_ADVERTISED_HOST_NAME=kafka
  - KAFKA_ADVERTISED_PORT=9092
  - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
  - KAFKA_CREATE_TOPICS=kafka_docker_topic:1:1
ports:
  - 9092:9092
depends_on:
  - zookeeper

弹簧:

个人资料:dev

云:

流:

defaultBinder:kafka

kafka:

   binder:

      brokers: kafka     # i added brokers and zkNodes property

      zkNodes: zookeeper #

        bindings:

        input:

        destination: message

        content-type: application/json