【问题标题】:Spring boot Kafka Camel Avro consumerSpring Boot Kafka Camel Avro 消费者
【发布时间】:2016-12-09 13:36:00
【问题描述】:

我一直在尝试寻找 Spring Boot Kafka Camel Avro 消费者的示例代码,但没有成功。我在以下网址找到了 Spring Camel Kafka 消费者和生产者示例:

https://thysmichels.com/2015/09/04/apache-camel-kafka-spring-integration/

但缺少的是 Avro 部分。我在这里查看 avro 的 Camel 文档:

http://camel.apache.org/avro.html

我的具体问题是,一旦我的 bean 是从 Avro 模式创建的并且我有 POJO 类,我如何将上面的 spring 示例中的骆驼告诉用户 Avro 序列化?具体来说,我指的是这行代码: from("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);

其中的序列化器是 StringEncoder。如何告诉 Camel 使用 Avro 序列化?

【问题讨论】:

  • serializerClass=... 不会是你放的地方吗?例如&serializerClass=avro.serializer.StringEncoder

标签: spring-boot apache-camel apache-kafka avro


【解决方案1】:

我找到了自己的答案。因此,我想与您分享。它实际上是serializerClass=org.springframework.integration.kafka.serializer.avro.AvroSerializer。 代码非常简单,您几乎可以自己编写代码。

    public class AvroSerializer<T> {

        public T deserialize(final byte[] bytes, final DatumReader<T> reader) throws IOException {
            final Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null);
            return reader.read(null, decoder);
        }

        public byte[] serialize(final T input, final DatumWriter<T> writer) throws IOException {
            final ByteArrayOutputStream stream = new ByteArrayOutputStream();

            final Encoder encoder = EncoderFactory.get().binaryEncoder(stream, null);
            writer.write(input, encoder);
            encoder.flush();

            return stream.toByteArray();
        }
    }

【讨论】:

  • 你的 camel-kafka-avro 配置路线是什么?这是我的部分路线,它导致类未找到异常` +"&valueDeserializer="+KafkaAvroDeserializer.class +"&keyDeserializer="+StringDeserializer.class`
猜你喜欢
  • 1970-01-01
  • 2018-02-18
  • 2019-08-25
  • 2022-10-05
  • 2020-02-27
  • 1970-01-01
  • 2023-04-11
  • 2017-10-19
  • 1970-01-01
相关资源
最近更新 更多