繁体   English   中英

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

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

我正在使用 Spring 云 Stream。

如何从 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
    }

}

您可以在 Controller Bean 中@Autowired StreamBridge并在 @PostMapping 端点中使用它。

正如文档所说... StreamBridge bean 允许我们将数据发送到 output 绑定,有效地将非流应用程序与 spring-cloud-stream 桥接

在此处查看文档。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM