Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from spotguides/kafka
Browse files Browse the repository at this point in the history
Add optional KafkaController
  • Loading branch information
bonifaido authored Nov 4, 2019
2 parents b9a6d82 + 1ae71e7 commit dbbfb5c
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
secretKeyRef:
name: {{ if .Values.mysql.existingSecret }}{{ .Values.mysql.existingSecret }}{{- else }}{{ include "call-nested" (list . "mysql" "mysql.fullname") }}{{- end }}
key: mysql-password
- name: SPRING_KAFKA_BOOTSTRAP_SERVERS
value: {{ .Values.kafka.bootstrapServers }}
{{ range $key, $value := .Values.env }}
- name: {{ $key }}
value: {{ $value | quote }}
Expand Down
4 changes: 4 additions & 0 deletions .banzaicloud/charts/spotguide-spring-boot/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ mysql:
livenessProbe:
initialDelaySeconds: 200

kafka:
enabled: false
bootstrapServers: "kafka-headless.kafka:29092"

pipeline-ingress:
enabled: false

Expand Down
2 changes: 1 addition & 1 deletion .banzaicloud/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pipeline:
image: lachlanevenson/k8s-helm:latest
commands:
- helm init -c
- helm repo add banzaicloud-stable http://kubernetes-charts.banzaicloud.com/branch/master
- helm repo add banzaicloud-stable https://kubernetes-charts.banzaicloud.com
- helm dep update ./.banzaicloud/charts/spotguide-spring-boot
- helm package ./.banzaicloud/charts/spotguide-spring-boot

Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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]"}'
```
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ services:
MYSQL_PASSWORD: sparky123
ports:
- 3306:3306

zookeeper:
image: wurstmeister/zookeeper
ports:
- 2181:2181

kafka:
image: wurstmeister/kafka
depends_on:
- zookeeper
ports:
- 9092:9092
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<version>${spring-cloud-kubernetes.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -110,7 +115,7 @@
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.2</version>
<version>1.7.0</version>
<configuration>
<from>
<image>gcr.io/distroless/java:11</image>
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/banzaicloud/spotguide/KafkaController.java
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);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ spring:
username: sparky
password: sparky123

kafka:
enabled: false
bootstrap-servers: localhost:9092
consumer:
group-id: myGroup

# There is an issue with `spring-cloud-starter-config`, when mixed with `spring-boot-starter-data-jpa` and `Spring Boot v2.1.x`.
# https://github.com/spring-cloud/spring-cloud-security/issues/143#issuecomment-376391930
spring.main.allow-bean-definition-overriding: true
Expand Down

0 comments on commit dbbfb5c

Please sign in to comment.