简体   繁体   English

Spring 云 Stream:如何从 REST Z594C103F2C6E04E0C3DAZ8 生成 Kafka 消息?

[英]Spring Cloud Stream: how can I produce a Kafka message from a REST controller?

I am using Spring Cloud Stream.我正在使用 Spring 云 Stream。

How can I produce a Kafka message from a REST controller route handler method?如何从 REST controller 路由处理程序方法生成 Kafka 消息?

@RestController
public final class TransactionController {

    @PostMapping("/transactions")
    public void recordTransaction(final RecordTransaction recordTransaction) {
        // I want to produce a TransactionRecorded event through Kafka here
    }

}

You can @Autowired StreamBridge in your Controller Bean and use it in the @PostMapping endpoint.您可以在 Controller Bean 中@Autowired StreamBridge并在 @PostMapping 端点中使用它。

As documentation says... StreamBridge bean allows us to send data to an output binding effectively bridging non-stream application with spring-cloud-stream正如文档所说... StreamBridge bean 允许我们将数据发送到 output 绑定,有效地将非流应用程序与 spring-cloud-stream 桥接

Check documentation here.在此处查看文档。 https://docs.spring.io/spring-cloud-stream/docs/3.1.0/reference/html/spring-cloud-stream.html#_sending_arbitrary_data_to_an_output_e_g_foreign_event_driven_sources https://docs.spring.io/spring-cloud-stream/docs/3.1.0/reference/html/spring-cloud-stream.html#_sending_arbitrary_data_to_an_output_e_g_foreign_event_driven_source

@Autowired
private StreamBridge streamBridge  

@PostMapping("/transactions")
public void recordTransaction(final RecordTransaction recordTransaction) {
    streamBridge.send("record_transaction-out-0", recordTransaction);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring-cloud-stream无法产生kafka消息 - Spring-cloud-stream cannot produce kafka message 默认情况下如何使用Kafka Spring Cloud Stream并使用汇合API生成的Kafka消息? - How to consume from Kafka Spring Cloud Stream by default and also consume a Kafka message generated by the confluent API? Spring Cloud Stream确定主题Kafka消息来自 - Spring Cloud Stream determine topic Kafka message came from 如何使用 Spring Cloud Stream 和 Kafka Streams Binder 暂停(打开/关闭)stream 处理? - How can I pause (turn on/off) stream processing w/ Spring Cloud Stream & Kafka Streams Binder? 如何使用 Spring Cloud Stream 只生成一条消息而没有弃用 @Output,或者关闭轮询? - How do I produce just one message using Spring Cloud Stream w/o deprecated @Output, or turn off polling? 如何使用 Spring Cloud Kafka Stream 3.1 创建生产者 - How can create a producer using Spring Cloud Kafka Stream 3.1 当没有 kafka 代理运行时,如何出于开发目的禁用 Spring Cloud 流? - How can I disable spring cloud stream for development purpose when there are not kafka broker running? 如何从springcloud流kafka中的ErrorMessage的有效载荷中获取failedMessage? - How to get failedMessage from the payload in the ErrorMessage in spring cloud stream kafka? Spring Cloud Stream Kafka>使用Confluent REST Proxy消费Avro消息 - Spring Cloud Stream Kafka > consuming Avro messages from Confluent REST Proxy 我如何自定义Spring Rest控制器的json输出 - How can I customize json output from spring rest controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM