Skip to content

shopKubernetesSpringboot/cart

Repository files navigation

Shop cart

Build Status

Quality Gate Status Bugs Code Smells Coverage Duplicated Lines (%) Lines of Code Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities

Description

Fully reactive shop cart REST API micro-service.

Also see the related projects:

Prerequisites

  • Enable annotation processors (in your IDE for lombok)

Architecture

  • Reactive Layers architecture (spring-framework reactor).
  • Java non-blocking functional programming: reactor+streams.
  • REST API with Spring-WebFlux.

Notes:

  • BeanValidation implementation.
  • Reactive junit tests.
  • Integration test.
  • Basic auth security implementation & csrf.
  • Sonar gradle plugin
    • you can run Sonarqube gradle task if you have sonar installed on localhost:9000
    • or see the Sonarcloud.io dashboard for this project (through travis-ci integration).

Run

Spring boot application

  • Run ShopCartApp as a spring-boot app:
    • command line: gradlew :bootRun
    • intellij: right button on ShopCartApp.java & Run...

With docker

#optional
docker system prune

#build & run image
docker build -t techtests/shopcart .
docker run -p 8080:8080 -t techtests/shopcart

Try

With curl

curl -v -b cookies.txt -c cookies.txt -u user:user -d '{"item": {"id": 1,"name": "product4"}}' -H 'Content-Type: application/json' http://localhost:8080/cart/add
curl -v -b cookies.txt -c cookies.txt -u user:user http://localhost:8080/cart/list

With postman

  • Use postman_collection.json (importing the json file in Postman client):
    • add: to add an item to the cart
    • list: to see all items in the cart

Initial setup reference

Travis & SonarCloud setup

Create a token for this project: https://sonarcloud.io/account/security/

Encrypt key (in project folder)

travis encrypt --pro <keyCreatedInSonarCloud>

Microservice

Size optimization

With spring-boot-thin-launcher

https://github.com/spring-projects-experimental/spring-boot-thin-launcher

Original/thin generated jars:

-rwxrwxrwx 1 slks slks 19683502 mar 15 09:13 shopCart-0.0.1-SNAPSHOT.jar
-rwxrwxrwx 1 slks slks 25610 mar 15 16:55 build/libs/shopCart-0.0.1-SNAPSHOT.jar

Original/thin docker image (techtests/shopcart): 125MB/105MB

Using FROM openjdk:8-jre-alpine (instead of FROM openjdk:8-jdk-alpine) final size: 84.9MB

Attempt to minimize docker image size with jdk11

This is not a solution as it generates a bigger image

docker build -f Dockerfile-optimized -t techtests/shopcart-optimized .
docker images | grep techtests/shopcart 

Generated images:

$ docker images | grep techtests/shopcart
techtests/shopcart                               latest               36234d830c12        14 hours ago        125MB
techtests/shopcart-optimized                     latest               3e22c1446dd8        15 hours ago        151MB

Kubernetes

Compile, publish to docker and deploy to kubernetes

./publishToDocker.sh && bash -c '../kubernetes/publish.sh $0' cart