update 优化 完善kafka案例

This commit is contained in:
疯狂的狮子Li
2024-06-04 14:10:37 +08:00
parent 4c27b2609d
commit 3e57a42f39
3 changed files with 10 additions and 9 deletions

View File

@@ -14,11 +14,11 @@ import org.springframework.stereotype.Component;
public class KafkaNormalConsumer {
//默认获取最后一条消息
@KafkaListener(topics = "test-topic",groupId = "demo")
public void timiKafka(ConsumerRecord record){
@KafkaListener(topics = "test-topic", groupId = "test-group-id")
public void timiKafka(ConsumerRecord<String, String> record) {
Object key = record.key();
Object value = record.value();
log.info("【消费者】received the message key {}value{}",key,value);
log.info("【消费者】received the message key {}value{}", key, value);
}
}

View File

@@ -5,8 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;
/**
* @author xbhog
* @date 2024/05/19 18:02
@@ -16,10 +14,9 @@ import java.util.concurrent.CompletableFuture;
public class KafkaNormalProducer {
@Autowired
private KafkaTemplate kafkaTemplate;
private KafkaTemplate<String, String> kafkaTemplate;
public void sendKafkaMsg() {
CompletableFuture send = kafkaTemplate.send("test-topic", "hello", "kafkaTest");
send.join();
kafkaTemplate.send("test-topic", "hello", "kafkaTest");
}
}