In order to centralized manage avro schemas, you can implement SchemaRegistry
interface in rocketmq-serializer-avro
module, and use SchemaRegistry
registering and getting schemas.
Format | Serializer | Deserializer |
---|---|---|
Raw String | Y | Y |
JSON | Y | Y |
Avro Generic | Y | Y |
Avro Specified | Y | Y |
Some serializer performance research please refer to https://github.com/vongosling/jvm-serializer.
You can define your format just implements RocketMQSerializer
and RocketMQDeserializer
.
Messages
provides methods like newMessage
and getMessageBody
to map between user class and byte array.AvroUtils
provides methods newGenericRecord
and newSchema
to create avro records and schemas.
DefaultMQProducer producer = new DefaultMQProducer("producer-group-json"); producer.setNamesrvAddr("localhost:9876"); producer.start(); // creating serializer for message body serializing RocketMQSerializer serializer = new RocketMQJsonSerializer<User>(); for (int i = 0; i < 100; i++) { User user = new User(); user.setName("tom"); user.setAge(i); // creating message from user data. Message message = Messages.newMessage("topic-json", user, serializer); SendResult result = producer.send(message); System.out.print(result.getSendStatus() + " " + i + "\n"); Thread.sleep(1000); }
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-group-json"); consumer.setNamesrvAddr("localhost:9876"); consumer.subscribe("topic-json", "*"); // creating deserializer for message body deserializing RocketMQDeserializer deserializer = new RocketMQJsonDeserializer<>(User.class); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) { for (MessageExt messageExt : list) { // getting data from message. User user = Messages.getMessageBody(messageExt, deserializer); System.out.print(user.getName() + ":" + user.getAge() + "\n"); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start();
rocketmq-serializer-avro
is powered by Apache Avro, and rocketmq-serializer-json
is powered by fastjson.