|
| 1 | +# Spring - How to version message payload Schemas using Spring Cloud Schema Registry |
| 2 | +As your application grows over time the format of the data that needs to be sent in the SQS messages may change as well. To allow for |
| 3 | +these changes, the [Spring Cloud Schema Registry](https://cloud.spring.io/spring-cloud-static/spring-cloud-schema-registry/1.0.0.RC1/reference/html/spring-cloud-schema-registry.html) |
| 4 | +can be used to track the version of your schemas, allowing the SQS consumer to be able to interpret multiple versions of your payload. |
| 5 | + |
| 6 | +## Full reference |
| 7 | +For a full working solution of this feature, take a look at the [Spring Cloud Schema Registry Example](../../../examples/spring-cloud-schema-registry-example). |
| 8 | + |
| 9 | +## Steps to consume messages serialized using Apache Avro |
| 10 | +1. Include the `Spring Cloud Schema Registry Extension` dependency |
| 11 | + ```xml |
| 12 | + <dependency> |
| 13 | + <groupId>com.jashmore</groupId> |
| 14 | + <artifactId>avro-spring-cloud-schema-registry-extension</artifactId> |
| 15 | + <version>${project.version}</version> |
| 16 | + </dependency> |
| 17 | + ``` |
| 18 | +1. Define your schemas and map this in your spring `application.yml` |
| 19 | + ```yml |
| 20 | + spring: |
| 21 | + cloud: |
| 22 | + schema-registry-client: |
| 23 | + endpoint: http://localhost:8990 |
| 24 | + schema: |
| 25 | + avro: |
| 26 | + schema-imports: |
| 27 | + - classpath:avro/author.avsc |
| 28 | + schema-locations: |
| 29 | + - classpath:avro/book.avsc |
| 30 | + ``` |
| 31 | + In this example above we have a book schema which is dependent on the author schema. We have also hardcoded the Schema Registry |
| 32 | + to be at [http://localhost:8990](http://localhost:8990). |
| 33 | +1. Create your schemas and place them in your `resources` directory. For example this is an example schema for the Book. |
| 34 | + ```json |
| 35 | + { |
| 36 | + "namespace" : "com.jashmore.sqs.extensions.registry.model", |
| 37 | + "type" : "record", |
| 38 | + "name" : "Book", |
| 39 | + "fields" : [ |
| 40 | + { "name":"id","type":"string" }, |
| 41 | + { "name":"name","type":"string" }, |
| 42 | + { "name":"author","type":"Author" } |
| 43 | + ] |
| 44 | + } |
| 45 | + ``` |
| 46 | +1. Enable the extension by annotating the Spring Application |
| 47 | + ```java |
| 48 | + @EnableSchemaRegistrySqsExtension |
| 49 | + @SpringBootApplication |
| 50 | + class Application { |
| 51 | + // normal code |
| 52 | + } |
| 53 | + ``` |
| 54 | +1. Define your queue listener using the `@SpringCloudSchemaRegistryPayload` to represent the payload that needs to be deserialized from |
| 55 | +the message payload. |
| 56 | + ```java |
| 57 | + @QueueListener(value = "queueName") |
| 58 | + public void listen(@SpringCloudSchemaRegistryPayload Book payload) { |
| 59 | + log.info("Payload: {}", payload); |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | +## Steps to produce messages using Avro |
| 64 | +You can wrap your `SqsAsyncClient` with the |
| 65 | +[AvroSchemaRegistrySqsAsyncClient](../../../util/proxy-method-interceptor/src/main/java/com/jashmore/sqs/registry/AvroSchemaRegistrySqsAsyncClient.java) |
| 66 | +to be able to more easily send a message that will be serialized using the Avro Schema. This Avro SQS Client was built for testing purposes and therefore it is |
| 67 | +recommended to developer your own logic for sending these messages. |
| 68 | + |
| 69 | +For a full example of building this client, take a look at the |
| 70 | +[Producer Example](../../../examples/spring-cloud-schema-registry-example/spring-cloud-schema-registry-producer). |
0 commit comments