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
求指教问题呀,按照你https://www.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