https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream
ZenWave Spring-Modulith events externalizer for Spring Cloud Stream
https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream
Last synced: 5 months ago
JSON representation
ZenWave Spring-Modulith events externalizer for Spring Cloud Stream
- Host: GitHub
- URL: https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream
- Owner: ZenWave360
- License: mit
- Created: 2024-12-10T19:04:55.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-10T19:41:02.000Z (about 1 year ago)
- Last Synced: 2024-12-10T20:33:48.030Z (about 1 year ago)
- Language: Java
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring-Modulith Events Externalizer for Spring Cloud Stream
[](https://search.maven.org/artifact/io.zenwave360.sdk/spring-modulith-events-scs)
[](https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream/actions/workflows/publish-maven-snapshots.yml)
[](https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream/actions/workflows/build.yml)
[](https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream/actions/workflows/build.yml)
[](https://github.com/ZenWave360/spring-modulith-events-spring-cloud-stream/blob/main/LICENSE)
Spring-Modulith Events Externalizer that uses Spring Cloud Stream supporting both JSON and Avro serialization formats.
Check out the blog post here: https://www.zenwave360.io/posts/Spring-Modulith-Events-Spring-Cloud-Stream-Externalizer/
## Getting Started
### Dependency
Add the following Maven dependency to your project:
```xml
io.zenwave360.sdk
spring-modulith-events-scs
${spring-modulith-events-scs.version}
```
### Configuration
Use `@EnableSpringCloudStreamEventExternalization` annotation to enable Spring Cloud Stream event externalization in your Spring configuration:
```java
@Configuration
@EnableSpringCloudStreamEventExternalization
public class SpringCloudStreamEventsConfig {
// Additional configurations (if needed)
}
```
This configuration ensures that, in addition to events annotated with `@Externalized`, all events of type `org.springframework.messaging.Message` with a header named `SpringCloudStreamEventExternalizer.SPRING_CLOUD_STREAM_EVENT_HEADER` will be externalized and routed to their specified destination using the value of this header as the routing target.
---
## Event Serialization
Using the transactional event publication log requires serializing events to a format that can be stored in a database. Since the generic type of `Message>` payload is lost when using the default `JacksonEventSerializer`, this library adds an extra `_class` field to preserve payload type information, allowing for complete deserialization to its original type.
This library provides support for POJO (JSON) and Avro serialization formats for `Message>` payloads.
### Avro Serialization
Avro serialization needs `com.fasterxml.jackson.dataformat.avro.AvroMapper` class present in the classpath. In order to use Avro serialization, you need to add the following dependency to your project:
```xml
com.fasterxml.jackson.dataformat
jackson-dataformat-avro
```
---
## Routing Events
### Programmatic Routing for `Message`> events
You can define routing targets programmatically using a Message header:
```java
public class CustomerEventsProducer implements ICustomerEventsProducer {
private final ApplicationEventPublisher applicationEventPublisher;
public void onCustomerCreated(CustomerCreated event) {
Message message = MessageBuilder.withPayload(event)
.setHeader(
SpringCloudStreamEventExternalizer.SPRING_CLOUD_STREAM_SENDTO_DESTINATION_HEADER,
"customer-created") // <- target binding name
.build();
applicationEventPublisher.publishEvent(message);
}
}
```
### Annotation-Based Routing for POJO Events
Leverage the `@Externalized` annotation to define the target binding name and routing key:
```java
@Externalized("customer-created::#{#this.getLastname()}")
class CustomerCreated {
public String getLastname() {
// Return the customer's last name
}
}
```
### Configure Spring Cloud Stream destination
Configure Spring Cloud Stream destination for your bindings as usual in `application.yml`:
```yaml
spring:
cloud:
stream:
bindings:
customer-created:
destination: customer-created-topic
```
### Routing Key
`SpringCloudStreamEventExternalizer` dynamically sets the appropriate Message header (e.g., `kafka_messageKey` or `rabbit_routingKey`) from your routing key based on the channel binder type, if the routing header is not already present.
- KafkaMessageChannelBinder: `kafka_messageKey`
- RabbitMessageChannelBinder: `rabbit_routingKey`
- KinesisMessageChannelBinder: `partitionKey`
- PubSubMessageChannelBinder: `pubsub_orderingKey`
- EventHubsMessageChannelBinder: `partitionKey`
- SolaceMessageChannelBinder: `solace_messageKey`
- PulsarMessageChannelBinder: `pulsar_key`
---
## Using Snapshot Versions
In order to test snapshot versions of this library, add the following repository to your Maven configuration:
```xml
maven-snapshots
https://central.sonatype.com/repository/maven-snapshots
true
```