Skip to content

inspectIT/spring-petclinic-microservices

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Distributed version of the Spring PetClinic Sample Application built with Spring Cloud

Build Status License

This microservices branch was initially derived from AngularJS version to demonstrate how to split sample Spring application into microservices. To achieve that goal, we use Spring Cloud Gateway, Spring Cloud Circuit Breaker, Spring Cloud Config, Micrometer Tracing, Resilience4j, Open Telemetry and the Eureka Service Discovery from the Spring Cloud Netflix technology stack.

Starting services locally without Docker

Every microservice is a Spring Boot application and can be started locally using IDE (Lombok plugin has to be set up) or ../mvnw spring-boot:run command. Please note that supporting services (Config and Discovery Server) must be started before any other application (Customers, Vets, Visits and API). Startup of Tracing server, Admin server, Grafana and Prometheus is optional. If everything goes well, you can access the following services at given location:

You can tell Config Server to use your local Git repository by using native Spring profile and setting GIT_REPO environment variable, for example: -Dspring.profiles.active=native -DGIT_REPO=/projects/spring-petclinic-microservices-config

Starting services locally with docker-compose

Just start the infrastructure with docker-compose up or podman-compose up command, if you want to use the images from the inspectIT Docker Hub. Make sure to set the desired image-tag in .env file. The default tag is latest.

If you want to execute the application with the postgres profile, you can use the following command:

docker-compose -f docker-compose-postgres.yml up

If you want to build the images locally, you can use the following command: bash ./mvnw clean install -P buildDocker This requires Docker or Docker desktop to be installed and running.

Alternatively you can also build all the images on Podman, which requires Podman or Podman Desktop to be installed and running.

./mvnw clean install -PbuildDocker -Dcontainer.executable=podman

By default, the Docker OCI image is build for an linux/amd64 platform. For other architectures, you could change it by using the -Dcontainer.platform maven command line argument. For instance, if you target container images for an Apple M2, you could use the command line with the linux/arm64 architecture:

./mvnw clean install -P buildDocker -Dcontainer.platform="linux/arm64"

Once images are ready, you can start them with a single command docker-compose up or podman-compose up.

Containers startup order is coordinated with the service_healthy condition of the Docker Compose depends-on expression and the healthcheck of the service containers. After starting services, it takes a while for API Gateway to be in sync with service registry, so don't be scared of initial Spring Cloud Gateway timeouts. You can track services availability using Eureka dashboard available by default at http://localhost:8761.

The main branch uses an Eclipse Temurin with Java 17 as Docker base image.

NOTE: Under MacOSX or Windows, make sure that the Docker VM has enough memory to run the microservices. The default settings are usually not enough and make the docker-compose up painfully slow.

Starting services locally with docker-compose and Java

If you experience issues with running the system via docker-compose you can try running the ./scripts/run_all.sh script that will start the infrastructure services via docker-compose and all the Java based applications via standard nohup java -jar ... command. The logs will be available under ${ROOT}/target/nameoftheapp.log.

Each of the java based applications is started with the chaos-monkey profile in order to interact with Spring Boot Chaos Monkey. You can check out the (README)[scripts/chaos/README.md] for more information about how to use the ./scripts/chaos/call_chaos.sh helper script to enable assaults.

Understanding the Spring Petclinic application

See the presentation of the Spring Petclinic Framework version

A blog post introducing the Spring Petclinic Microsevices (french language)

You can then access petclinic here: http://localhost:8080/

Spring Petclinic Microservices screenshot

Architecture diagram of the Spring Petclinic Microservices

Spring Petclinic Microservices architecture

In case you find a bug/suggested improvement for Spring Petclinic Microservices

Our issue tracker is available here: https://github.com/spring-petclinic/spring-petclinic-microservices/issues

Database configuration

In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data. A similar setup is provided for Postgres in case a persistent database configuration is needed. Dependency for the Postgres JDBC driver is already included in the pom.xml files.

Start a Postgres database

You may start a Postgres database with docker:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

or download and install the Postgres database, which can be found here: https://dev.mysql.com/downloads/

Use the Spring 'postgres' profile

To use a postgres database, you have to start 3 microservices (visits-service, customers-service and vets-services) with the postgres Spring profile. Add the --spring.profiles.active=postgres as programm argument.

