【问题标题】:Convert Spring Cloud Stream to use reactive cloud function将 Spring Cloud Stream 转换为使用响应式云功能
【发布时间】:2020-04-29 17:25:29
【问题描述】:

目前我有类似这样的 Spring Boot 应用程序。

@Component
@EnableBinding(Source::class)
class EmailMessageProducer(private val source: Source) {

    suspend fun send(textMessage: TextMessage) {

        source.output().send(
            MessageBuilder.withPayload(textMessage).setHeader("service", "test").build()
        )
    }
}

我想在这里使用反应模式使用 Spring Cloud Function。

此外,我当前的解决方案是非阻塞的吗?我问这个是因为这是我第一次在这种情况下使用 Kotlin 协程。

Java 解决方案也适用于我,因为我只是想理解这里的概念。

【问题讨论】:

    标签: spring-boot spring-cloud-stream kotlin-coroutines spring-cloud-function spring-reactive


    【解决方案1】:

    您正在寻找的是响应式供应商(例如,Supplier<Flux>)。

    在你的情况下,它看起来像这样:

    @SpringBootApplication
    public class SomeApplication {
    
        @Bean
        public Supplier<Flux<Message<String>>> messageProducer() {
            return () -> Flux.just(MessageBuilder.withPayload(textMessage).setHeader("service", "test").build());
        }
    }
    

    提供spring.cloud.function.definition=messageProducer 属性,就差不多了。

    显然,上面的示例生成了一个包含单个项目的有限流,但可以随意修改返回的通量。事实上,我们会更详细地讨论这个问题here

    【讨论】:

    • 我如何在这里传递textMessage
    • 通过?传到哪里?这是一个供应商 - 消息的生产者。 . .
    • 我明白你的意思,但在我的情况下,我正在根据上面示例中给出的参数生成消息。
    • 您可以使用EmitterProcessor。详情请参考this section
    • 我这样做了private val processor: EmitterProcessor&lt;TextMessage&gt; = EmitterProcessor.create() @Bean fun supplier(): Supplier&lt;Flux&lt;EmailMessage&gt;&gt; { return Supplier { processor } } 和后来的processor.onNext(textMessageBuilder.build()) 但是我在 RabbitMQ 中没有看到任何消息我需要在 application.yml 中添加任何内容
    猜你喜欢
    • 2020-10-24
    • 2022-07-26
    • 1970-01-01
    • 2017-04-13
    • 2020-10-23
    • 2018-11-17
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多