尝试使用具有功能(供应商)模型的 Spring 云流将对象发布到 Kafka

Trying to publish object to Kafka using Spring cloud stream with Functional(Supplier) model

我正在尝试使用具有功能模型的 spring 云流将对象发布到 Kafka 主题。这是我的要求的代码片段。

控制器:

@PostMapping(path = "/publish")
public void publish(@RequestBody SampleObject obj) {
    service.publish(obj);
    log.info("Published Data {} successfully", obj.toString());
}

服务Class:

@Bean
public Supplier<Object> publish(SampleObject obj) {
    return () -> {
        log.info("posting data to kafka topic {}", obj);
        return obj;
    };
}

我的要求是我需要将请求对象从控制器发送到服务并将该对象发布到 Kafka 主题。

注意:我不想使用已弃用的模型。

可以考虑使用StreamBridge

参考:https://docs.spring.io/spring-cloud-stream/docs/3.0.10.RELEASE/reference/html/spring-cloud-stream.html#_using_streambridge

@SpringBootApplication
@Controller
public class WebSourceApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebSourceApplication.class, "--spring.cloud.stream.source=toStream");
    }

    @Autowired
    private StreamBridge streamBridge;

    @RequestMapping
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void delegateToSupplier(@RequestBody String body) {
        System.out.println("Sending " + body);
        streamBridge.send("toStream-out-0", body);
    }
}