简体   繁体   中英

Spring Cloud Stream Rabbit Binder serialization problem for Pojo

I have 2 applications running. One of them is Producer, other one is Consumer. I am using spring cloud stream rabbitmq binder for messaging infrastructure. I am using POJO classes to send messages between microservices. In my producer application, I am setting content-type configuration to application/json to send pojo classes. But consumer application is not able to consume this message. It seems somehow rabbitmq convert pojo classes to byte array although I defined content type in producer level.

This is producer configuration

   spring:
      autoconfigure:
        exclude: org.springframework.boot.actuate.autoconfigure.metrics.jdbc.DataSourcePoolMetricsAutoConfiguration
      application:
        name: simulator
      cloud:
        stream:
          function:
              definition: simulate1;simulate2
          rabbit:
            type: rabbit
            environment:
              spring:
                rabbitmq:
                  host: localhost
                  port: 5672
                  username: guest
                  password: guest
          kafka:
            binder:
              replicationFactor: 2
              auto-create-topics: true
              brokers: localhost:9092, localhost:9093, localhost:9094
              consumer-properties:
                key.deserializer: org.apache.kafka.common.serialization.StringDeserializer
                value.deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
                spring:
                  json:
                    trusted:
                      packages: '*'
              configuration:
                value.deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
                key.deserializer: org.springframework.kafka.support.serializer.ErrorHandlingDeserializer
          bindings:
            simulate1-in-0:
              destination: pf-topic
              content-type: application/json
              group: service
              binder: rabbit
              consumer:
                use-native-decoding: true
                concurrency: 5
            simulate2-in-0:
                  destination: pf-topic
                  content-type: application/json
                  group: service
                  binder: rabbit
                  consumer:
                    use-native-decoding: true
                    concurrency: 5

This is code for sending message

      Message message1 = MessageBuilder.withPayload(new PFSimulationEvent(1, "sd"))
            .setHeader("to_process", true)
            .build();
        output.send("simulatePf-out-0", message1);

If I want to read attributes of pojo classe in consumer application using following bean, I am getting exception.

    @Bean
    public Consumer<Message<PFSimulationEvent>> simulatePf() {
        return message -> {
            log.info("PF MEssage " + message.getPowerFlowId());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        };
    }

This is the log of message header

MESSAGE {amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=pf-topic,
     amqp_deliveryTag=2, deliveryAttempt=1, amqp_consumerQueue=pf-topic.simulation, amqp_redelivered=false, amqp_receivedRoutingKey=pf-topic, skip-input-type-conversion=true, amqp_timestam
    p=Wed Sep 07 14:04:30 TRT 2022, source-type=streamBridge, amqp_messageId=dce167f7-09f6-1d91-676c-67b491c3ad92, to_process=false, id=a5f5293e-0320-9b27-a4e6-b8d898c354a7, amqp_consumerT
    ag=amq.ctag-IY9eB8NSw8ZuwhAmBnO0xg, sourceData=(Body:'[serialized object]' MessageProperties [headers={to_process=false, target-protocol=streamBridge}, timestamp=Wed Sep 07 14:04:30 TR
    T 2022, messageId=dce167f7-09f6-1d91-676c-67b491c3ad92, **contentType=application/x-java-serialized-objec**t, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=fals
    e, receivedExchange=pf-topic, receivedRoutingKey=pf-topic, deliveryTag=2, consumerTag=amq.ctag-IY9eB8NSw8ZuwhAmBnO0xg, consumerQueue=pf-topic.simulation]), contentType=application/x-ja
    va-serialized-object, timestamp=1662548673773}

If I use Kafka in same approach, everything is working if I define serializers in producer and consumer levels. I could not find any configuration in rabbitmq binder like this

I find the problem.Native encoding attribute leads failing in type conversion. When I remove native encoding, I was able to transmit pojo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM