对于最新稳定版本,请使用spring-cloud-stream 5.0.1spring-doc.cadn.net.cn

KafkaBindingRebalanceListener

应用程序可能希望在初始分配分区时寻找任意偏移量的主题/分区,或者对使用者执行其他操作。 从版本 2.1 开始,如果在应用程序上下文中提供一个单例 KafkaBindingRebalanceListener bean,则将其注入所有 Kafka 消费器绑定中。spring-doc.cadn.net.cn

public interface KafkaBindingRebalanceListener {

	/**
	 * Invoked by the container before any pending offsets are committed.
	 * @param bindingName the name of the binding.
	 * @param consumer the consumer.
	 * @param partitions the partitions.
	 */
	default void onPartitionsRevokedBeforeCommit(String bindingName, Consumer<?, ?> consumer,
			Collection<TopicPartition> partitions) {

	}

	/**
	 * Invoked by the container after any pending offsets are committed.
	 * @param bindingName the name of the binding.
	 * @param consumer the consumer.
	 * @param partitions the partitions.
	 */
	default void onPartitionsRevokedAfterCommit(String bindingName, Consumer<?, ?> consumer, Collection<TopicPartition> partitions) {

	}

	/**
	 * Invoked when partitions are initially assigned or after a rebalance.
	 * Applications might only want to perform seek operations on an initial assignment.
	 * @param bindingName the name of the binding.
	 * @param consumer the consumer.
	 * @param partitions the partitions.
	 * @param initial true if this is the initial assignment.
	 */
	default void onPartitionsAssigned(String bindingName, Consumer<?, ?> consumer, Collection<TopicPartition> partitions,
			boolean initial) {

	}

}

不能将 resetOffsets 消费器属性设置为 true,同时提供平衡侦听器。spring-doc.cadn.net.cn