spring云流:如何从rest控制器生成kafka消息?

kse8i1jr  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(359)

我用的是Spring Cloud溪。
如何从rest控制器路由处理程序方法生成kafka消息?

@RestController
public final class TransactionController {

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

}
2w2cym1i

2w2cym1i1#

您可以在控制器bean中@autowiredstreambridge并在@postmapping端点中使用它。
文件上说。。。streambridgebean允许我们将数据发送到输出绑定,从而有效地将非流应用程序与springcloudstream连接起来
在此处查看文档。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

@Autowired
private StreamBridge streamBridge  

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

相关问题