0


【Kafka】Java整合Kafka

1.引入依赖

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>2.3.1</version>
  5. </dependency>

2.搭建生产者

  1. package com.wen.kafka;
  2. import org.apache.kafka.clients.producer.*;
  3. import org.apache.kafka.common.serialization.StringSerializer;
  4. import java.util.Properties;
  5. import java.util.concurrent.ExecutionException;
  6. public class MyProducer {
  7. public static void main(String[] args) throws ExecutionException, InterruptedException {
  8. //配置信息
  9. Properties prop = new Properties();
  10. prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.117.80:9092");
  11. prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
  12. prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
  13. //创建生产者
  14. Producer<String,String> producer = new KafkaProducer<String, String>(prop);
  15. //创建消息
  16. ProducerRecord<String,String> record = new ProducerRecord<>("test", "hello kafka-client");
  17. //同步发送消息
  18. // RecordMetadata metadata = producer.send(record).get();
  19. // System.out.println("同步消息——topic:"+metadata.topic()+"partition"+metadata.partition()+"offset"+metadata.offset());
  20. //异步发送消息
  21. producer.send(record, new Callback() {
  22. @Override
  23. public void onCompletion(RecordMetadata recordMetadata, Exception e) {
  24. if (e != null) {
  25. System.out.println(e.getMessage());
  26. }
  27. if (recordMetadata != null) {
  28. System.out.println("异步消息——topic:"+recordMetadata.topic()+"partition"+recordMetadata.partition()+"offset"+recordMetadata.offset());
  29. }
  30. }
  31. });
  32. Thread.sleep(1000);
  33. }
  34. }

3.搭建消费者

  1. package com.wen.kafka;
  2. import org.apache.kafka.clients.consumer.*;
  3. import org.apache.kafka.common.serialization.StringDeserializer;
  4. import java.time.Duration;
  5. import java.util.Arrays;
  6. import java.util.Properties;
  7. public class MyConsumer {
  8. public static void main(String[] args) {
  9. //参数信息
  10. Properties prop = new Properties();
  11. prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.117.80:9092");
  12. prop.put(ConsumerConfig.GROUP_ID_CONFIG,"testGroup");
  13. prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
  14. prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
  15. //创建消费者
  16. Consumer<String,String> consumer = new KafkaConsumer<String, String>(prop);
  17. //订阅主题
  18. consumer.subscribe(Arrays.asList("test"));
  19. //拉取消息
  20. while (true){
  21. ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
  22. for (ConsumerRecord<String, String> record : records) {
  23. System.out.println(record.value());
  24. }
  25. }
  26. }
  27. }
标签: kafka java linq

本文转载自: https://blog.csdn.net/qq_58137891/article/details/134533778
版权归原作者 key< 所有, 如有侵权,请联系我们删除。

“【Kafka】Java整合Kafka”的评论:

还没有评论