停靠的spring云流服务,kafka代理无法连接到zookeeper

o75abkj4  于 2021-06-07  发布在  Kafka
关注(0)|答案(4)|浏览(338)

我正在测试一个示例spring云流应用程序(在ubuntulinux机器上运行),它有一个源和一个接收器服务。我所有的服务都是码头集装箱,我想使用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,出现了一个重复的连接拒绝错误集,并以 ZkTimeoutException 使服务终止(见下文)。
奇怪的是,如果不是将源(和接收器)测试服务作为docker容器运行,而是通过maven将它们作为jar文件运行 mvn spring-boot:run <etc...> 这些服务运行良好,能够通过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

你知道问题出在哪里吗?
编辑:
我发现在“jar”执行日志中 test-source 服务尝试通过ip连接到zookeeper 127.0.0.1 ,从下面截取的原木可以看出:

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可见),但这并不能解决问题:显然spring云流kafka配置忽略了这个属性 spring.cloud.stream.kafka.binder.zkNodes 如合同规定 application.yml (请注意,如果我从服务中记录该环境变量的值,我将看到 192.168.33.101 我在那里硬编码以进行调试)。

jvidinwx

jvidinwx1#

您已将defaultbinder设置为 rabbit 在尝试使用Kafka活页夹配置时。两者都有吗 rabbit 以及 kafka 应用程序类路径中的绑定?在这种情况下,您可以在这里启用

kx1ctssn

kx1ctssn2#

Zookeeper:
资料图:伍斯特迈斯特/Zookeeper

container_name: 'zookeeper'

ports:
  - 2181:2181

xpszyzbs

xpszyzbs4#

--
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

Spring:
配置文件:开发
云:
流:
Kafka

kafka:

   binder:

      brokers: kafka     # i added brokers and zkNodes property

      zkNodes: zookeeper #

        bindings:

        input:

        destination: message

        content-type: application/json

相关问题