当前位置:   article > 正文

Kafka学习笔记:Java操作Kafka_kakfa host.name listeners

kakfa host.name listeners

0x0 搭建环境

上官网,看quick start
http://kafka.apache.org/quickstart
太简单不写了。

注意一点:
要配置server.properties中的一个属性:
advertised.host.name=主机IP
否则只能在本机连接kafka,外部无法访问。
可以看到配置文件中的说明:

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# 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://your.host.name:9092
advertised.host.name=192.168.80.224
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

0x1 Java代码

生产者:

    @Test
    public void testProducer() throws ExecutionException, InterruptedException {
        Properties props = new Properties();

        props.put("bootstrap.servers", "192.168.80.224:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        //创建kafka的生产者类
        Producer<String, String> producer = new KafkaProducer<>(props);

        System.out.println("producer is created!");

        String topic = "test";
        producer.send(new ProducerRecord<String, String>(topic, "idea-key2", "java-message 1")).get();
        producer.send(new ProducerRecord<String, String>(topic, "idea-key2", "java-message 2")).get();
        producer.send(new ProducerRecord<String, String>(topic, "idea-key2", "java-message 3")).get();

        producer.close();
        System.out.println("producer is closed!");

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

消费者:

@Test
    public void testConsumer() {
        String topic = "test";
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.80.224:9092");//用于建立与 kafka 集群连接的 host/port 组。
        props.put("group.id", "testGroup1");// Consumer Group Name
        props.put("enable.auto.commit", "true");// Consumer 的 offset 是否自动提交
        props.put("auto.commit.interval.ms", "1000");// 自动提交 offset 到 zookeeper 的时间间隔,时间是毫秒
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        Consumer<String, String> consumer = new KafkaConsumer(props);
        consumer.subscribe(Arrays.asList(topic));
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records)
                System.out.printf("partition = %d, offset = %d, key = %s, value = %s%n", record.partition(), record.offset(), record.key(), record.value());
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

以上代码可以进行测试,当然,可以打开kafka自带的生产者和消费者进行查看消息:

./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/985160
推荐阅读
相关标签
  

闽ICP备14008679号