Kafka的java连接报连接不上集群服务器

博客详细记录了使用Java连接Kafka时遇到的连接错误,包括报错日志和解决步骤。首先,报错显示在发送消息到主题mykafka时遇到ClosedChannelException。解决思路包括检查Zookeeper和Kafka服务状态,确认主机hosts文件配置正确,并在修正后验证生产者和消费者可以正常运行。最后,展示了解决问题后的消费者日志。
摘要由CSDN通过智能技术生成

报错日志

Verifying properties
Property metadata.broker.list is overridden to 192.168.80.50:9092,192.168.80.51:9092,192.168.80.52:9092
Property serializer.class is overridden to kafka.serializer.StringEncoder
Fetching metadata from broker BrokerEndPoint(1,192.168.80.51,9092) with correlation id 0 for 1 topic(s) Set(mykafka)
Connected to 192.168.80.51:9092 for producing
Disconnecting from 192.168.80.51:9092
Connected to hadoop02:9092 for producing
Disconnecting from hadoop02:9092
Failed to send producer request with correlation id 2 to broker 1 with data for partitions [mykafka,0]
java.nio.channels.ClosedChannelException
at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
at kafka.producer.SyncProducer.liftedTree1 1 ( S y n c P r o d u c e r . s c a l a : 80 ) a t k a f k a . p r o d u c e r . S y n c P r o d u c e r . k a f k a 1(SyncProducer.scala:80) at kafka.producer.SyncProducer.kafka 1(SyncProducer.scala:80)atkafka.producer.SyncProducer.kafkaproducer S y n c P r o d u c e r SyncProducer SyncProducer d o S e n d ( S y n c P r o d u c e r . s c a l a : 79 ) a t k a f k a . p r o d u c e r . S y n c P r o d u c e r doSend(SyncProducer.scala:79) at kafka.producer.SyncProducer doSend(SyncProducer.scala:79)atkafka.producer.SyncProducer a n o n f u n anonfun anonfunsend 1 1 1 a n o n f u n anonfun anonfunapply m c V mcV mcVsp 1. a p p l y 1.apply 1.applymcV s p ( S y n c P r o d u c e r . s c a l a : 110 ) a t k a f k a . p r o d u c e r . S y n c P r o d u c e r sp(SyncProducer.scala:110) at kafka.producer.SyncProducer sp(SyncProducer.scala:110)atkafka.producer.SyncProducer a n o n f u n anonfun

Java中正确地连接和交互Kafka服务器通常需要以下几个步骤: 1. **添加依赖**:首先,你需要将Kafka客户端库(如`kafka-clients`)添加到你的Maven或Gradle项目中。 ```xml <!-- Maven --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>your-kafka-version</version> </dependency> <!-- Gradle --> implementation 'org.apache.kafka:kafka-clients:your-kafka-version' ``` 2. **创建Producer或Consumer实例**:根据你的需求,是生产者(Producer)还是消费者(Consumer)。例如,创建一个生产者的示例: ```java Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", Serdes.String().serializer()); props.put("value.serializer", Serdes.String().serializer()); KafkaProducer<String, String> producer = new KafkaProducer<>(props); ``` 3. **发送消息**(Producer): ```java producer.send(new ProducerRecord<>("my-topic", "key", "value")); producer.close(); ``` 4. **消费消息**(Consumer): ```java Properties consumerProps = new Properties(); consumerProps.put("bootstrap.servers", "localhost:9092"); consumerProps.put("group.id", "my-consumer-group"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(consumerProps, new StringDeserializer(), new StringDeserializer()); consumer.subscribe(Arrays.asList("my-topic")); ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value()); consumer.close(); ``` 5. **配置SSL/TLS**:如果你的Kafka集群是安全模式运行,记得提供相应的SSL/TLS属性,比如`security.protocol`, `ssl.truststore.location`, 等等。 6. **错误处理**:确保对可能出现的异常进行恰当的处理,如网络问题、权限问题等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值