gpt4 book ai didi

docker - 如何从主机向Docker(bitnami)Apache Kafka发送消息?

转载 作者:行者123 更新时间:2023-12-02 18:52:03 27 4
gpt4 key购买 nike

我可以让我的Apache Kafka生产者在容器中运行时发送消息。但是,当我的生产者在主机中的容器外部运行时,它不起作用。我怀疑这是我的Docker撰写文件存在Docker网络问题,但我无法弄清楚。

我尝试过在线发布与我的问题类似的解决方案,但它们对我不起作用。救命!

  • Docker组成文件
  • version: '3'
    services:
    zookeeper:
    image: 'bitnami/zookeeper:latest'
    ports:
    - '2181:2181'
    environment:
    - ALLOW_ANONYMOUS_LOGIN=yes
    kafka:
    image: 'bitnami/kafka:latest'
    ports:
    - '9092:9092'
    environment:_
    - KAFKA_BROKER_ID=1
    - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
    - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
    - ALLOW_PLAINTEXT_LISTENER=yes

  • 主持人

  • //import util.properties packages
    import java.util.Properties;

    //import simple producer packages
    import org.apache.kafka.clients.producer.Producer;

    //import KafkaProducer packages
    import org.apache.kafka.clients.producer.KafkaProducer;

    //import ProducerRecord packages
    import org.apache.kafka.clients.producer.ProducerRecord;

    //Create java class named “SimpleProducer”
    public class SimpleProducer {

    public static void main(String[] args) throws Exception{

    // Check arguments length value
    if(args.length == 0){
    System.out.println("Enter topic name");
    return;
    }

    //Assign topicName to string variable
    String topicName = args[0].toString();

    // create instance for properties to access producer configs
    Properties props = new Properties();

    //Assign localhost id
    props.put("bootstrap.servers", "localhost:9092");

    //Set acknowledgements for producer requests.
    props.put("acks", "all");

    //If the request fails, the producer can automatically retry,
    props.put("retries", 0);

    //Specify buffer size in config
    props.put("batch.size", 16384);

    //Reduce the no of requests less than 0
    props.put("linger.ms", 1);

    //The buffer.memory controls the total amount of memory available to the producer for buffering.
    props.put("buffer.memory", 33554432);

    props.put("key.serializer",
    "org.apache.kafka.common.serialization.StringSerializer");

    props.put("value.serializer",
    "org.apache.kafka.common.serialization.StringSerializer");

    Producer<String, String> producer = new KafkaProducer
    <String, String>(props);

    for(int i = 0; i < 10; i++)
    producer.send(new ProducerRecord<String, String>(topicName,
    Integer.toString(i), Integer.toString(i)));
    System.out.println("Message sent successfully");
    producer.close();
    }
    }


    主机生产者应将消息发布到Docker Apache kafka,但事实并非如此。它创建了主题,但从未收到消息。我究竟做错了什么?这是位图像,而不是融合图像。

    最佳答案

    从我的previous answer here:

    What I needed to do was to declare the LISTENERS as both binding to the docker host, and then advertise them differently - one to the docker network, one to the host.


    services:
    zookeeper:
    image: confluentinc/cp-zookeeper
    ports:
    - "2181:2181"
    environment:
    ZOOKEEPER_CLIENT_PORT: 2181
    ZOOKEEPER_TICK_TIME: 2000
    ZOOKEEPER_SYNC_LIMIT: 2
    kafka:
    image: confluentinc/cp-kafka
    ports:
    - 9094:9094
    depends_on:
    - zookeeper
    environment:
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    KAFKA_LISTENERS: INTERNAL://kafka:9092,OUTSIDE://kafka:9094
    KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka:9092,OUTSIDE://localhost:9094
    KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,OUTSIDE:PLAINTEXT
    KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL

    现在您可以在本地主机上使用Kafka了:9094(根据OUTSIDE侦听器和docker-compose文件中的 ports条目),以及在Docker网络中:: 9092。

    关于docker - 如何从主机向Docker(bitnami)Apache Kafka发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56419545/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com