Apache kafka 如何在Quarkus中注入KafkaTemplate
我正在尝试注入一个KafkaTemplate来发送一条消息。我正在开发一个小函数,它位于被动方法之外 我只能找到使用Smallrye的@Ingoing和@Outgoing的示例,但我不需要KafkaStream 我尝试过卡夫卡CDI,但我无法注射SimpleKafkaProducer 有什么想法吗 克莱门特的回答 这似乎是正确的方向,但执行命令;我收到这个错误:Apache kafka 如何在Quarkus中注入KafkaTemplate,apache-kafka,quarkus,microprofile,Apache Kafka,Quarkus,Microprofile,我正在尝试注入一个KafkaTemplate来发送一条消息。我正在开发一个小函数,它位于被动方法之外 我只能找到使用Smallrye的@Ingoing和@Outgoing的示例,但我不需要KafkaStream 我尝试过卡夫卡CDI,但我无法注射SimpleKafkaProducer 有什么想法吗 克莱门特的回答 这似乎是正确的方向,但执行命令;我收到这个错误: (vert.x-eventloop-thread-3) Unhandled exception:java.lang.IllegalSt
(vert.x-eventloop-thread-3) Unhandled exception:java.lang.IllegalStateException: Stream not yet connected
我通过命令行从我的主题中消费,Kafka已启动并运行,如果我手动生成,我可以看到消费的消息
这似乎与doc的这句话有关:
要为流hello使用发射器,您需要一个@Incominghello
在代码或配置中的某个位置
我班上有以下代码:
@Incoming("orders")
public CompletionStage<Void> consume(KafkaMessage<String, String> msg) {
log.info("Received message (topic: {}, partition: {}) with key {}: {}", msg.getTopic(), msg.getPartition(), msg.getKey(), msg.getPayload());
return msg.ack();
}
也许我忘记了一些配置?所以,您只需要使用发射器: 为避免流尚未连接的异常,如doc所建议: 要为流hello使用发射器,您需要一个@Incominghello 在代码或配置中的某个位置 假设应用程序中有类似的内容。属性:
# Orders topic (READ)
smallrye.messaging.source.orders-r-topic.type=io.smallrye.reactive.messaging.kafka.Kafka
smallrye.messaging.source.orders-r-topic.topic=orders
smallrye.messaging.source.orders-r-topic.bootstrap.servers=0.0.0.0:9092
smallrye.messaging.source.orders-r-topic.key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
smallrye.messaging.source.orders-r-topic.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
smallrye.messaging.source.orders-r-topic.group.id=my-group-id
添加如下内容:
@Incoming("orders-r-topic")
public CompletionStage<Void> consume(KafkaMessage<String, String> msg) {
log.info("Received message (topic: {}, partition: {}) with key {}: {}", msg.getTopic(), msg.getPartition(), msg.getKey(), msg.getPayload());
return msg.ack();
}
因为注释已被弃用。注释
必须改用
您可以使用quarkus smallrye反应式消息传递卡夫卡依赖项提供的发射器生成到卡夫卡主题的消息
一个简单的卡夫卡生产者实现:
公共类MyKafkaProducer{
@注入
@引导我的话题
发射器/发射器;
公共void produceString消息{
myEmitter.sendmessage;
}
}
并且必须将以下配置添加到application.properties文件中:
mp.messaging.outgoing.my-topic.connector=smallrye-kafka
mp.messaging.outgoing.my-topic.bootstrap.servers=localhost:9092
mp.messaging.outgoing.my-topic.value.serializer=org.apache.kafka.common.serialization.StringSerializer
这将生成名为my topic的kafka主题的字符串序列化消息
请注意,默认情况下,频道名称也是将在其中生成数据的卡夫卡主题的名称。可以通过配置更改此行为。响应式消息中描述了支持的配置属性什么叫KafkaTemplate?我指的是KafkaProducer您可以共享代码吗?IllegalStateException看起来像一个bug。你什么时候发射物品?启动时?在HTTP请求时?我在application.properties中混淆了主题名称和流名称。现在一切正常。谢谢,方向似乎是对的,但仍然不起作用。我已经更新了我的问题
@Incoming("orders-r-topic")
public CompletionStage<Void> consume(KafkaMessage<String, String> msg) {
log.info("Received message (topic: {}, partition: {}) with key {}: {}", msg.getTopic(), msg.getPartition(), msg.getKey(), msg.getPayload());
return msg.ack();
}
mp.messaging.outgoing.my-topic.connector=smallrye-kafka
mp.messaging.outgoing.my-topic.bootstrap.servers=localhost:9092
mp.messaging.outgoing.my-topic.value.serializer=org.apache.kafka.common.serialization.StringSerializer