-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create basic kafka consumer (#6)
- Loading branch information
1 parent
85773cc
commit cbf1f62
Showing
7 changed files
with
114 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# payments-hexagonal | ||
A Spring Boot API developed in Hexagonal Architecture | ||
|
||
## Docker compose | ||
```bash | ||
docker-compose up -d | ||
``` | ||
|
||
## Kafka | ||
* Consume messages for debugging: | ||
```bash | ||
kafka-console-consumer --bootstrap-server localhost:9092 --topic payments.info --from-beginning | ||
``` | ||
|
||
## Requirements |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/mv/hexagonal/payments/adapters/in/consumer/PaymentInfoConsumer.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,21 @@ | ||
package com.mv.hexagonal.payments.adapters.in.consumer; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Component | ||
public class PaymentInfoConsumer { | ||
@KafkaListener( | ||
topics = "${topics.payment-info.name}", | ||
groupId = "${topics.payment-info.group-id}") | ||
public void receive(ConsumerRecord<String, String> consumerRecord) { | ||
log.info("Received payment info: {}", consumerRecord); | ||
log.info("consumer key: {}", consumerRecord.key()); | ||
log.info("consumer value: {}", consumerRecord.value()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../producer/PaymentInfoProducerAdapter.java → ...pters/out/PaymentInfoProducerAdapter.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
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/mv/hexagonal/payments/configs/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,45 @@ | ||
package com.mv.hexagonal.payments.configs; | ||
|
||
import static org.apache.kafka.clients.consumer.ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
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.annotation.EnableKafka; | ||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory; | ||
|
||
@EnableKafka | ||
@Configuration | ||
public class KafkaConsumerConfig { | ||
@Value(value = "${topics.bootstrap-address}") | ||
private String bootstrapAddress; | ||
|
||
@Value(value = "${topics.payment-info.group-id}") | ||
private String groupId; | ||
|
||
@Bean | ||
public ConsumerFactory<String, String> consumerFactory() { | ||
Map<String, Object> props = new HashMap<>(); | ||
|
||
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.bootstrapAddress); | ||
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
props.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
// props.put(GROUP_ID_CONFIG, groupId); | ||
|
||
return new DefaultKafkaConsumerFactory<>(props); | ||
} | ||
|
||
@Bean | ||
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() { | ||
ConcurrentKafkaListenerContainerFactory<String, String> factory = | ||
new ConcurrentKafkaListenerContainerFactory<>(); | ||
factory.setConsumerFactory(this.consumerFactory()); | ||
return factory; | ||
} | ||
} |
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
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