我在 docker 容器中运行服务以及 kafka 和 zookeeper。
我启动了 akhq 服务,它就像一个用于查看 kafka 主题和消息的面板。
akhq 服务显示创建的主题和 kafka 消息。此服务在 kafka:9092 连接
但是另一项服务 adapter 无法连接到 kafka。出现错误:拨打 tcp :9092: 连接:连接被拒绝
虽然适配器服务也尝试连接到 kafka:9092 ,(尝试了 127.0.0.1:9092 和 localhost:9092)从适配器服务的控制台,地址 kafka:9092 被成功 ping。
我该如何解决?
我正在附加 docker-compose.yaml
version: '3'
volumes:
zookeeper-data:
driver: local
zookeeper-log:
driver: local
kafka-data:
driver: local
services:
akhq:
image: tchiotludo/akhq
environment:
AKHQ_CONFIGURATION: |
akhq:
security:
default-group: reader
connections:
docker-kafka-server:
properties:
bootstrap.servers: "kafka:9092"
schema-registry:
url: "http://schema-registry:8085"
ports:
- 8888:8080
links:
- kafka
- schema-registry
zookeeper:
image: confluentinc/cp-zookeeper
volumes:
- zookeeper-data:/var/lib/zookeeper/data:Z
- zookeeper-log:/var/lib/zookeeper/log:Z
environment:
ZOOKEEPER_CLIENT_PORT: '2181'
ZOOKEEPER_ADMIN_ENABLE_SERVER: 'false'
kafka:
image: confluentinc/cp-kafka
volumes:
- kafka-data:/var/lib/kafka/data:Z
environment:
KAFKA_BROKER_ID: '0'
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_NUM_PARTITIONS: '12'
KAFKA_COMPRESSION_TYPE: 'gzip'
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: '1'
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: '1'
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: '1'
KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://kafka:9092'
KAFKA_CONFLUENT_SUPPORT_METRICS_ENABLE: 'false'
KAFKA_JMX_PORT: '9091'
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
KAFKA_AUTHORIZER_CLASS_NAME: 'kafka.security.authorizer.AclAuthorizer'
KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: 'true'
links:
- zookeeper
schema-registry:
image: confluentinc/cp-schema-registry
depends_on:
- kafka
environment:
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'PLAINTEXT://kafka:9092'
SCHEMA_REGISTRY_HOST_NAME: 'schema-registry'
SCHEMA_REGISTRY_LISTENERS: 'http://0.0.0.0:8085'
SCHEMA_REGISTRY_LOG4J_ROOT_LOGLEVEL: 'INFO'
adapter: #kafka adapter
build:
context: .
dockerfile: ./adapter.dockerfile
environment:
- GO_ENV=dev
- GOPATH=/go
container_name: kafka-adapter-app
depends_on:
- kafka
这是错误
links
不会在服务之间创建依赖关系。必须使用depends_on
,然后它会docker compose
等待Kafka,然后它会启动akhq
底线是该服务在启动时立即尝试连接到 Kafka。卡夫卡此时还没有准备好。
为服务设置从启动服务到尝试连接的超时时间。