kafka WSL producer

背景

windows10 和windows 10 下的WSL(windows10 sub Linux)linux

在wsl中安装了

  • zookeeper(单节点模式)

  • kafka 单节点模式

    • kafka的config/server.properties文件如下

      # see kafka.server.KafkaConfig for additional details and defaults
      ​
      ############################# Server Basics #############################
      ​
      # The id of the broker. This must be set to a unique integer for each broker.
      broker.id=0
      ​
      # Switch to enable topic deletion or not, default value is false
      delete.topic.enable=true
      ​
      ############################# 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://0.0.0.0: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().
      # 172.17.160.188:9092为wsl的IP地址
      advertised.listeners=PLAINTEXT://172.17.160.188:9092
      ​
      # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
      #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

       

  • Java producer客户端代码

    package com.ncu.controller;
    ​
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.Producer;
    import org.apache.kafka.clients.producer.ProducerRecord;
    ​
    import java.util.Properties;
    ​
    public class MyKafkaProducer {
        public static void main(String[] args) {
    ​
            Properties props = new Properties();
            // Kafka服务端的主机名和端口号
            props.put("bootstrap.servers", "172.17.160.188:9092");
            // 等待所有副本节点的应答
            props.put("acks", "all");
            // 消息发送最大尝试次数
            props.put("retries", 0);
            // 一批消息处理大小
            props.put("batch.size", 16384);
            // 请求延时
            props.put("linger.ms", 1);
            // 发送缓存区内存大小
            props.put("buffer.memory", 33554432);
            // key序列化
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            // value序列化
            props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    ​
            Producer<String, String> producer = new KafkaProducer<>(props);
            for (int i = 0; i < 50; i++) {
                producer.send(new ProducerRecord<String, String>("first", Integer.toString(i), "hello world-" + i));
            }
    ​
            producer.close();
        }
    ​
    }
    ​

    遇到的问题

    1. win10 自带的Linux,本人弄混了windows10和其子系统Linux的IP地址。一直以为windows和Linux共用其IP地址,所以在Java producer生产者中的参数bootstrap.servers参数中一直用的是localhost,所以导致一直无法连接成功。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值