Spring Kafka: UnknownHostException: 34bcfcc207e0

参考:

https://stackoverflow.com/questions/69527813/spring-kafka-unknownhostexception-34bcfcc207e0

我遇到的问题和@Adán Escobar是一样的。

在SpringBoot整合kafka的时候日志报了Spring Kafka: UnknownHostException: 34bcfcc207e0,34bcfcc207e0经过排查是容器的ID。

解决办法:

修改docker-compose.yml文件。

我使用的是docker compose创建启动的容器。

点击查看代码
version: '3.9'

services:
  redis:
    image: redis:latest
    container_name: redis
    restart: always
    ports:
      - "6379:6379"
    networks:
      - backend

  zookeeper:
    image: bitnami/zookeeper:latest
    container_name: zookeeper
    restart: always
    environment:
      ALLOW_ANONYMOUS_LOGIN: yes
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    networks:
      - backend

  kafka:
    image: bitnami/kafka:latest
    container_name: kafka
    restart: always
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    hostname: kafka
    environment:
      ALLOW_PLAINTEXT_LISTENER: yes
      KAFKA_CFG_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    networks:
      - backend

  kafka-ui:
    image: provectuslabs/kafka-ui:master
    container_name:  kafka-ui
    restart: always
    depends_on:
      - kafka
    ports:
      - "8080:8080"
    environment:
      KAFKA_CLUSTERS_0_NAME: dev
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
    networks:
      - backend

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: always
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
    networks:
      - backend

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    restart: always
    depends_on:
      - prometheus
    ports:
      - "3000:3000"
    networks:
      - backend

networks:
  backend:
    name: backend

在kafka配置下加上了hostname: kafka设置。

修改hosts文件

> 配置容器所在的服务器地址

# kafka
192.168.234.128 kafka

再次测试发现问题解决。
不清楚是否有更优雅的解决方案。等后续深入学习kafka再说。

posted @ 2023-11-08 16:26  雨中遐想  阅读(41)  评论(0编辑  收藏  举报