-
Notifications
You must be signed in to change notification settings - Fork 35
[volume - 8] Decoupling with Kafka #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pmh5574
wants to merge
2
commits into
Loopers-dev-lab:pmh5574
Choose a base branch
from
pmh5574:feature/week8
base: pmh5574
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/commerce-api/src/main/java/com/loopers/domain/payment/PaymentEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.loopers.domain.payment; | ||
|
|
||
| public class PaymentEvent { | ||
| public record PaymentPaid(Payment payment) { | ||
| public static PaymentPaid from(final Payment payment) { | ||
| return new PaymentPaid(payment); | ||
| } | ||
| } | ||
| public record PaymentFailed(Payment payment) { | ||
| public static PaymentFailed from(final Payment payment) { | ||
| return new PaymentFailed(payment); | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
apps/commerce-api/src/main/java/com/loopers/domain/payment/PaymentEventPublisher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.loopers.domain.payment; | ||
|
|
||
| import com.loopers.domain.payment.PaymentEvent.PaymentPaid; | ||
|
|
||
| public interface PaymentEventPublisher { | ||
| void publish(PaymentPaid paymentCreated); | ||
| void publish(PaymentEvent.PaymentFailed paymentFailed); | ||
| } |
28 changes: 28 additions & 0 deletions
28
...merce-api/src/main/java/com/loopers/infrastructure/payment/PaymentCoreEventPublisher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.loopers.infrastructure.payment; | ||
|
|
||
| import com.loopers.domain.payment.PaymentEvent.PaymentPaid; | ||
| import com.loopers.domain.payment.PaymentEvent.PaymentFailed; | ||
| import com.loopers.domain.payment.PaymentEventPublisher; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class PaymentCoreEventPublisher implements PaymentEventPublisher { | ||
|
|
||
| private static final String paidTopic = "payment.paid"; | ||
| private static final String failedTopic = "payment.failed"; | ||
| private final KafkaTemplate<Object, Object> kafkaTemplate; | ||
|
|
||
|
|
||
| @Override | ||
| public void publish(final PaymentPaid paymentCreated) { | ||
| kafkaTemplate.send(paidTopic, paymentCreated.payment().getId(), paymentCreated); | ||
| } | ||
|
|
||
| @Override | ||
| public void publish(final PaymentFailed paymentFailed) { | ||
| kafkaTemplate.send(failedTopic, paymentFailed.payment().getId(), paymentFailed); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
apps/commerce-api/src/main/java/com/loopers/interfaces/event/order/OrderEventListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.loopers.interfaces.event.order; | ||
|
|
||
| import com.loopers.confg.kafka.KafkaConfig; | ||
| import java.util.List; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.support.Acknowledgment; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class OrderEventListener { | ||
|
|
||
| @KafkaListener( | ||
| topics = {"payment.paid"}, | ||
| containerFactory = KafkaConfig.BATCH_LISTENER, | ||
| groupId = "order" | ||
| ) | ||
| public void orderPaidListener( | ||
| List<ConsumerRecord<Object,Object>> messages, | ||
| Acknowledgment acknowledgment | ||
| ){ | ||
| // messages.stream() | ||
| acknowledgment.acknowledge(); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...commerce-api/src/main/java/com/loopers/interfaces/event/product/ProductEventListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.loopers.interfaces.event.product; | ||
|
|
||
| import com.loopers.confg.kafka.KafkaConfig; | ||
| import java.util.List; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.support.Acknowledgment; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class ProductEventListener { | ||
|
|
||
| @KafkaListener( | ||
| topics = {"payment.paid"}, | ||
| containerFactory = KafkaConfig.BATCH_LISTENER, | ||
| groupId = "product" | ||
| ) | ||
| public void productPaidListener( | ||
| List<ConsumerRecord<Object,Object>> messages, | ||
| Acknowledgment acknowledgment | ||
| ){ | ||
| // messages.stream() | ||
| acknowledgment.acknowledge(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kafka ํ๋ก๋์ ์์ฑ์ ํ๊ธฐ๋ฒ์ ์์ ํด์ผ ํฉ๋๋ค.
Spring Boot์
spring.kafka.producer.properties๋งต ๋ด๋ถ์์๋ ์ (dot)์ ํฌํจํ Kafka ํด๋ผ์ด์ธํธ ์์ฑ ์ด๋ฆ์ ๋๊ดํธ ํ๊ธฐ๋ฒ์ผ๋ก ์์ฑํด์ผ ํฉ๋๋ค. ํ์ฌenable.idempotence: true๋ YAML ํ์๊ฐ ์ค์ฒฉ๋ ํค๋ก ํด์ํ ์ ์์ต๋๋ค.Based on learnings, Kafka ํด๋ผ์ด์ธํธ ์์ฑ์ ์ ํํ ์ด๋ฆ์ผ๋ก ์ธ์๋๋๋ก ๋๊ดํธ๋ก ๊ฐ์ธ์ผ ํฉ๋๋ค.
๐ ์์ ์ ์
producer: key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.springframework.kafka.support.serializer.JsonSerializer retries: 3 acks: all properties: - enable.idempotence: true + "[enable.idempotence]": true๐ Committable suggestion
๐ค Prompt for AI Agents