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. YUGHUUU
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:
- Docker
- Docker for Desktop
- skaffold and its dependencies
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 build . -t spotguide-spring-boot
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 build . -t spotguide-spring-boot
# 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 implemented 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.
Build Run and Test locally
## Build and Test locally with Docker for Mac Kubernetes
docker build -t banzaicloud/spotguide-spring-boot:latest .
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/users -d '{"userName":"john","email":"john@doe.com"}'