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

绑定属性

绑定属性通过使用格式为 spring.cloud.stream.bindings.<bindingName>.<property>=<value> 的方式提供。 <bindingName> 表示正在配置的绑定的名称。spring-doc.cadn.net.cn

例如,对于以下功能spring-doc.cadn.net.cn

@Bean
public Function<String, String> uppercase() {
	return v -> v.toUpperCase();
}

这两个绑定分别命名为uppercase-in-0,用于输入,以及uppercase-out-0,用于输出。有关更多信息,请参阅[绑定和绑定名称]spring-doc.cadn.net.cn

To avoid repetition, Spring Cloud Stream 支持为所有绑定设置值,格式为 spring.cloud.stream.default.<property>=<value>spring.cloud.stream.default.<producer|consumer>.<property>=<value> 用于常见的绑定属性。

当涉及到为长时间绑定属性避免重复时,应使用这种格式 - spring.cloud.stream.<binder-type>.default.<producer|consumer>.<property>=<value>spring-doc.cadn.net.cn

通用绑定属性

这些属性通过org.springframework.cloud.stream.config.BindingProperties公开spring-doc.cadn.net.cn

以下为输入和输出绑定都可用的绑定属性,必须以 spring.cloud.stream.bindings.<bindingName>. 前缀(例如,spring.cloud.stream.bindings.uppercase-in-0.destination=ticktock)。spring-doc.cadn.net.cn

默认值可以通过使用spring.cloud.stream.default前缀(例如spring.cloud.stream.default.contentType=application/json)来设置。spring-doc.cadn.net.cn

目的地

绑定到已绑定中间件的目标目的地(例如,RabbitMQ交换机或Kafka主题)。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

如果绑定表示使用者绑定(输入),则它可以绑定到多个目标,并且可以指定目标名称作为以逗号分隔的String值。spring-doc.cadn.net.cn

否则,将使用实际绑定名称。spring-doc.cadn.net.cn

此属性的默认值无法被覆盖。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

group

绑定的消费者组。 仅适用于入站绑定。 见消费者组spring-doc.cadn.net.cn

默认: null(表示匿名消费者)。spring-doc.cadn.net.cn

内容类型

此绑定的内容类型。请参阅 Content Type Negotiationspring-doc.cadn.net.cn

默认值: application/jsonspring-doc.cadn.net.cn

绑定器

用于此绑定的绑定程序。 请参阅Multiple Binders on the Classpath获取详细信息。spring-doc.cadn.net.cn

Default: null (the default binder is used, if it exists).spring-doc.cadn.net.cn

消费者属性

这些属性通过org.springframework.cloud.stream.binder.ConsumerProperties公开spring-doc.cadn.net.cn

以下绑定属性仅适用于输入绑定,并且必须以spring.cloud.stream.bindings.<bindingName>.consumer.为前缀(例如,spring.cloud.stream.bindings.input.consumer.concurrency=3)。spring-doc.cadn.net.cn

默认值可以通过使用spring.cloud.stream.default.consumer前缀(例如,spring.cloud.stream.default.consumer.headerMode=none)设置。spring-doc.cadn.net.cn

自动启动

触发此消费者自动启动信号spring-doc.cadn.net.cn

默认值: truespring-doc.cadn.net.cn

并发

inbound consumer 的并发量。spring-doc.cadn.net.cn

默认值: 1spring-doc.cadn.net.cn

分区化的

消费者是否从分区生产者接收数据。spring-doc.cadn.net.cn

默认值: falsespring-doc.cadn.net.cn

页面模式

设置为none时,禁用输入上的标题解析。仅对不原生支持消息标题且需要嵌入标题的消息中间件有效。当从非Spring Cloud Stream应用程序消费数据且不支持原生标题时,此选项很有用。设置为headers时,它使用中间件的原生标题机制。设置为embeddedHeaders时,它将标题嵌入到消息负载中。spring-doc.cadn.net.cn

默认值取决于绑定器实现。spring-doc.cadn.net.cn

maxAttempts

如果处理失败,重试处理消息的次数(包括第一次)。设置为1以禁用重试。spring-doc.cadn.net.cn

