当spring引导容器尝试连接kafka容器时,出现“broker可能不可用”错误

9udxz4iz  于 2021-06-04  发布在  Kafka
关注(0)|答案(4)|浏览(593)

我遇到了一个问题与 Spring 启动Kafka码头,尝试了所有的事情在下面的链接中提到,但问题仍然存在。
Spring Boot容器无法连接到Kafka容器
附加mydocker-compose.yml

version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    ports:
    - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    ports:
    - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  my-app:
    image: my-app
    container_name: my-app
    ports:
      - "8081:8081"
    depends_on:
      - zookeeper
      - kafka

错误日志

-1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
my-app     | 2020-11-03 08:10:21.444  WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

我正在运行wrustmiester/kafka和wrustmiester/zookeeper。如果我在本地运行springboot应用程序,一切都会顺利运行。
当我尝试构建spring启动应用程序的映像并运行该映像时,问题就出现了。那么错误是代理可能不可用。
如果有人能指引我,请。
添加我的应用程序.yml

spring:
  kafka:
    consumer:
      bootstrap-servers: localhost:9092
      group-id: group_id
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      #org.apache.kafka.common.serialization.StringDeserializer
      properties.spring.json.trusted.packages: com.myapp.pojo

    producer:
      bootstrap-servers: localhost:9092
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
      #org.apache.kafka.common.serialization.StringSerializer

将my docker-compose.yml更新为

version: '3'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    network_mode: bridge
    ports:
    - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    network_mode: bridge
    ports:
    - "9092:9092"
    hostname: kafka
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
     # KAFKA_LISTENERS: INSIDE://:9092,OUTSIDE://:9094
     # KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
     # KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ADVERTISED_PORT: 9092
    links:
      - zookeeper
  my-app:
    image: my-app
    container_name: my-app
    network_mode: bridge
    ports:
      - "8081:8081"
    depends_on:
      - zookeeper
      - kafka

将my application.yml更新为

spring:
  kafka:
    consumer:
      bootstrap-servers: kafka:9092
      group-id: group_id
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
      #org.apache.kafka.common.serialization.StringDeserializer
      properties.spring.json.trusted.packages: com.myapp.pojo

    producer:
      bootstrap-servers: kafka:9092
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
      #org.apache.kafka.common.serialization.StringSerializer

错误日志相同

WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
my-app     | 2020-11-03 17:05:39.585  WARN 1 --- [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
vmdwslir

vmdwslir1#

您的错误来自adminclient,因此如果您没有在代码中手动创建该示例,则需要为其添加配置部分 spring.kafka.admin.* 也设置了引导服务器
或者,如果所有三个客户机都与同一集群通信,则从生产者和消费者中移除引导服务器,并使用全局设置它 spring.kafka.bootstrap-servers 因为您使用的是docker网络,所以地址必须是kafka容器的主机名。还要注意的是 links 是一个不推荐使用的撰写功能,不应该依赖

exdqitrt

exdqitrt2#

你能试试这个吗

version: '3'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    ports:
    - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    container_name: kafka
    ports:
    - "9092:9092"
    hostname: kafka
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
     # KAFKA_LISTENERS: INSIDE://:9092,OUTSIDE://:9094
     # KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
     # KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ADVERTISED_PORT: 9092
    links:
      - zookeeper
  my-app:
    image: my-app
    container_name: my-app
    ports:
      - "8081:8081"
    depends_on:
      - zookeeper
      - kafka
networks:
  default:
    external:
      name: bridge
zxlwwiss

zxlwwiss3#

我能解决我的问题。
这是我更新的docker-compose.yml,可以正常工作,其余配置与上面相同

version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

  my-app:
    image: my-app
    container_name: my-app
    environment:
      SPRING_KAFKA_BOOTSTRAPSERVERS: kafka:29092
    ports:
      - "8081:8081"
    depends_on:
      - kafka

所做的更改
我使用了融合的Kafka和zookeeper,但即使是上面的docker-compose.yml与wrustmeisterKafka和zookeeper都可以工作。
最重要的财产
环境:spring\u kafka\u bootstrapserver:kafka:29092
也正如@onecricketeer所建议的
或者,如果所有三个客户机都与同一集群通信,则从生产者和消费者中移除引导服务器,并使用spring.kafka.bootstrap-servers全局设置它
我觉得这样也行。
也许我可以稍后再试试,然后更新我的答案。

4smxwvx5

4smxwvx54#

我用几乎相同的方法做了同样的事情(差别很小),只要看看下面的撰写文件,如果这对你有用的话

version: '3'

services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - 2181:2181
  kafka:
    image: wurstmeister/kafka
    ports:
      - 9092:9092
    hostname: kafka
    environment:
      - KAFKA_ADVERTISED_HOST_NAME=kafka
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_ADVERTISED_PORT=9092
    links:
      - zookeeper

相关问题