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

spotguides/spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spotguide for Sprint Boot with MySQL database

This repository was created by Banzai Cloud Pipeline. Spotguides provide automated configuration, logging, monitoring, security, and scaling for your application stacks.

Development

Every time you make changes to the source code and update the master branch, the CI/CD pipeline will be triggered to test, validate and update the deployment of your application. Check the .pipeline.yaml file for CI/CD steps.

Local development

Deploy to a local Kubernetes cluster

Requirements:

A local Kubernetes cluster must be running (eg. Docker for Desktop).

# verify the Kubernetes context
$ kubectl config get-contexts
# expected output
CURRENT   NAME                 CLUSTER                      AUTHINFO             NAMESPACE
*         docker-for-desktop   docker-for-desktop-cluster   docker-for-desktop
# build the Docker image and deploy via helm
$ cd .banzaicloud
$ skaffold run

This will build the application and install all the components to Kubernetes.

Run tests

Using installed Maven:

Requirements:

Commands:

mvn clean package
Using Docker:

Requirements:

Commands:

DOCKER_REPO=spotguide-spring-boot DOCKER_TAG=latest mvn compile jib:dockerBuild

Start application in development mode

This spotguide uses spring-boot-devtools, it is a utility dependency that will monitor for any changes in your classpath and automatically restart your server.

Using local Spring Boot and with the SQL compatible H2 database:

Requirements:

Commands:

mvn spring-boot:run

Using Docker and Docker Compose:

Requirements:

Commands:

# start dependencies
docker-compose up -d
# build image
DOCKER_REPO=spotguide-spring-boot DOCKER_TAG=latest mvn compile jib:dockerBuild
# start application in development mode
mvn spring-boot:run

Kubernetes ready Spring Boot

Our Spring Boot Actuator library and Spring Cloud Kubernetes together provides all the essential features to prepare your Spring Boot application truly ready for production on Kubernetes, such as:

  • graceful error handling & shutdown
  • structured JSON logging
  • various HTTP middleware
  • health checks
  • metrics

Environment variables

name description default
SERVER_PORT Application port 8080
SPRING_DATASOURCE_URL JDBC URL, scheme: jdbc:driver:host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
SPRING_DATASOURCE_USERNAME Database user
SPRING_DATASOURCE_PASSWORD Database user password
SPRING_DATASOURCE_DATABASE Database database name

Endpoints

These are the available REST endpoints in the sample application:

GET /actuator/health/kubernetes

Kubernetes endpoint, returns basic Pod information, like name, namespace and image.

GET /actuator/prometheus

Prometheus metrics endpoint.

GET /actuator/health

Health check, liveness probe endpoint. Returns 200 when the application is healthy, can reach the database.

GET /api/v1/users

Fetch all users.

POST /api/v1/users

Create a new user. The request body has the following schema:

{
  "email": "",
  "userName": "",
  "firstName": "",
  "lastName": ""
}

GET /api/v1/users/:id

Fetch a user.

PUT /api/v1/users/:id

Update a user. The request body has the same schema as POST /api/v1/users.

DELETE /api/v1/users/:id

Delete a user.

Optional Kafka endpoints

You have to enable Kafka support via running it 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.

{
  "message": "hello-spring-boot"
}

Build Run and Test locally

## Build and Test locally with Docker for Mac Kubernetes
DOCKER_REPO=banzaicloud/spotguide-spring-boot DOCKER_TAG=latest mvn compile jib:dockerBuild
helm dep update .banzaicloud/charts/spotguide-spring-boot
helm upgrade --install spotguide-spring-boot .banzaicloud/charts/spotguide-spring-boot --set ingress.enabled=true --set "ingress.hosts[0]=localhost" --set monitor.enabled=true --set pipeline-ingress.enabled=true

# Check the application (if Ingress not enabled)
kubectl port-forward deployment/spotguide-spring-boot 8080

# Access the Spring application
open http://localhost:[8080]
# Delete the Helm release
helm delete --purge spotguide-spring-boot
# Build the fat JAR
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/api/v1/users -d '{"userName":"john","email":"[email protected]"}'