此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 spring-cloud-stream 4.3.0! |
在多粘合剂应用中定制粘合剂
当应用程序中有多个绑定器并想要自定义绑定器时,可以通过提供BinderCustomizer
实现。
对于具有单个 Binder 的应用程序,不需要此特殊定制器,因为 Binder 上下文可以直接访问自定义 Bean。
但是,在多绑定器方案中并非如此,因为不同的绑定器位于不同的应用程序上下文中。
通过提供BinderCustomizer
接口,绑定器虽然驻留在不同的应用程序上下文中,但将接收自定义。
Spring Cloud Stream 确保在应用程序开始使用绑定器之前进行自定义。
用户必须检查活页夹类型,然后应用必要的自定义项。
下面是一个提供BinderCustomizer
豆。
@Bean
public BinderCustomizer binderCustomizer() {
return (binder, binderName) -> {
if (binder instanceof KafkaMessageChannelBinder kafkaMessageChannelBinder) {
kafkaMessageChannelBinder.setRebalanceListener(...);
}
else if (binder instanceof KStreamBinder) {
...
}
else if (binder instanceof RabbitMessageChannelBinder) {
...
}
};
}
请注意,当有多个相同类型的活页夹实例时,活页夹名称可用于过滤自定义。