![](/img/trans.png)
[英]How to configure the bindings of a function in Spring Cloud Stream to bind its input to a web endpoint and its output to a Kafka topic
[英]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.