简体   繁体   English

为 Kafka Consumer 随机生成的组 ID

[英]Randomly generated group id for Kafka Consumer

We have an application that uses spring kafka to read messages.我们有一个使用 spring kafka 读取消息的应用程序。 It is necessary that each instance of the application has a unique groupId, as well as reset it and get a new one on restart.应用程序的每个实例都必须有一个唯一的 groupId,并重置它并在重新启动时获得一个新的。 The GroupId is randomly generated via ${random.uuid} . GroupId 是通过${random.uuid}随机生成的。

Is the solution to generate randomly id really correct?随机生成 id 的解决方案真的正确吗?

Yes, generating through ${random.uuid} is correct.是的,通过${random.uuid}生成是正确的。

spring.kafka.consumer.group-id=${random.uuid}

There is also another choice if you want more control over the way the group id is generated.如果您想更好地控制组 ID 的生成方式,还有另一种选择。 Use the @KafkaListener annotation with a Spring expression.@KafkaListener注释与 Spring 表达式一起使用。 From the Spring Kafka reference :来自Spring Kafka 参考

You can configure most attributes on the annotation with SpEL by using #{…} or property placeholders (${…}).您可以使用 #{...} 或属性占位符 (${...}) 使用 SpEL 配置注释上的大多数属性。 See the Javadoc for more information.有关更多信息,请参阅Javadoc

@KafkaListener(topics = "hi", groupId = "#{T(java.util.UUID).randomUUID().toString()}") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM