-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
58 additions
and
47 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Spring Boot | ||
|
||
Spring Boot + Spring Kafka producer and consumer examples. | ||
|
||
### Other LInks | ||
|
||
- [confluent spring kafka examples](https://www.confluent.io/blog/apache-kafka-spring-boot-application/) | ||
- [spring kafka settings](https://docs.spring.io/spring-kafka/reference/html/) | ||
|
||
### Setup | ||
|
||
Run spring boot: | ||
|
||
```bash | ||
./gradlew kafka-spring-boot:bootRun | ||
http :8585/actuator/health | ||
``` | ||
|
||
### Produce | ||
|
||
Spring has the class `KafkaTemplate` that allows you to produce messages. | ||
|
||
```java | ||
@Value("${spring.kafka.topic}") | ||
private String topic; | ||
|
||
@Autowired | ||
private KafkaTemplate<String, Customer> kafkaTemplate; | ||
|
||
public void sendCustomer(Customer customer) { | ||
log.info("Producing message: {}", customer); | ||
kafkaTemplate.send(topic, customer.getId().toString(), customer); | ||
} | ||
``` | ||
|
||
In another terminal: | ||
|
||
```bash | ||
http :8585/produce messages==10 | ||
``` | ||
|
||
### Consume | ||
|
||
You can use the `KafkaListener` annotation. | ||
|
||
```java | ||
@KafkaListener(topics = { "${spring.kafka.topic}" }) | ||
public void consume(ConsumerRecord<String, Customer> record) { | ||
log.info("Customer ID: {}", record.value()); | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.