Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

spring-petclinic-microservices

> 后端框架
Open source

Distributed version of Spring Petclinic built with Spring Cloud

2.2K stars0 likes0 views
WebsiteGitHub

About

Distributed version of Spring Petclinic built with Spring Cloud

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

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 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:

  • Discovery Server - http://localhost:8761
  • Config Server - http://localhost:8888
  • AngularJS frontend (API Gateway) - http://localhost:8080
  • Customers, Vets, Visits and GenAI Services - random port, check Eureka Dashboard
  • Tracing Server (Zipkin) - http://localhost:9411/zipkin/ (we use openzipkin)
  • Admin Server (Spring Boot Admin) - http://localhost:9090
  • Grafana Dashboards - http://localhost:3030
  • Prometheus - http://localhost:9091

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

In order to start entire infrastructure using Docker, you have to build images by executing 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 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/

Microservices Overview

This project consists of several microservices:

  • Customers Service: Manages customer data.
  • Vets Service: Handles information about veterinarians.
  • Visits Service: Manages pet visit records.
  • GenAI Service: Provides a chatbot interface to the application.
  • API Gateway: Routes client requests to the appropriate services.
  • Config Server: Centralized configuration management for all services.
  • Discovery Server: Eureka-based service registry.

Each service has its own specific role and communicates via REST APIs.

Architecture diagram of the Spring Petclinic Microservices

Integrating the Spring AI Chatbot

Spring Petclinic integrates a Chatbot that allows you to interact with the application in a natural language. Here are some examples of what you could ask:

  1. Please list the owners that come to the clinic.
  2. Are there any vets that specialize in surgery?
  3. Is there an owner named Betty?
  4. Which owners have dogs?
  5. Add a dog for Betty. Its name is Moopsie.
  6. Create a new owner.

This spring-petclinic-genai-service microservice currently supports OpenAI (default) or Azure's OpenAI as the LLM provider. In order to start the microservice, perform the following steps:

  1. Decide which provider you want to use. By default, the spring-ai-starter-model-openai dependency is enabled. You can change it to spring-ai-starter-model-azure-openaiin the pom.xml.
  2. Create an OpenAI API key or an Azure OpenAI resource in your Azure Portal. Refer to the OpenAI's quickstart or Azure's documentation for further information on how to obtain these. You only need to populate the provider you're using - either openai, or azure-openai. If you don't have your own OpenAI API key, don't worry! You can temporarily use the demo key, which OpenAI provides free of charge for demonstration purposes. This demo key has a quota, is limited to the gpt-4o-mini model, and is intended solely for demonstration use. With your own OpenAI account, you can test the gpt-4o model by modifying the deployment-name property of the application.yml file.
  3. Export your API keys and endpoint as environment variables:
    • either OpenAI:
    export OPENAI_API_KEY="your_api_key_here"
    
    • or Azure OpenAI:
    export AZURE_OPENAI_ENDPOINT="https://your_resource.openai.azure.com"
    export AZURE_OPENAI_KEY="your_api_key_here"
    

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 MySql in case a persistent database configuration is needed. Dependency for Connector/J, the MySQL JDBC driver is already included in the pom.xml files.

Start a MySql database

You may start a MySql database with docker:

docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:8.4.5

or download and install the MySQL database (e.g., MySQL Community Server 8.4.5 LTS), which can be found here: https://dev.mysql.com/downloads/

Use the Spring 'mysql' profile

To use a MySQL database, you have to start 3 microservices (visits-service, customers-service and vets-services) with the mysql Spring profile. Add the --spring.profiles.active=mysql as program 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/mysql/{schema,data}.sql" scripts of each 3 microservices. In the application.yml of the [Configuration repository], set the initialization-mode to never.

If you are running the microservices with Docker, you have to add the mysql profile into the Dockerfile:

ENV SPRING_PROFILES_ACTIVE docker,mysql

In the mysql section of the application.yml from the [Configuration repository], you have to change the host and port of your MySQL JDBC connection string.

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

Using Prometheus

  • Prometheus can be accessed from your local machine at http://localhost:9091

Using Grafana with Prometheus

  • An anonymous access and a Prometheus datasource are setup.
  • A Spring Petclinic Metrics Dashboard is available at the URL http://localhost:3030/d/69JXeR0iw/spring-petclinic-metrics. You will find the JSON configuration file here: docker/grafana/dashboards/grafana-petclinic-dashboard.json.
  • You may create your own dashboard or import the Micrometer/SpringBoot dashboard via the Import Dashboard menu item. The id for this dashboard is 4701.

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 [M

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •Discovery Server - http://localhost:8761
  • •Config Server - http://localhost:8888
  • •AngularJS frontend (API Gateway) - http://localhost:8080
  • •Customers, Vets, Visits and GenAI Services - random port, check Eureka Dashboard
  • •Tracing Server (Zipkin) - http://localhost:9411/zipkin/ (we use openzipkin)
  • •Admin Server (Spring Boot Admin) - http://localhost:9090
  • •Grafana Dashboards - http://localhost:3030
  • •Prometheus - http://localhost:9091
  • •Customers Service: Manages customer data.
  • •Vets Service: Handles information about veterinarians.

> Tags

Javachaos-monkeydockereurekagrafana

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架