-
Notifications
You must be signed in to change notification settings - Fork 34
[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 all commits
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
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); | ||
| } |
13 changes: 13 additions & 0 deletions
13
apps/commerce-api/src/main/java/com/loopers/domain/product/EventType.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,13 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum EventType { | ||
| VIEWED("์ํ ์์ธ ์กฐํ"), | ||
| ; | ||
|
|
||
| private final String description; | ||
| } |
9 changes: 9 additions & 0 deletions
9
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductEvent.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,9 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| public record ProductEvent() { | ||
| public record ProductViewed(Long productId) { | ||
| public static ProductViewed from(Long productId) { | ||
| return new ProductViewed(productId); | ||
| } | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductEventPublisher.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,5 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| public interface ProductEventPublisher { | ||
| void publish(ProductEvent.ProductViewed event); | ||
| } |
22 changes: 22 additions & 0 deletions
22
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductEventService.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,22 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| import com.loopers.domain.product.ProductEvent.ProductViewed; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| @Component | ||
| public class ProductEventService { | ||
| private final ProductOutboxEventRepository productOutboxEventRepository; | ||
|
|
||
| @Transactional | ||
| public ProductOutboxEvent saveOutboxEvent(final ProductOutboxEvent productOutboxEvent) { | ||
| return productOutboxEventRepository.save(productOutboxEvent); | ||
| } | ||
|
|
||
| public void findByProductEvent(final ProductViewed event) { | ||
| productOutboxEventRepository.findBy | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductOutboxEvent.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,31 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| import com.loopers.domain.BaseEntity; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Table(name = "product_outbox_events") | ||
| @Entity | ||
| public class ProductOutboxEvent extends BaseEntity { | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private EventType eventType; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private ProductOutboxStatus productOutboxStatus; | ||
|
|
||
| private Long productId; | ||
|
|
||
| public static ProductOutboxEvent create(final EventType eventType, final Long productId) { | ||
| ProductOutboxEvent productOutboxEvent = new ProductOutboxEvent(); | ||
| productOutboxEvent.eventType = eventType; | ||
| productOutboxEvent.productOutboxStatus = ProductOutboxStatus.PENDING; | ||
| productOutboxEvent.productId = productId; | ||
| return productOutboxEvent; | ||
| } | ||
| } | ||
|
|
5 changes: 5 additions & 0 deletions
5
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductOutboxEventRepository.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,5 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| public interface ProductOutboxEventRepository { | ||
| ProductOutboxEvent save(ProductOutboxEvent productOutboxEvent); | ||
| } |
15 changes: 15 additions & 0 deletions
15
apps/commerce-api/src/main/java/com/loopers/domain/product/ProductOutboxStatus.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,15 @@ | ||
| package com.loopers.domain.product; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum ProductOutboxStatus { | ||
| PENDING("์ด๋ฒคํธ ๋ฐํ ๋๊ธฐ ์ค"), | ||
| PUBLISHED("์ด๋ฒคํธ ๋ฐํ ์๋ฃ"), | ||
| FAILED("์ด๋ฒคํธ ๋ฐํ ์คํจ"), | ||
| ; | ||
|
|
||
| private final String description; | ||
| } |
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); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...merce-api/src/main/java/com/loopers/infrastructure/product/ProductCoreEventPublisher.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,20 @@ | ||
| package com.loopers.infrastructure.product; | ||
|
|
||
| import com.loopers.domain.product.ProductEvent; | ||
| import com.loopers.domain.product.ProductEventPublisher; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.ApplicationEventPublisher; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class ProductCoreEventPublisher implements ProductEventPublisher { | ||
|
|
||
| private final ApplicationEventPublisher applicationEventPublisher; | ||
| // private final KafkaTemplate<Object, Object> kafkaTemplate; | ||
|
|
||
| @Override | ||
| public void publish(final ProductEvent.ProductViewed event) { | ||
| applicationEventPublisher.publishEvent(event); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...api/src/main/java/com/loopers/infrastructure/product/ProductOutboxEventJpaRepository.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.infrastructure.product; | ||
|
|
||
| import com.loopers.domain.product.ProductOutboxEvent; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| public interface ProductOutboxEventJpaRepository extends JpaRepository<ProductOutboxEvent, Long> { | ||
| } |
17 changes: 17 additions & 0 deletions
17
...pi/src/main/java/com/loopers/infrastructure/product/ProductOutboxEventRepositoryImpl.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,17 @@ | ||
| package com.loopers.infrastructure.product; | ||
|
|
||
| import com.loopers.domain.product.ProductOutboxEvent; | ||
| import com.loopers.domain.product.ProductOutboxEventRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class ProductOutboxEventRepositoryImpl implements ProductOutboxEventRepository { | ||
| private final ProductOutboxEventJpaRepository productOutboxEventJpaRepository; | ||
|
|
||
| @Override | ||
| public ProductOutboxEvent save(final ProductOutboxEvent productOutboxEvent) { | ||
| return productOutboxEventJpaRepository.save(productOutboxEvent); | ||
| } | ||
| } |
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(); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
...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,46 @@ | ||
| package com.loopers.interfaces.event.product; | ||
|
|
||
| import com.loopers.application.product.ProductFacade; | ||
| import com.loopers.confg.kafka.KafkaConfig; | ||
| import com.loopers.domain.product.ProductEvent; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.support.Acknowledgment; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class ProductEventListener { | ||
|
|
||
| private final ProductFacade productFacade; | ||
|
|
||
| @KafkaListener( | ||
| topics = {"payment.paid"}, | ||
| containerFactory = KafkaConfig.BATCH_LISTENER, | ||
| groupId = "product" | ||
| ) | ||
| public void handle( | ||
| List<ConsumerRecord<Object,Object>> messages, | ||
| Acknowledgment acknowledgment | ||
| ){ | ||
| // messages.stream() | ||
| acknowledgment.acknowledge(); | ||
| } | ||
|
|
||
| @EventListener | ||
| public void productViewOutboxHandle(ProductEvent.ProductViewed event) { | ||
| productFacade.productViewOutboxHandle(event); | ||
| } | ||
|
|
||
| @Async | ||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| public void productViewPublishKafka(ProductEvent.ProductViewed event) { | ||
| productFacade.productViewPublishKafka(event); | ||
| } | ||
| } |
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