代码之家  ›  专栏  ›  技术社区  ›  Thiru

Kafka-在使用者中反序列化对象

  •  1
  • Thiru  · 技术社区  · 8 年前

    生产者将消息作为hashmap对象放入队列。我们有JSON序列化程序,我们假设映射将被序列化并放入队列中。这里是生产者配置。

    spring:
      kafka:
        bootstrap-servers: localhost:9092
        producer:
            key-serializer: org.springframework.kafka.support.serializer.JsonSerializer
            value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
    

    spring:
       kafka:
           consumer:
                group-id: xyz
                key-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
                value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
    

    我们的听众方法:

      public void listener(SomeClass abx)
    

    我们看了几篇文章,建议做如下的事情:

     @Bean
      public ConsumerFactory<String, Car> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
            new JsonDeserializer<>(Car.class));
      }
    

    2 回复  |  直到 8 年前
        1
  •  3
  •   Gary Russell    8 年前

    the boot documentation

    spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer

    spring.kafka.consumer.properties.spring.json.value.default.type=com.example.Invoice

    spring.kafka.consumer.properties.spring.json.trusted.packages=com.example,org.acme

        2
  •  -1
  •   Cristian Batista    8 年前

    你可以看看合流: https://www.confluent.io/

    import io.confluent.kafka.serializers.KafkaAvroDeserializerConfig;
    import org.apache.kafka.clients.producer.ProducerConfig;
    
        @Bean
        public Map<String, Object> producerConfigs() {
            Map<String, Object> props = new HashMap<>();
    
            props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
            props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, io.confluent.kafka.serializers.KafkaAvroSerializer.class);
    
            props.put(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistry);
            props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, io.confluent.kafka.serializers.KafkaAvroSerializer.class);
    
            (...)
    
            return props;
        }