kafka 发送消息send报错,无法连接

Mr丶Dong° 发表于: 2018-01-25   最后更新时间: 2018-01-25  
  •   26 订阅,1049 游览
package kafka.demo;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;


/**
 * kafka-client
 * <dependency>
 *         <artifactId>kafka_2.12</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.0.0</version>
    </dependency>
 * 
 * @author zxc Mar 30, 2017 2:44:05 PM
 */
public class KafkaProducerDemo {


    protected static Properties props        = new Properties();
    protected static String     kafka_server = "120.79.44.52:8080";
    protected static String     topic        = "test";

    static {
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka_server);
        props.put(ProducerConfig.ACKS_CONFIG, "all");
        props.put(ProducerConfig.RETRIES_CONFIG, 0);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        // props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, CustomPartitioner.class);
    }

    public static void main(String[] args) throws Exception {
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        for (int i = 0; i < 100; i++) {
            producer.send(new ProducerRecord<String, String>(topic, Integer.toString(i), Integer.toString(i)), //
                          new org.apache.kafka.clients.producer.Callback() {

                              @Override
                              public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                                  if (e == null) {
                                      System.out.println("offset=" + recordMetadata.offset() + ",partition=" + recordMetadata.partition());
                                  } else {
                                      System.out.println("send error!" + e);
                                  }
                              }
                          });
        }
        TimeUnit.SECONDS.sleep(3);
        producer.close();
    }
}

但是执行的时直接报错了

Code here
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time
send error!org.apache.kafka.common.errors.TimeoutException: Expiring 100 record(s) for test-0: 30026 ms has passed since batch creation plus linger time

求指教问题呀,按照你http://orchome.com/342 这里提到的,修改ip,但是修改了也直接报错。

Code here
kafka.common.KafkaException: Socket server failed to bind to 120.79.44.52:8080: Cannot assign requested address.
    at kafka.network.Acceptor.openServerSocket(SocketServer.scala:331)
    at kafka.network.Acceptor.<init>(SocketServer.scala:256)
    at kafka.network.SocketServer.$anonfun$startup$1(SocketServer.scala:98)
    at kafka.network.SocketServer.$anonfun$startup$1$adapted(SocketServer.scala:89)
    at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:52)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at kafka.network.SocketServer.startup(SocketServer.scala:89)
    at kafka.server.KafkaServer.startup(KafkaServer.scala:229)
    at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:38)
    at kafka.Kafka$.main(Kafka.scala:92)
    at kafka.Kafka.main(Kafka.scala)
Caused by: java.net.BindException: Cannot assign requested address
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
    at kafka.network.Acceptor.openServerSocket(SocketServer.scala:327)
    ... 11 more






发表于: 3月前   最后更新时间: 3月前   游览量:1049
上一条: kafka发送消息慢
下一条: kafka单条消息3.22M,按照网上的解答修改了broker的配置,但还是发不出去,也不报错

评论…


  • 你的kafka端口怎么是8080呢?你自己设置的?
    • 那这样,是不是我只能在服务器上面才能访问,我本地通过公网访问,就无法进行了?我刚刚设置成私有地址,启动起来了,但是我本地运行代码还是报错:send error!org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

        • 我不太清楚你的网络环境是什么,设置内网地址之后,如果你有外网地址,要路由映射才能对外访问。就跟你开启apache,也要路由映射才能对公网访问。
            • 我通过telnet 端口是可以成功的,但是就是代码不行。我把地址给您,您看看您那边的代码是否能测试通过?我怀疑是我的代码问题了(telnet 120.79.44.52 9092)
                • 我刚才用了这3种命令分别试了下,生产者发送即报错。

                  ## 新生产者(支持0.9版本+)
                  bin/kafka-console-producer.sh --broker-list 120.79.44.52:9092 --topic test

                  ## 新消费者(支持0.9版本+)
                  bin/kafka-console-consumer.sh --bootstrap-server 120.79.44.52:9092 --new-consumer --from-beginning --topic test

                  ## 查看消费者列表(支持0.9版本+)
                  bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server 120.79.44.52:9092 --list

                  你在你本地执行试试,我怎么感觉你服务搭建的有问题呢。
                    • bin/kafka-console-producer.sh --broker-list 120.79.44.52:9092 --topic test
                      test
                      [2018-01-26 10:30:25,314] ERROR Error when sending message to topic test with key: null, value: 4 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
                      org.apache.kafka.common.errors.TimeoutException: Batch containing 1 record(s) expired due to timeout while requesting metadata from brokers for test-0
                        • 我在配置里面 设置了advertised.listeners  就可以了。但是我还想问一个问题,当我用两台机器去跑一个获取消息的main方法,却只有一台能接收,并没有负载两台电脑接收呢?
                          • 评论…
                            • in this conversation
                              提问