简体   繁体   English

使用Kafka和Zookeeper运行微服务时docker-compose up错误

[英]Error with docker-compose up when running a micro service with kafka and zookeeper

SOLVED! 解决了! thank you guys, i am going to update the correct config to work. 谢谢大家,我要更新正确的配置才能工作。

I'm trying to do my first microservice with spring-boot, kafka and zookeeper through docker-compose. 我正在尝试通过docker-compose使用spring-boot,kafka和zookeeper做我的第一个微服务。 I'm following in the footsteps of https://docs.docker.com/compose/gettingstarted/ but when I try to raise the services with the docker-compose up command I'm getting an exception: 我正在遵循https://docs.docker.com/compose/gettingstarted/的脚步,但是当我尝试使用docker-compose up命令提高服务质量时,却遇到了异常:

demo-quartz_1  | web - 2018-05-09 14:57:48,824 [scheduler_Worker-2] INFO  c.a.d.service.SampleJobService - created entity...
demo-quartz_1  | web - 2018-05-09 14:57:48,824 [scheduler_Worker-2] INFO  c.a.d.service.SampleJobService - BitcoinEuroEntity [idBCEntity=8, id=bitcoin, name=Bitcoin, symbol=BTC, rank=1, priceUsd=9299.31, priceBtc=1.0, _24hVolumeUsd=7170100000.0, marketCapUsd=158311453440, availableSupply=17024000.0, totalSupply=17024000.0, maxSupply=21000000.0, percentChange1h=0.05, percentChange24h=1.36, percentChange7d=1.7, lastUpdated=1525877672, priceEur=7830.80946135, _24hVolumeEur=6037833658.5, marketCapEur=133311700270]
demo-quartz_1  | web - 2018-05-09 14:58:38,868 [scheduler_Worker-1] ERROR o.s.k.s.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='BitcoinEuroKafkaEntity [id=bitcoin, name=Bitcoin, symbol=BTC, rank=1, priceUsd=9299.31, priceBtc=1.0...' to topic aironman:
demo-quartz_1  | org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

I can see that zookeeper, kafka and micro service containers are running fine within their containers: 我可以看到zookeeper,kafka和微服务容器在它们的容器中运行良好:

$:demo-quartz aironman$ docker ps
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                                NAMES
f9201046956c        wurstmeister/kafka                    "start-kafka.sh"         7 minutes ago       Up 6 minutes        0.0.0.0:9092->9092/tcp                               demo-quartz_kafka_1
386270f126da        aironman/demo-quartz:0.0.1-SNAPSHOT   "/usr/bin/java -jar …"   7 minutes ago       Up 6 minutes                                                             demo-quartz_demo-quartz_1
ac1df2c6429d        wurstmeister/zookeeper                "/bin/sh -c '/usr/sb…"   7 minutes ago       Up 6 minutes        22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp   demo-quartz_zookeeper_1

This is the GitHub repo develop branch 这是GitHub repo开发分支

Docker file looks like: Docker文件如下所示:

FROM openjdk:8-jre
ENTRYPOINT ["/usr/bin/java", "-jar", "/usr/share/aironman/demo-quartz.jar"]
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/aironman/demo-quartz.jar

Docker-compose.yml file looks like: Docker-compose.yml文件如下所示:

version: '3.6'
services:
demo-quartz:
image: aironman/demo-quartz:0.0.1-SNAPSHOT
deploy:
  replicas: 5
  resources:
    limits:
      cpus: "0.5"
      memory: 512M
  restart_policy:
      condition: on-failure

  zookeeper:
  image: wurstmeister/zookeeper
  ports:
  - "2181:2181"

  kafka:
  image: wurstmeister/kafka
  ports:
  - "9092:9092"
  environment:
  KAFKA_BROKER_ID: 1
  KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
  KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock

This is a link of the complete output of docker-compose up command: 这是docker-compose up命令的完整输出的链接

application.properties file of the micro service looks like: 微服务的application.properties文件如下所示:

using.spring.schedulerFactory=true
kafka.bootstrapAddress=kafka:9092
message.topic.name=aironman
greeting.topic.name=greeting
filtered.topic.name=filtered
partitioned.topic.name=partitioned

I have been looking at other threads in stackoverflow with similar problems but none of them help me to solve the problem by raising the microservice along with zookeeper and kafka talking to each other without any problem. 我一直在寻找类似问题的stackoverflow中的其他线程,但是它们都没有通过与动物园管理员和kafka互相交谈而提升微服务来解决问题。

It looks like the micro service is not able to communicate with kafka and zookeeper isn't, because, docker-compose runs the container but micro service container cannot talk each other. 看起来微服务无法与kafka通信,而zookeeper无法与之通信,因为docker-compose运行该容器,但微服务容器无法互相通信。 If i run the process locally using mvn spring-boot:run and running locally zookeeper and kafka, it works fine, i can push the message into the topic. 如果我使用mvn spring-boot:run在本地运行该进程,并在本地运行zookeeper和kafka,则工作正常,我可以将消息推送到主题中。

Can you guide me to the right solution? 您能指导我找到正确的解决方案吗?

Thank you guys, i learnt a lot! 谢谢大家,我学到了很多东西!

