This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
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.
Merge pull request #6 from spotguides/kafka
Add optional KafkaController
- Loading branch information
Showing
8 changed files
with
100 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,7 @@ Our [`Spring Boot Actuator` library](https://docs.spring.io/spring-boot/docs/cur | |
|
||
## Endpoints | ||
|
||
These are the implemented REST endpoints in the sample application. | ||
These are the available REST endpoints in the sample application: | ||
|
||
### `GET /actuator/health/kubernetes` | ||
|
||
|
@@ -149,7 +149,6 @@ Kubernetes endpoint, returns basic [Pod](https://kubernetes.io/docs/concepts/wor | |
|
||
Health check, liveness probe endpoint. Returns `200` when the application is healthy, can reach the database. | ||
|
||
|
||
### `GET /api/v1/users` | ||
|
||
Fetch all users. | ||
|
@@ -179,6 +178,22 @@ Update a user. The request body has the same schema as [`POST /api/v1/users`](#p | |
|
||
Delete a user. | ||
|
||
## Optional Kafka endpoints if running with SPRING_KAFKA_ENABLED=true | ||
|
||
### `GET /api/v1/kafka` | ||
|
||
Fetch all Kafka messages. | ||
|
||
### `POST /api/v1/kafka` | ||
|
||
Send a new message to the `"spring-boot"` Kafka topic. | ||
|
||
```json | ||
{ | ||
"message": "hello-spring-boot" | ||
} | ||
``` | ||
|
||
## Build Run and Test locally | ||
|
||
```bash | ||
|
@@ -205,5 +220,5 @@ mvn clean package -DskipTests | |
|
||
SPRING_DATASOURCE_USERNAME=sparky SPRING_DATASOURCE_PASSWORD=sparky123 java -jar target/app.jar | ||
|
||
curl -H "Content-Type: application/json" http://localhost:8080/users -d '{"userName":"john","email":"[email protected]"}' | ||
curl -H "Content-Type: application/json" http://localhost:8080/api/v1/users -d '{"userName":"john","email":"[email protected]"}' | ||
``` |
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/banzaicloud/spotguide/KafkaController.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,46 @@ | ||
package com.banzaicloud.spotguide; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/kafka") | ||
@ConditionalOnProperty(value = "spring.kafka.enabled", havingValue = "true") | ||
public class KafkaController { | ||
|
||
private static final String BOOT_TOPIC = "spring-boot"; | ||
|
||
private final List<String> messages = new ArrayList<>(); | ||
|
||
@Autowired | ||
private KafkaTemplate<String, String> kafkaTemplate; | ||
|
||
@PostMapping | ||
public ResponseEntity sendMessage(@RequestBody Map<String, String> body) { | ||
kafkaTemplate.send(BOOT_TOPIC, body.get("message")); | ||
return new ResponseEntity(HttpStatus.ACCEPTED); | ||
} | ||
|
||
@GetMapping | ||
public List<String> listMessages() { | ||
return messages; | ||
} | ||
|
||
@KafkaListener(topics = KafkaController.BOOT_TOPIC) | ||
public void consume(String message) { | ||
messages.add(message); | ||
} | ||
} |
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