From 6ccc45b7abbfe118fb4205cf1c847c3a68dfb710 Mon Sep 17 00:00:00 2001 From: chanhyeong-cho Date: Fri, 16 Aug 2024 17:01:02 +0900 Subject: [PATCH] Add publish/consume guides into README --- README.md | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dae187d..08be970 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,130 @@ Event Driven lightweight message format -[Document](https://github.com/naver/eventeria/wiki) +## Version compatibility + + eventeria | spring-boot | spring-cloud-stream +-----------|---------------|--------------------- + 1.2.1 | 3.2.x | 4.1.x + 1.1.1 | 3.0.x ~ 3.1.x | 4.0.x + 1.0.1 | 2.7.x | 3.2.x + +## Getting Started + +- [Document](https://github.com/naver/eventeria/wiki) +- [spring-boot-eventeria guide projects](https://github.com/naver/eventeria/tree/main/guide-projects/spring-boot-eventeria-guide) + +```gradle +dependencies { + implementation("com.navercorp.eventeria:spring-boot-eventeria:${version}") +} +``` + +```java +@Configuration +public class MessageConfig { + @Bean + ChannelBindable channelBindable() { + return new ChannelBindable(); + } + + @Bean + @ConditionalOnMissingBean + ChannelBinder channelBinder( + SubscribableChannelBindingTargetFactory bindingTargetFactory, + ChannelBindable channelBindable + ) { + return new DefaultChannelBinder(bindingTargetFactory, channelBindable); + } +} +``` + +## Produce `Message`s + +1. Register your spring-cloud-stream produce channel in application.yml or application.properties + ```yml + spring: + cloud: + stream: + bindings: + outbound-kafka-channel-command: + destination: command_topic + producer: + partition-count: 2 + ``` +2. Register beans of outbound `MessageChannel`, `SpringMessagePublisher`, `IntegrationFlow`. + - [example code](https://github.com/naver/eventeria/blob/main/guide-projects/spring-boot-eventeria-guide/src/main/java/com/navercorp/eventeria/guide/boot/publisher/ProgrammaticBindingNotifyCommandPublisher.java) +3. Inject `SpringMessagePublisher` bean and publish messages. + +```java +@Service +public class ExampleService { + ExampleService( + @Qualified("springMessagePublisherBean") SpringMessagePublisher springMessagePublisher + ) { + this.springMessagePublisher = springMessagePublisher; + } + + public void publish(Message message) { + springMessagePublisher.publish(message); + } +} +``` + +> **NOTE:** Does not support Functional publishing currently. + +## Consume `Message`s + +1. First, register your spring-cloud-stream consume channel in application.yml or application.properties + ```yml + spring: + cloud: + stream: + bindings: + inbound-kafka-channel-command: + group: example_group + destination: command_topic + consumer: + concurrency: 1 + ``` +2. Choose one of the Programmatic or Functional way to consume `Message`s. + +### Programmatic way + +- Register beans of inbound `SubscribableChannel`, `SpringMessagePublisher`, `IntegrationFlow`. `MessageRouter` + - [example code](https://github.com/naver/eventeria/blob/main/guide-projects/spring-boot-eventeria-guide/src/main/java/com/navercorp/eventeria/guide/boot/listener/ProgrammaticBindingEventListener.java) + - `MessageRouter`: route messages to your application. + +### Functional way + +1. Register additional function mappings in application.yml or application.properties. + ```yml + spring: + cloud: + function: + definition: + transformCloudEventToMessage|consumeImplementsEventeriaMessage; + transformCloudEventToMessage|consumeXXX; + ``` +2. Register `Function` bean which converts spring-messaging `Message` to eventeria `Message`. + ```java + @Bean + Function, Message> transformCloudEventToMessage( + CloudEventMessageReaderWriter cloudEventMessageReaderWriter + ) { + return FunctionalBindingSupports.convertToMessage(cloudEventMessageReaderWriter); + } + ``` +3. Register your `Consumer` bean which receives data type of eventeria `Message`. + ```java + @Bean + Consumer consumeImplementsEventeriaMessage() { + return command -> log.info("[CONSUME][consumeImplementsEventeriaMessage] {}", command); + } + ``` ## License + ``` Copyright (c) 2022-present NAVER Corp. Licensed under the Apache License, Version 2.0 (the "License");