繁体   English   中英

Spring 云 Stream Function:调用 Z86408593C34AF77FDD160DF932F8B52<t,r> 通过 REST 调用和 output 它到一个 KAFKA 主题</t,r>

[英]Spring Cloud Stream Function : Invoke Function<T,R> via REST call and output it to a KAFKA Topic

我有简单的@Bean (Java 8 函数),它们映射到目标topic-out-in )。

@Bean
public Function<String, String> transform() {
    return payload -> payload.toUpperCase();
}

@Bean
public Consumer<String> receive() {
    return payload -> logger.info("Data received: " + payload);
}

.yml 配置:

spring:
  cloud:
    stream:
      function:
        definition: transform;receive
      bindings:
        transform-out-0:
          destination: myTopic
        receive-in-0:
          destination: myTopic

Now, I want to invoke the transform function via a REST call so that it's output goes to the destination topic (ie transform-out-0 mapped to myTopic ) and is picked up by the consumer from this destination ( receive-in-0 mapped到myTopic )。 基本上,每个 REST 调用都应该生成一个KAFKA Producer的新实例并关闭它。

请问如何使用spring-cloud-stream来实现这一点?

谢谢

昂舒曼

您应该使用StreamBridge而不是transform function。 这是 Spring Cloud Stream 中动态目的地的新推荐方法。 这是基本思想:

@Autowired
private StreamBridge streamBridge;

@RequestMapping
public void delegateToSupplier(@RequestBody String body) {
    streamBridge.send("transform-out-0", body);
}

然后通过配置提供这个属性 - spring.cloud.stream.source: transform

Spring Cloud Stream 将为您创建一个名为transform-out-0的 output 绑定。 每次调用 REST 端点时,您都会通过StreamBridge将数据发送到目标主题。

有关更多信息,请参阅

暂无
暂无

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

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