-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Added kafka configs to Payment Service #55
Merged
koreanMike513
merged 1 commit into
update/orderservice-and-requestdto
from
feature/add-kafka-payments
Jan 19, 2025
Merged
Changes from all commits
Commits
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 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
57 changes: 57 additions & 0 deletions
57
payment/src/main/java/com/f_lab/la_planete/payment/config/KafkaConsumerConfig.java
This file contains 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,57 @@ | ||
package com.f_lab.la_planete.payment.config; | ||
|
||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
import org.springframework.kafka.support.serializer.JsonDeserializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
public class KafkaConsumerConfig { | ||
|
||
@Value("${spring.kafka.consumer.bootstrap-servers}") | ||
private String BOOTSTRAP_SERVERS; | ||
|
||
@Value("${spring.kafka.consumer.enable-auto-commit}") | ||
private boolean AUTO_COMMIT; | ||
|
||
@Value("${kafka.container.concurrency:3}") | ||
private int CONCURRENCY; | ||
|
||
@Bean | ||
public Map<String, Object> consumerConfig() { | ||
Map<String, Object> consumerConfig = new HashMap<>(); | ||
|
||
consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS); | ||
consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class); | ||
consumerConfig.put(JsonDeserializer.TRUSTED_PACKAGES, "com.f_lab.la_planete.core.*"); | ||
consumerConfig.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, AUTO_COMMIT); | ||
|
||
return consumerConfig; | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, Object> factory = | ||
new ConcurrentKafkaListenerContainerFactory<>(); | ||
|
||
factory.setConsumerFactory(consumerFactory()); | ||
factory.setConcurrency(CONCURRENCY); | ||
|
||
return factory; | ||
} | ||
|
||
@Bean | ||
public ConsumerFactory<String, Object> consumerFactory() { | ||
return new DefaultKafkaConsumerFactory<>(consumerConfig()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
payment/src/main/java/com/f_lab/la_planete/payment/config/KafkaProducerConfig.java
This file contains 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,78 @@ | ||
package com.f_lab.la_planete.payment.config; | ||
|
||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.config.TopicBuilder; | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.core.ProducerFactory; | ||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
public class KafkaProducerConfig { | ||
|
||
|
||
@Value("${spring.kafka.producer.bootstrap-servers:localhost:9092}") | ||
private String BOOTSTRAP_SERVERS; | ||
|
||
@Value("${spring.kafka.producer.ack:all}") | ||
private String ACK; | ||
|
||
@Value("${spring.kafka.producer.enable.idempotence:true}") | ||
private String IDEMPOTENCE; | ||
|
||
@Value("${payments.commands.topic.name}") | ||
private String paymentProcessCommand; | ||
|
||
@Value("${payments.events.topic.name}") | ||
private String paymentProcessedEvents; | ||
|
||
@Value("${kafka.topic.partitions:3}") | ||
private int TOPIC_PARTITIONS; | ||
|
||
|
||
@Bean | ||
public Map<String, Object> producerConfig() { | ||
Map<String, Object> config = new HashMap<>(); | ||
|
||
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS); | ||
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); | ||
config.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, IDEMPOTENCE); | ||
config.put(ProducerConfig.ACKS_CONFIG, ACK); | ||
|
||
return config; | ||
} | ||
|
||
public ProducerFactory<String, Object> producerFactory() { | ||
return new DefaultKafkaProducerFactory<>(producerConfig()); | ||
} | ||
|
||
@Bean | ||
public KafkaTemplate<String, Object> kafkaTemplate() { | ||
return new KafkaTemplate<>(producerFactory()); | ||
} | ||
|
||
@Bean | ||
public NewTopic foodsProcessFoodsReserveCommand() { | ||
return TopicBuilder | ||
.name(paymentProcessCommand) | ||
.partitions(TOPIC_PARTITIONS) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public NewTopic foodsFoodsReservationProcessedEvent() { | ||
return TopicBuilder | ||
.name(paymentProcessedEvents) | ||
.partitions(TOPIC_PARTITIONS) | ||
.build(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,37 @@ | ||
server.port: 0 | ||
|
||
spring: | ||
application: | ||
name: payments | ||
|
||
kafka: | ||
producer: | ||
bootstrap-servers: localhost:9092 | ||
acks: all | ||
enable: | ||
idempotence: true | ||
|
||
consumer: | ||
bootstrap-servers: localhost:9092 | ||
group-id: payments | ||
enable-auto-commit: false | ||
properties: | ||
spring.json.trusted.packages: com.f-lab.la_planete.core.* | ||
|
||
payments: | ||
commands: | ||
topic: | ||
name: payments.payment-process-command | ||
|
||
events: | ||
topic: | ||
name: payments.payment-processed-event | ||
|
||
|
||
|
||
kafka: | ||
topic: | ||
partitions: 3 | ||
|
||
container: | ||
concurrency: 3 |
Oops, something went wrong.
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.
이전 pr에서 중복되는 class들에 대한 공통 로직 추출 및 재사용 방법에 대한 고민을 추천드립니다.