By default, at startup, database schema will be created and data will be populated. You may also manually create the PetClinic database and data by executing the "db/postgres/{schema,data}.sql" scripts of each 3 microservices.

In the application.yml of the Configuration repository, set the host either to the desired designation of the database.

If you are running the microservices with Docker, you have to add the postgres profile into the (Dockerfile)[docker/Dockerfile]:

ENV SPRING_PROFILES_ACTIVE docker,postgres

Custom metrics monitoring

Grafana and Prometheus are included in the docker-compose.yml configuration, and the public facing applications have been instrumented with MicroMeter to collect JVM and custom business metrics.

A JMeter load testing script is available to stress the application and generate metrics: petclinic_test_plan.jmx

Grafana metrics dashboard

Using Prometheus

Using Grafana with Prometheus

Custom metrics

Spring Boot registers a lot number of core metrics: JVM, CPU, Tomcat, Logback... The Spring Boot auto-configuration enables the instrumentation of requests handled by Spring MVC. All those three REST controllers OwnerResource, PetResource and VisitResource have been instrumented by the @Timed Micrometer annotation at class level.

  • customers-service application has the following custom metrics enabled:
    • @Timed: petclinic.owner
    • @Timed: petclinic.pet
  • visits-service application has the following custom metrics enabled:
    • @Timed: petclinic.visit

Looking for something in particular?

Spring Cloud components Resources
Configuration server Config server properties and Configuration repository
Service Discovery Eureka server and Service discovery client
API Gateway Spring Cloud Gateway starter and Routing configuration
Docker Compose Spring Boot with Docker guide and docker-compose file
Circuit Breaker Resilience4j fallback method
Grafana / Prometheus Monitoring Micrometer implementation, Spring Boot Actuator Production Ready Metrics
Front-end module Files
Node and NPM The frontend-maven-plugin plugin downloads/installs Node and NPM locally then runs Bower and Gulp
Bower JavaScript libraries are defined by the manifest file bower.json
Gulp Tasks automated by Gulp: minify CSS and JS, generate CSS from LESS, copy other static resources
Angular JS app.js, controllers and templates

Pushing to a Docker registry

Docker images for linux/amd64 and linux/arm64 platforms have been published into DockerHub in the springcommunity organization. You can pull an image:

docker pull springcommunity/spring-petclinic-config-server

You may prefer to build then push images to your own Docker registry.

Choose your Docker registry

You need to define your target Docker registry. Make sure you're already logged in by running docker login <endpoint> or docker login if you're just targeting Docker hub.

Setup the REPOSITORY_PREFIX env variable to target your Docker registry. If you're targeting Docker hub, simple provide your username, for example:

export REPOSITORY_PREFIX=springcommunity

For other Docker registries, provide the full URL to your repository, for example:

export REPOSITORY_PREFIX=harbor.myregistry.com/petclinic

To push Docker image for the linux/amd64 and the linux/arm64 platform to your own registry, please use the command line:

mvn clean install -Dmaven.test.skip -P buildDocker -Ddocker.image.prefix=${REPOSITORY_PREFIX} -Dcontainer.build.extraarg="--push" -Dcontainer.platform="linux/amd64,linux/arm64"

Be sure to turn on "Use containerd for pulling and storing images" in the Docker Desktop settings, when trying to do multi-platform builds.

The scripts/pushImages.sh and scripts/tagImages.sh shell scripts could also be used once you build your image with the buildDocker maven profile. The scripts/tagImages.sh requires to declare the VERSION env variable.

Interesting Spring Petclinic forks

The Spring Petclinic main branch in the main spring-projects GitHub org is the "canonical" implementation, currently based on Spring Boot and Thymeleaf.

This spring-petclinic-microservices project is one of the several forks hosted in a special GitHub org: spring-petclinic. If you have a special interest in a different technology stack that could be used to implement the Pet Clinic then please join the community there.

Contributing

The issue tracker is the preferred channel for bug reports, features requests and submitting pull requests.

For pull requests, editor preferences are available in the editor config for easy use in common text editors. Read more and download plugins at http://editorconfig.org.

InspectIT related Stuff

Soap Service Testing

For more information about testing the custom soap service and client, please refer to the README in the soap folder.

About

Distributed version of Spring Petclinic built with Spring Cloud

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 73.7%
  • HTML 9.2%
  • JavaScript 6.4%
  • Less 5.9%
  • Shell 2.9%
  • Dockerfile 1.9%