从包含内部的Spring Boot服务的Docker内部连接主机Kafka

时间:2019-06-25 12:37:28

标签: spring-boot docker apache-kafka

我想从同一台计算机上docker容器内的spring boot服务连接到本地计算机上托管的kafka代理。

春季启动中的Kafka侦听器配置。这可以在本地计算机上的容器外部运行:

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服务器属性文件:

  # 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文件:

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

运行命令:

docker run decoupling

1 个答案:

答案 0 :(得分:0)

在docker run命令中使用--network =“ host”,然后docker容器中的127.0.0.1将指向您的docker主机。