:demo-quartz aironman$ docker exec -it 693ab89a26fb  /bin/sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:13:00:03  
      inet addr:172.19.0.3  Bcast:172.19.255.255  Mask:255.255.0.0
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:227 errors:0 dropped:0 overruns:0 frame:0
      TX packets:390 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:20677 (20.1 KiB)  TX bytes:30809 (30.0 KiB)

lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:6 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1 
      RX bytes:590 (590.0 B)  TX bytes:590 (590.0 B)

/ # ping 172.19.0.2
PING 172.19.0.2 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.190 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.167 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.121 ms
64 bytes from 172.19.0.2: seq=3 ttl=64 time=0.173 ms
64 bytes from 172.19.0.2: seq=4 ttl=64 time=0.173 ms
^C
--- 172.19.0.2 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.121/0.164/0.190 ms
/ # 
/ # 
/ # 
/ # kafka-topics.sh --list --zookeeper 172.19.0.2:2181
/ # kafka-topics.sh --create --zookeeper 172.19.0.2:2181 --replication-factor 1 
--partitions 1 --topic aironman 
Created topic "aironman".
/ # kafka-topics.sh --list --zookeeper 172.19.0.2:2181
aironman

I can see that the topic is created in kafka broker, but exception still happen. 我可以看到该主题是在kafka代理中创建的,但仍然会发生异常。

I got it to work with this configuration for Kafka. 我可以在Kafka的这种配置下使用它。

kafka:
image: wurstmeister/kafka
depends_on:
  - zookeeper
ports:
  - "9092:9092"
environment:
  KAFKA_BROKER_ID: 1
  KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
  KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092

The key configuration is to have the docker service name and the advertised host name the same (kafka in this case).In your application.properties, you can use kafka.bootstrapAddress=kafka:9092. 关键配置是使docker服务名称和发布的主机名相同(在本例中为kafka)。在application.properties中,可以使用kafka.bootstrapAddress = kafka:9092。

Since all the docker services are written in the same docker-compose.yml, all of them will participate in the same default network and will be able to resolve each other by the service name. 由于所有docker服务都写在同一个docker-compose.yml中,因此它们都将参与相同的默认网络,并且能够通过服务名称相互解析。

Update : Using docker-compose from the original question and with few updates. 更新 :使用来自最初问题的docker-compose并进行少量更新。

 version: '3.3'
    services:
      demo-quartz:
        image: aironman/demo-quartz:0.0.1-SNAPSHOT

      zookeeper:
        image: wurstmeister/zookeeper
        ports:
          - "2181:2181"
      kafka:
        image: wurstmeister/kafka
        ports:
          - "9092:9092"
        environment:
          KAFKA_BROKER_ID: 1
          KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
          KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092

This works perfectly fine. 这工作得很好。 Have you updated the bootstrap url to bootstrap.servers = kafka:9092? 您是否已将引导程序网址更新为bootstrap.servers = kafka:9092? Also, demo-quartz is using old version of Kafka Client. 另外,demo-quartz正在使用旧版本的Kafka Client。 Please update. 请更新。

web - 2018-05-09 18:17:29,260 [scheduler_Worker-1] INFO  o.a.kafka.common.utils.AppInfoParser - Kafka version : 0.10.1.1
web - 2018-05-09 18:17:29,260 [scheduler_Worker-1] INFO  o.a.kafka.common.utils.AppInfoParser - Kafka commitId : f10ef2720b03b247

You can build a sample kafka producer docker image from here: https://github.com/vallikranth/kafka-spring-producer and verify your setup. 您可以从此处构建示例kafka生产者docker映像: https : //github.com/vallikranth/kafka-spring-producer并验证您的设置。

I am by no means a Docker expert but I believe that the default network type that is created is a bridge network. 我绝不是Docker专家,但我相信创建的默认网络类型是桥接网络。 With bridge networks you need to link services explicitly so that they can see each other. 使用网桥网络时,您需要显式链接服务,以便它们可以彼此看到。

For example: version: '3.6' services: demo-quartz: image: aironman/demo-quartz:0.0.1-SNAPSHOT deploy: replicas: 5 resources: limits: cpus: "0.5" memory: 512M restart_policy: condition: on-failure links: - kafka - zookeeper zookeeper: image: wurstmeister/zookeeper ports: - "2181:2181" kafka: image: wurstmeister/kafka ports: - "9092:9092" links: - zookeeper environment: KAFKA_ADVERTISED_HOST_NAME: 192.168.99.100 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 volumes: - /var/run/docker.sock:/var/run/docker.sock 例如: version: '3.6' services: demo-quartz: image: aironman/demo-quartz:0.0.1-SNAPSHOT deploy: replicas: 5 resources: limits: cpus: "0.5" memory: 512M restart_policy: condition: on-failure links: - kafka - zookeeper zookeeper: image: wurstmeister/zookeeper ports: - "2181:2181" kafka: image: wurstmeister/kafka ports: - "9092:9092" links: - zookeeper environment: KAFKA_ADVERTISED_HOST_NAME: 192.168.99.100 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 volumes: - /var/run/docker.sock:/var/run/docker.sock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM