小编Ebi*_*Man的帖子

动态主题名称 / Quarkus SmallRye 反应式消息传递 Kafka

我想使用这个扩展:[Quarkus Smallrye Reactive Messaging Kafka]

但在我的应用程序中,主题的名称是事先未知的,它是根据运行时从用户收到的消息指定的。如何在没有注释的情况下以编程方式指定主题名称和与主题相关的设置?(仅用于向 Kafka 发送消息 -> Produce)

@ApplicationScoped
public class PriceGenerator {

    private Random random = new Random();

    // Don't want to use this 
    // "generated-price" not known at build time
    @Outgoing("generated-price")                       
    public Multi<Integer> generate() {                  
        return Multi.createFrom().ticks().every(Duration.ofSeconds(5))
                .onOverflow().drop()
                .map(tick -> random.nextInt(100));
    }

}
Run Code Online (Sandbox Code Playgroud)

或者这些配置应该在运行时以编程方式设置

mp.messaging.outgoing.generated-price.connector=smallrye-kafka
mp.messaging.outgoing.generated-price.topic=prices
mp.messaging.outgoing.generated-price.value.serializer=org.apache.kafka.common.serialization.IntegerSerializer

Run Code Online (Sandbox Code Playgroud)

因为不认识路,所以使用了原生的Kafka驱动

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-kafka-client</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)
Properties props = new Properties();
props.put("bootstrap.servers", "85.93.89.115:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("linger.ms", 1);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
producer.send(new …
Run Code Online (Sandbox Code Playgroud)

java apache-kafka quarkus smallrye-reactive-messaging smallrye

6
推荐指数
1
解决办法
2627
查看次数