如何配置Spring Cloud StreamBridge来生产Avro?

时间:2020-06-29 15:02:27

标签: spring dynamic spring-cloud spring-cloud-stream confluent-platform

因此,我尝试使用StreamBridge将消息动态发送到不同的主题。如果我的输出是 Message ,而不是 Message

,则我可以成功完成此操作

代码示例:

{
    amount = 1;
    weight = 70;
    item = {
       id = 12;
       name = "QWE"
    };
}

我得到的错误是@StreamListener(Sink.INPUT) public void process(@Payload GenericRecord messageValue, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) GenericRecord messageKey, @Header("Type") String type) { log.info("Processing Event --> " + messageValue); // Code... // convert to Message<GenericRecord> Message<GenericRecord> message = ... streamBridge.send(type, message); log.info("Processed Event --> " + messageValue); } ,我猜是因为streamBridge acceptedOutputTypes = application / json

Caused by: org.springframework.messaging.converter.MessageConversionException: Could not write JSON: Not a map:

我尝试通过在属性中设置以下内容来将可接受的输出类型修改为avro,这是行不通的。

2020-06-28 04:42:55.670  INFO 54347 --- [container-0-C-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Looking up function 'streamBridge' with acceptedOutputTypes: [application/json]

关于如何将StreamBridge配置为avro的任何想法?

编辑:我也尝试过spring.cloud.stream.function.definition=streamBridge spring.kafka.producer.key-serializer=io.confluent.kafka.serializers.KafkaAvroSerializer spring.kafka.producer.value-serializer=io.confluent.kafka.serializers.KafkaAvroSerializer spring.cloud.stream.bindings.streamBridge-out-0.content-type=application/*+avro spring.cloud.stream.bindings.streamBridge-out-0.producer.use-native-encoding=true ,但这也有转换错误。

2 个答案:

答案 0 :(得分:1)

我无法让StreamBridge动态工作,所以我改用Function:

@Bean
public Function<Message<GenericRecord>, Message<GenericRecord>> process() {
    return message -> {

        // Code...

        String topic = message.getHeaders().get("type");

        // convert to Message<GenericRecord>
        Message<GenericRecord> message = MessageBuilder...
            .setHeader("spring.cloud.stream.sendto.destination", topic)
            .build();
        

        return outgoingMessage;
    };
}

属性文件为:

spring.cloud.function.definition=process
spring.cloud.stream.bindings.process-in-0.destination=${consumer_topic}
spring.cloud.stream.bindings.process-in-0.group=${spring.application.name}

spring.cloud.stream.bindings.process-out-0.content-type=application/*+avro
spring.cloud.stream.bindings.process-out-0.producer.use-native-encoding=true

编辑:Streambridge已修复为支持此功能:https://github.com/spring-cloud/spring-cloud-stream/issues/2007

答案 1 :(得分:0)

您需要使用useNativeEncoding生产者属性才能使用自定义序列化程序。