简体   繁体   English

Kinesis作为Spring Boot反应式API的生产者

[英]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: 有几个技巧可以计数:

  1. For the 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通道时进行任何转换尝试。
  2. Since 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.

相关问题 Spring Boot Webflux 反应式 API - Spring Boot Webflux reactive api 无法使用spring cloud kinesis stream启动spring boot - Unable to start spring boot with spring cloud kinesis stream 如何在 Spring Boot RSocket Reactive 中处理入站 stream 取消 - How to handle inbound stream cancellation in Spring Boot RSocket Reactive 如何在不通过 Kinesis Data 的情况下直接将 KPL(Kinesis Producer Library)集成到 Kinesis firehose Stream - How to integrate KPL (Kinesis Producer Library) to Kinesis firehose directly without going through Kinesis Data Stream Spring Boot 2反应式webflux - Spring Boot 2 reactive webflux Spring 启动反应式缓存 - Spring boot Reactive caching Spring Boot 2. 异步 API。 CompletableFuture 与 Reactive - Spring Boot 2. Async API. CompletableFuture vs. Reactive 弹簧启动和弹簧集成aws运动学错误 - spring boot and spring integration aws kinesis error 响应式 Spring Boot API 包装了 Elasticsearch 的异步批量索引 - Reactive Spring Boot API wrapping Elasticsearch's async bulk indexing Spring 云总线与 AWS Kinesis stream @refreshscope - Spring cloud bus with AWS Kinesis stream @refreshscope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM