Skip to content

Commit

Permalink
update spring boot
Browse files Browse the repository at this point in the history
  • Loading branch information
sauljabin committed Jun 18, 2024
1 parent a675744 commit 035376f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 47 deletions.
19 changes: 3 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Kafka Sandbox](https://sauljabin.github.io/kafka-sandbox/) it's a markdown book designed to help you to deploy a kafka sandbox locally. It intends to be a simple way to get started with kafka and
[Kafka Sandbox](https://sauljabin.github.io/kafka-sandbox/) it's a markdown book designed to help you to deploy a kafka sandbox locally.
It intends to be a simple way to get started with kafka and
help you on your learning path. It provides you with a wide variety of tools from the kafka ecosystem and a simple way
to run them all. It also includes a set of tools and tips to make it easier for you to use kafka. It does not include
security since it is not a production system.
Expand All @@ -9,7 +10,7 @@ You can access it at https://sauljabin.github.io/kafka-sandbox/.
## Developing Commands

> You must to install [rust](https://www.rust-lang.org/tools/install) first.
> You must install [rust](https://www.rust-lang.org/tools/install) first.
Install `mdbook`:

Expand All @@ -28,17 +29,3 @@ Build statics:
```bash
mdbook build
```

## Using Docker

Create docker image:

```bash
docker build -t sauljabin/kafka-sandbox-book:latest -f docker/Dockerfile .
```

Running the book ([open it in the web browser](http://localhost)):

```bash
docker run --name kafka-sandbox-book -d -p 80:80 sauljabin/kafka-sandbox-book:latest
```
4 changes: 2 additions & 2 deletions kafka-spring-boot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ management:

spring:
kafka:
topic: kafka-spring-boot.customers
topic: spring.customers
consumer:
bootstrap-servers: localhost:19092,localhost:29092,localhost:39092
group-id: kafka-spring-boot.consumer
group-id: spring.consumer
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
Expand Down
1 change: 1 addition & 0 deletions md/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Kafka Rest Proxy](kafka-rest-proxy.md)
- [Kafka MQTT Proxy](kafka-mqtt-proxy.md)
- [Avro Producer and Consumer](avro-producer-and-consumer.md)
- [Spring Boot](spring-boot.md)
- [Cleanup](cleanup.md)

---
Expand Down
2 changes: 1 addition & 1 deletion md/avro-producer-and-consumer.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ for (int i = 0; i < messages; i++) {
Supplier supplier = createNewCustomer();
ProducerRecord<String, Supplier> record = new ProducerRecord<>(
topic,
supplier.getId(),
supplier.getId().toString(),
supplier
);
producer.send(
Expand Down
51 changes: 51 additions & 0 deletions md/spring-boot.md
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());
}
```
5 changes: 0 additions & 5 deletions md/using-kafka/kafka-clients/kafka-clients.md

This file was deleted.

23 changes: 0 additions & 23 deletions md/using-kafka/kafka-clients/spring-boot.md

This file was deleted.

0 comments on commit 035376f

Please sign in to comment.