[英]Kinesis as producer in Spring Boot Reactive Stream API
I'm trying to build a small Spring Boot Reactive API. 我正在尝试构建一个小的Spring Boot Reactive API。 The API should let the users subscribe to some data, returned as SSE. API应该让用户订阅一些数据,以SSE形式返回。
The data is located on a Kinesis Topic. 数据位于Kinesis主题上。
Creating the Reactive API, and the StreamListener to Kinesis is fairly easy - but can I combine these, so the Kinesis Topic are used as a producer for the event stream used by my data service. 创建Reactive API和Kinesis的StreamListener相当简单-但我可以将它们组合在一起,因此Kinesis Topic用作数据服务使用的事件流的生产者。
The code looks more or less like this 该代码看起来或多或少像这样
//Kinesis binding, with listenerMode: rawRecords
@EnableBinding(Sink.class)
public class KinesisStreamListener {
@StreamListener(value = Sink.INPUT)
public void logger(List<Record> payload) throws Exception {
}
}
@RestController
@RequestMapping("/data")
public class DataResource {
@Autowired
DataService service;
@GetMapping(produces = {MediaType.TEXT_EVENT_STREAM_VALUE, MediaType.APPLICATION_STREAM_JSON_VALUE})
public Flux<EventObject> getData() {
return service.getData();
}
}
@Component
public class DataService {
Flux<EventObject> getData() {
Flux<Long> interval = Flux.interval(Duration.ofMillis(1000));
Flux<EventObject> dataFlux = Flux.fromStream(Stream.generate(() -> ???
));
return dataFlux.zip(interval, dataFlux).map(Tuple2::getT2);
}
}
Here is a sample how I would do that: https://github.com/artembilan/sandbox/tree/master/cloud-stream-kinesis-to-webflux . 这是我如何执行此操作的示例: https : //github.com/artembilan/sandbox/tree/master/cloud-stream-kinesis-to-webflux 。
Once we agree about details and some improvements it can go to the official Spring Cloud Stream Samples repository: https://github.com/spring-cloud/spring-cloud-stream-samples 一旦我们就细节和一些改进达成共识,可以转到官方的Spring Cloud Stream Samples存储库: https : //github.com/spring-cloud/spring-cloud-stream-samples
The main idea is to reuse the same Flux
provided by the @StreamListener
via Spring Cloud Stream Reactive Support. 主要思想是通过Spring Cloud Stream Reactive Support重用@StreamListener
提供的相同Flux
。 This is is already a FluxPublish
, so any new SSE connections will work as a plain Reactive subscribers. 这已经是FluxPublish
,因此任何新的SSE连接都将作为普通的Reactive订户使用。
There are a couple tricks to count with: 有几个技巧可以计数:
listenerMode: rawRecords
, we also need to configure a contentType: application/octet-stream
to avoid any conversion attempts when Binder sends a message to the Sink.INPUT
channel. 对于listenerMode: rawRecords
,我们还需要配置contentType: application/octet-stream
以避免在Binder将消息发送到Sink.INPUT
通道时进行任何转换尝试。 listenerMode: rawRecords
returns a List<Record>
our Flux
in the @StreamListener
method should expect exactly this type, but not a plain Record
. 由于listenerMode: rawRecords
返回一个List<Record>
我们的Flux
在@StreamListener
方法应该想到正是这种类型,而不是一个简单的Record
。 Both concerns are considered as a Framework improvements. 这两个问题都被视为对框架的改进。
So, let us now how it looks and works for you. 因此,现在让我们看看它的外观和工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.