[英]Connect host kafka from inside docker contained spring boot service
I would like to connect to a kafka broker hosted on my local machine from a spring boot service inside a docker container on the same machine. 我想从同一台机器上的docker容器内的spring boot服务连接到我本地计算机上托管的kafka代理。
Kafka Listener config in spring boot. Kafka Listener配置在春季启动。 This works outside of the container on the local machine:
这适用于本地计算机上的容器外部:
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ConsumerConfig.GROUP_ID_CONFIG, "my_test_application");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
Kafka server properties file: Kafka服务器属性文件:
# Hostname and port the broker will advertise to producers and consumers.
If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use
the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://localhost:9092
Docker file of image: Docker文件的图片:
FROM openjdk:8-slim
FROM apacheignite/ignite:2.4.0
COPY decoupling.jar decoupling.jar
EXPOSE 9081 3306 9092 2181
CMD java -jar decoupling.jar
run command: 运行命令:
docker run decoupling
在docker run命令中使用--network =“host”,然后docker容器中的127.0.0.1将指向您的docker主机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.