默认值: 3spring-doc.cadn.net.cn

重试初始间隔

重试时的退避初始间隔。spring-doc.cadn.net.cn

默认值: 1000spring-doc.cadn.net.cn

重试最大间隔

最大退避间隔。spring-doc.cadn.net.cn

默认值: 10000spring-doc.cadn.net.cn

退避乘数

退避乘数。spring-doc.cadn.net.cn

默认值: 2.0spring-doc.cadn.net.cn

默认可重试

监听器抛出且未列在retryableExceptions中的异常是否可重试。spring-doc.cadn.net.cn

默认值: truespring-doc.cadn.net.cn

实例数量

设置为大于等于零的值时,可以自定义此消费者的实例数量(如果不同于spring.cloud.stream.instanceCount)。
当设置为负值时,默认为spring.cloud.stream.instanceCount
有关更多信息,请参见Instance Index and Instance Countspring-doc.cadn.net.cn

默认值: -1spring-doc.cadn.net.cn

实例索引

当设置为大于等于零的值时,它允许自定义此消费者的实例索引(如果不同于spring.cloud.stream.instanceIndex)。当设置为负值时,默认为spring.cloud.stream.instanceIndex。如果提供了instanceIndexList,则会被忽略。Instance Index and Instance Count了解更多详细信息。spring-doc.cadn.net.cn

默认值: -1spring-doc.cadn.net.cn

实例索引列表

与不支持原生分区的绑定器一起使用(例如RabbitMQ);允许应用程序实例从多个分区中消费。spring-doc.cadn.net.cn

默认:空。spring-doc.cadn.net.cn

可重试异常

一个以 Throwable 类名作为键,布尔值作为值的映射。
指定那些将要或不会重试的异常(以及子类)。
另请参阅 defaultRetriable
示例: spring.cloud.stream.bindings.input.consumer.retryable-exceptions.java.lang.IllegalStateException=falsespring-doc.cadn.net.cn

默认:空。spring-doc.cadn.net.cn

使用原生解码

当设置为 true 时,入站消息将由客户端库直接反序列化,必须相应地配置客户端库(例如,设置适当的 Kafka 生产者值反序列化器)。
当使用此配置时,入站消息的反序列化不基于绑定的 contentType
当使用原生解码时,生产者的责任是使用适当的编码器(例如,Kafka 生产者值序列化器)来序列化出站消息。
另外,当使用原生编码和解码时,headerMode=embeddedHeaders 属性被忽略且标题未嵌入到消息中。
参见生产者属性 useNativeEncodingspring-doc.cadn.net.cn

默认值: falsespring-doc.cadn.net.cn

多路复用

设置为true时,底层绑定器将在同一输入绑定上原生多路复用目标。spring-doc.cadn.net.cn

默认值: falsespring-doc.cadn.net.cn

高级消费者配置

对于消息驱动消费者的基础消息监听容器的高级配置,请向应用上下文中添加单个ListenerContainerCustomizerbean。在上述属性被应用后,它将被调用,并可用于设置其他属性。同样地,对于轮询消费者,请添加一个MessageSourceCustomizerbean。spring-doc.cadn.net.cn

以下是RabbitMQ绑定器的示例:spring-doc.cadn.net.cn

@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer> containerCustomizer() {
    return (container, dest, group) -> container.setAdviceChain(advice1, advice2);
}

@Bean
public MessageSourceCustomizer<AmqpMessageSource> sourceCustomizer() {
    return (source, dest, group) -> source.setPropertiesConverter(customPropertiesConverter);
}

生产者属性

这些属性通过org.springframework.cloud.stream.binder.ProducerProperties公开spring-doc.cadn.net.cn

以下绑定属性仅适用于输出绑定,并且必须以前缀spring.cloud.stream.bindings.<bindingName>.producer.(例如,spring.cloud.stream.bindings.func-out-0.producer.partitionKeyExpression=headers.id)。spring-doc.cadn.net.cn

可以通过使用前缀 spring.cloud.stream.default.producer 来设置默认值(例如,spring.cloud.stream.default.producer.partitionKeyExpression=headers.id)。spring-doc.cadn.net.cn

