我遇到了一个问题与 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.
4条答案
按热度按时间vmdwslir1#
您的错误来自adminclient,因此如果您没有在代码中手动创建该示例,则需要为其添加配置部分
spring.kafka.admin.*
也设置了引导服务器或者,如果所有三个客户机都与同一集群通信,则从生产者和消费者中移除引导服务器,并使用全局设置它
spring.kafka.bootstrap-servers
因为您使用的是docker网络,所以地址必须是kafka容器的主机名。还要注意的是links
是一个不推荐使用的撰写功能,不应该依赖exdqitrt2#
你能试试这个吗
zxlwwiss3#
我能解决我的问题。
这是我更新的docker-compose.yml,可以正常工作,其余配置与上面相同
所做的更改
我使用了融合的Kafka和zookeeper,但即使是上面的docker-compose.yml与wrustmeisterKafka和zookeeper都可以工作。
最重要的财产
环境:spring\u kafka\u bootstrapserver:kafka:29092
也正如@onecricketeer所建议的
或者,如果所有三个客户机都与同一集群通信,则从生产者和消费者中移除引导服务器,并使用spring.kafka.bootstrap-servers全局设置它
我觉得这样也行。
也许我可以稍后再试试,然后更新我的答案。
4smxwvx54#
我用几乎相同的方法做了同样的事情(差别很小),只要看看下面的撰写文件,如果这对你有用的话