项目作者: AndriyKalashnykov

项目描述 :
Spring Boot REST microservice
高级语言: Java
项目地址: git://github.com/AndriyKalashnykov/spring-boot-rest-example.git
创建时间: 2019-12-12T17:37:17Z
项目社区:https://github.com/AndriyKalashnykov/spring-boot-rest-example

开源协议:Apache License 2.0

下载


test
Hits
License: MIT

Spring Boot REST microservice

Java / Maven / Spring Boot microservice

  • Full integration with the latest Spring Framework 2.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
  • CRUD functionality with H2 in-memory data source using Spring Repository pattern
  • MockMVC test framework
  • Self-documented APIs: Swagger2 using annotations

Pre-requisites

Build and Run

This application is packaged as a jar which has Tomcat 8 embedded.

  • Clone this repository
  1. git clone git@github.com:AndriyKalashnykov/spring-boot-rest-example.git
  2. cd spring-boot-rest-example
  • Select JDK
  1. sdk install java 18.0.1-tem
  2. sdk use java 18.0.1-tem
  • Build the project and run the tests by running
  1. mvn clean package
  • Run the service
  1. mvn clean spring-boot:run -Dspring-boot.run.profiles=default
  1. http://localhost:8080/swagger-ui/#/hotels
  2. http://localhost:8080/actuator/env
  3. http://localhost:8080/actuator/health
  4. http://localhost:8080/actuator/info
  5. http://localhost:8080/actuator/metrics
  6. http://localhost:8080/actuator/configprops

Microservice API

  1. curl -X POST 'http://localhost:8080/v1/hotels' --header 'Content-Type: application/json' --header 'Accept: application/json' --data @hotel.json --stderr -

or

  1. http POST 'http://localhost:8080/v1/hotels' < hotel.json

or

  1. curl -X POST 'http://localhost:8080/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 -

Retrieve a paginated list of hotels

  1. curl -X GET --silent 'http://localhost:8080/v1/hotels?page=0&size=10' --stderr - 2>&1 | jq .

or

  1. http 'http://localhost:8080/v1/hotels?page=0&size=10'

Swagger 2 API docs

  1. xdg-open http://localhost:8080/swagger-ui/#/hotels

Building docker image

Optional, local test only: Using local maven cache

In order to build image quickly by compiling maven project using host OS maven repo

Build project, artifact will be placed in $PWD/target

  1. cd spring-boot-rest-example
  2. docker run -v ~/.m2:/root/.m2 -v "$PWD":/usr/src -w /usr/src maven:3-jdk-8 mvn clean package

Build non multi-stage image using existing artifact in $PWD/target

  1. cd spring-boot-rest-example
  2. docker rm -f spring-boot-rest-example
  3. docker build -f Dockerfile.maven-host-cache -t spring-boot-rest-example .

Build multi-stage image

  1. docker rm -f spring-boot-rest-example
  2. docker build -f Dockerfile.maven-multi-stage-layer-cached -t spring-boot-rest-example .

Test application

  1. docker run --name spring-boot-rest-example -p 8080:8080 spring-boot-rest-example:latest
  2. curl -X POST 'http://localhost:8080/v1/hotels' --header 'Content-Type: application/json' --header 'Accept: application/json' --data @hotel.json --stderr -
  3. curl -X GET --silent 'http://localhost:8080/v1/hotels?page=0&size=10' --stderr - 2>&1 | jq .

Attaching to the application from IDE

Run the service with these command line options:

  1. mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

or

  1. java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=test -Ddebug -jar target/spring-boot-rest-example-0.0.1.jar

IntelliJ : Run -> Edit configuration -> Remote.

IntelliJ IDEA

Deploy application to k8s

  1. minikube delete --all
  2. minikube start -p minikube --memory=16384 --cpus=6 --disk-size=30g --vm-driver=virtualbox
  3. eval $(minikube docker-env)
  4. eval "$(docker-machine env -u)"
  5. # minikube start --vm-driver=virtualbox --extra-config=apiserver.anonymous-auth=false --insecure-registry=localhost:5000
  6. # deploy to k8s
  7. minikube ssh 'docker logs $(docker ps -a -f name=k8s_kube-api --format={{.ID}})'

Test deployed application

  1. 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)/v1/hotels
  2. http $(minikube service spring-boot-rest-example --url | sed -n 1p)/v1/hotels?page=0&size=10
  3. http $(minikube service spring-boot-rest-example --url | sed -n 2p)/swagger-ui.html
  4. http $(minikube service spring-boot-rest-example --url | sed -n 2p)/info
  5. http $(minikube service spring-boot-rest-example --url | sed -n 2p)/health

Monitor k8s resources

  1. 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'
  2. kubectl top pod --all-namespaces