自动启动

触发此消费者自动启动信号spring-doc.cadn.net.cn

默认值: truespring-doc.cadn.net.cn

分区键表达式

一个SpEL表达式,用于确定如何划分传出数据。
如果设置,则对此绑定上的传出数据进行分区。partitionCount必须设置为大于1的值才能生效。
请参阅Partitioningspring-doc.cadn.net.cn

默认值:null。spring-doc.cadn.net.cn

分区键提取器名称

实现 PartitionKeyExtractorStrategy 的 bean 的名称。用于提取用作计算分区 ID 的键(参见“partitionSelector*”)。与“partitionKeyExpression”互斥。spring-doc.cadn.net.cn

默认值:null。spring-doc.cadn.net.cn

分区选择器名称

实现 PartitionSelectorStrategy 的 Bean 名称。用于根据分区键确定分区 ID(参见 'partitionKeyExtractor*')。与 'partitionSelectorExpression' 相互排斥。spring-doc.cadn.net.cn

默认值:null。spring-doc.cadn.net.cn

分区选择器表达式

用于自定义分区选择的 SpEL 表达式。如果两者均未设置,则分区将选择为 hashCode(key) % partitionCount,其中 key 是通过 partitionKeyExpression 计算得出的。spring-doc.cadn.net.cn

默认值: nullspring-doc.cadn.net.cn

分区数量

启用分区时,数据的目标分区数。如果生产者已分区,则必须设置为大于1的值。在Kafka中,这被视为一个提示。此值与目标主题的分区数中的较大值将被使用。spring-doc.cadn.net.cn

默认值: 1spring-doc.cadn.net.cn

必需的组

一个逗号分隔的组列表,生产者必须确保即使它们在创建后启动,消息交付也会得到保障(例如,在RabbitMQ中预先创建持久队列)。spring-doc.cadn.net.cn

页面模式

设置为none时,它会在输出上禁用头嵌入。仅当消息中间件不支持原生消息头且需要头嵌入时才有效。当生产数据用于非Spring Cloud Stream应用程序且不支持原生头时,此选项很有用。
设置为headers时,它使用中间件的原生头机制。
设置为embeddedHeaders时,它将头嵌入到消息负载中。spring-doc.cadn.net.cn

默认值取决于绑定器实现。spring-doc.cadn.net.cn

使用原生编码

当设置为 true 时,出站消息由客户端库直接序列化,必须相应地进行配置(例如,设置适当的 Kafka 生产者值序列化程序)。 使用此配置时,出站消息的封送不是基于绑定的 contentType。 使用原生编码时,消费者有责任使用适当的解码器(例如,Kafka 消费者值反序列化程序)来反序列化入站消息。 另外,当使用原生编码和解码时,headerMode=embeddedHeaders 属性被忽略且不将标头嵌入消息中。 参见消费者属性 useNativeDecodingspring-doc.cadn.net.cn

默认值: falsespring-doc.cadn.net.cn

错误通道启用

当设置为 true 时,如果绑定器支持异步发送结果,则会将发送失败的消息发送到目的地的错误通道。有关更多信息,请参阅错误处理。spring-doc.cadn.net.cn

默认值:false。spring-doc.cadn.net.cn

高级生产者配置

在某些情况下,仅使用生产者属性不足以正确配置绑定器中的消息处理器(MessageHandler),或者您可能更倾向于采用编程方式来配置此类生产消息处理器。无论出于何种原因,Spring Cloud Stream 提供了 ProducerMessageHandlerCustomizer 来实现这一目标。spring-doc.cadn.net.cn

@FunctionalInterface
public interface ProducerMessageHandlerCustomizer<H extends MessageHandler> {

	/**
	 * Configure a {@link MessageHandler} that is being created by the binder for the
	 * provided destination name.
	 * @param handler the {@link MessageHandler} from the binder.
	 * @param destinationName the bound destination name.
	 */
	void configure(H handler, String destinationName);

}

正如您所见,它为您提供对实际生产实例MessageHandler的访问权限,您可以根据需要进行配置。
您所需要做的就是提供此策略的实现,并将其配置为@Beanspring-doc.cadn.net.cn