Java / Maven / Spring Boot microservice / Docker image / Buildkit / Buildpacks / Kaniko / Skaffold/ K8s Deployment / Minikube
- Full integration with the latest Spring Framework 2.x.x: inversion of control, dependency injection, etc.
- Packaging as a single jar with embedded container (tomcat 8)
- Demonstrates how to set up healthcheck, metrics, info, environment, etc. endpoints automatically on a configured port. Inject your own health / metrics info with a few lines of code.
- RESTful service using annotation: supports both XML and JSON request / response
- Exception mapping from application exceptions to the right HTTP response with exception details in the body
- Spring Data Integration with JPA/Hibernate / MongoDB
- CRUD functionality with H2 in-memory data source using Spring Repository pattern
- MockMVC test framework
- Self-documented APIs: Swagger2 using annotations
- Buildkit
- Buildpacks
- Kaniko
- Skaffold
- K8s Deployment / Minikube
-
Install OpenJDK 11.x
sdk install java 11.0.11.hs-adpt sdk default java 11.0.11.hs-adpt sdk use java 11.0.11.hs-adpt
This application is packaged as a jar which has Tomcat 8 embedded.
- Clone this repository
git clone git@github.com:AndriyKalashnykov/spring-boot-demo.git
cd spring-boot-demo
- Build the project and run the service
mvn clean package spring-boot:run -Drun.arguments="spring.profiles.active=default" -DskipTests
or
mvn clean package
java -jar -Dspring.profiles.active=default target/spring-boot-demo-1.0.jar
http://localhost:8080/actuator/info
http://localhost:8080/actuator/health
http://localhost:8080/actuator/env
http://localhost:8080/actuator/health/hotelServiceHealth
http://localhost:8080/actuator/metrics
http://localhost:8080/commitid?mediaType=json
http://localhost:8080/v2/api-docs
http://localhost:8080/swagger-ui/index.html
curl -X POST 'http://localhost:8080/example/v1/hotels' --header 'Content-Type: application/json' --header 'Accept: application/json' --data @hotel.json --stderr -
or
http POST 'http://localhost:8080/example/v1/hotels' < hotel.json
or
curl -X POST 'http://localhost:8080/example/v1/hotels' --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"name":"Beds R Us","description":"Very basic, small rooms but clean","city":"Santa Ana","rating":2}' --stderr -
curl -X GET --silent 'http://localhost:8080/example/v1/hotels?page=0&size=10&mediaType=json' --stderr - 2>&1 | jq .
or
http 'http://localhost:8080/example/v1/hotels?page=0&size=10&mediaType=json'
open -a /Applications/Google\ Chrome.app http://localhost:8080/swagger-ui/index.html
In order to build image quickly by compiling maven project using host OS maven repo
Build project, artifact will be placed in "$PWD"/target
cd spring-boot-demo
docker run -v ~/.m2:/root/.m2 -v "$PWD":/usr/src -w /usr/src maven:3-jdk-11 mvn clean package
Docker Build enhancements for 18.09 release introduces a much-needed overhaul of the build architecture. By integrating BuildKit, users should see an improvement on performance, storage management, feature functionality, and security.
cd spring-boot-demo
docker rm -f spring-boot-demo
DOCKER_BUILDKIT=1 docker build -f Dockerfile -t spring-boot-demo .
# provide image registry login and password
DOCKER_LOGIN=
DOCKER_PWD=
docker login --username $DOCKER_LOGIN --password $DOCKER_PWD docker.io
./scripts/build-dockerimage-buildpacks.sh
./scripts/build-dockerimage-kaniko.sh
cd ./scripts
# adding 100 to port number to avoid local conflicts: 8081->8181
docker run --rm --name spring-boot-demo -p 8080:8080 -p 8181:8081 spring-boot-demo:latest
curl -X POST 'http://localhost:8080/example/v1/hotels' --header 'Content-Type: application/json' --header 'Accept: application/json' --data @hotel.json --stderr -
curl -X GET --silent 'http://localhost:8080/example/v1/hotels?page=0&size=10' --stderr - 2>&1 | jq .
Run the service with these command line options:
mvn clean package spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
or
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=test -Ddebug -jar target/spring-boot-demo-1.0.jar
IntelliJ : Run -> Edit configuration -> Remote.
minikube delete --all
minikube start -p minikube --memory=16384 --cpus=6 --disk-size=30g --vm-driver=virtualbox
eval $(minikube docker-env)
eval "$(docker-machine env -u)"
# minikube start --vm-driver=virtualbox --extra-config=apiserver.anonymous-auth=false --insecure-registry=localhost:5000
mvn clean package -DskipTests -Pk8s fabric8:undeploy
mvn clean package -Pk8s fabric8:deploy
minikube ssh 'docker logs $(docker ps -a -f name=k8s_kube-api --format={{.ID}})'
mvn clean package fabric8:deploy -Dfabric8.generator.from=fabric8/java-alpine-openjdk11-jdk
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --data @hotel.json $(minikube service spring-boot-rest-example --url | sed -n 1p)/example/v1/hotels
http $(minikube service spring-boot-demo --url | sed -n 1p)/example/v1/hotels?page=0&size=10
http $(minikube service spring-boot-demo --url | sed -n 2p)/swagger-ui/index.html
http $(minikube service spring-boot-demo --url | sed -n 2p)/info
http $(minikube service spring-boot-demo --url | sed -n 2p)/health
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'
kubectl top pod --all-namespaces
- Add Spring multi-layer support for the Docker image, be aware of the issue- docker/build-push-action#79
- Add GitHub workflow - see https://github.com/CodigoPraTodos/cursospratodos/blob/master/.github/workflows/ci-api.yml