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
你的kafka端口怎么是8080呢?你自己设置的?
是的,但是默认的9092 也是一样的结果,这是什么情况呢?
listeners=PLAINTEXT://10.211.55.5:9092
这儿如果我指定了IP 就直接报错,你看我上面最后一个代码
你的地址是内网地址吗?
不是内网,是服务器的地址,但是所有涉及的端口我都开放了的
设置成内网的地址。不是说端口。
那这样,是不是我只能在服务器上面才能访问,我本地通过公网访问,就无法进行了?
我刚刚设置成私有地址,启动起来了,但是我本地运行代码还是报错:
send error!org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
我不太清楚你的网络环境是什么,设置内网地址之后,如果你有外网地址,要路由映射才能对外访问。就跟你开启apache,也要路由映射才能对公网访问。
你先telnet试试通不通。
我通过telnet 端口是可以成功的,但是就是代码不行。我把地址给您,您看看您那边的代码是否能测试通过?我怀疑是我的代码问题了(telnet 120.79.44.52 9092)
kafka manager 通过您的文章也搭建成功,能够访问了,就是代码现在跑不通,不知道为何,大神,我能加您QQ或者微信吗?
我刚才用了这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
你在你本地执行试试,我怎么感觉你服务搭建的有问题呢。
你上述的代码我在服务器运行是可以的呢?都没有报错~~ 好奇怪
这是集群管理工具地址:https://120.79.44.52:8080
防火墙关了试下
您那边三种命令都报错吗?我这边防火墙是未开的
只有发送是这样, 超时异常。
消费者
可以拿到信息吗?
拿不到消息的
好吧。我重新搭建试试,大神,您能否提供下您的封装代码呀?
先用官网的例子吧。什么都不要动
我在配置里面 设置了advertised.listeners 就可以了。但是我还想问一个问题,当我用两台机器去跑一个获取消息的main方法,却只有一台能接收,并没有负载两台电脑接收呢?
advertised.listeners新版本弃用了,用listeners。
你的答案