此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4! |
死信主题分区选择
默认情况下,使用与原始记录相同的分区将记录发布到 Dead-Letter 主题。 这意味着 Dead-Letter 主题必须至少具有与原始记录一样多的分区。
要更改此行为,请添加DlqPartitionFunction
implementation 作为@Bean
添加到应用程序上下文中。
只能存在一个这样的 bean。
该函数随 consumer 组一起提供,失败的ConsumerRecord
和例外。
例如,如果您始终希望路由到分区 0,则可以使用:
@Bean
public DlqPartitionFunction partitionFunction() {
return (group, record, ex) -> 0;
}
如果将使用者绑定的dlqPartitions 属性设置为 1(并且 Binder 的minPartitionCount 等于1 ),则无需提供DlqPartitionFunction ;框架将始终使用分区 0。
如果将使用者绑定的dlqPartitions property 的值设置为1 (或 Binder 的minPartitionCount 大于1 ),您必须提供DlqPartitionFunction bean,即使分区计数与原始主题的分区计数相同。 |
还可以为 DLQ 主题定义自定义名称。
为此,请创建一个DlqDestinationResolver
作为@Bean
添加到应用程序上下文中。
当 Binder 检测到这样的 bean 时,它优先,否则它将使用dlqName
财产。
如果两者都未找到,它将默认为error.<destination>.<group>
.
下面是一个DlqDestinationResolver
作为@Bean
.
@Bean
public DlqDestinationResolver dlqDestinationResolver() {
return (rec, ex) -> {
if (rec.topic().equals("word1")) {
return "topic1-dlq";
}
else {
return "topic2-dlq";
}
};
}
在 为DlqDestinationResolver
是 Binder 中的 Provisioner 不会自动为应用程序创建主题。
这是因为 Binders 无法推断实现可能发送到的所有 DLQ 主题的名称。
因此,如果您使用此策略提供 DLQ 名称,则应用程序有责任确保事先创建这些主题。