mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-02-11 09:01:10 +00:00
updated Readme file
This commit is contained in:
parent
7106e4a41f
commit
8f56b455ac
1 changed files with 106 additions and 143 deletions
249
README.md
249
README.md
|
|
@ -1,174 +1,137 @@
|
|||
# Spring PetClinic Sample Application [](https://github.com/spring-projects/spring-petclinic/actions/workflows/maven-build.yml)[](https://github.com/spring-projects/spring-petclinic/actions/workflows/gradle-build.yml)
|
||||
# Feature Flag Enabled Spring PetClinic
|
||||
|
||||
[](https://gitpod.io/#https://github.com/spring-projects/spring-petclinic) [](https://github.com/codespaces/new?hide_repo_select=true&ref=main&repo=7517918)
|
||||
This project is an extension of the official **Spring PetClinic** application with a **custom-built Feature Flag service** implemented from scratch (without using libraries like FF4J or Togglz).
|
||||
|
||||
## Understanding the Spring Petclinic application with a few diagrams
|
||||
The feature flag system allows enabling/disabling application features dynamically using database-driven flags that persist across restarts.
|
||||
|
||||
See the presentation here:
|
||||
[Spring Petclinic Sample Application (legacy slides)](https://speakerdeck.com/michaelisvy/spring-petclinic-sample-application?slide=20)
|
||||
---
|
||||
|
||||
> **Note:** These slides refer to a legacy, pre–Spring Boot version of Petclinic and may not reflect the current Spring Boot–based implementation.
|
||||
> For up-to-date information, please refer to this repository and its documentation.
|
||||
|
||||
|
||||
## Run Petclinic locally
|
||||
|
||||
Spring Petclinic is a [Spring Boot](https://spring.io/guides/gs/spring-boot) application built using [Maven](https://spring.io/guides/gs/maven/) or [Gradle](https://spring.io/guides/gs/gradle/).
|
||||
Java 17 or later is required for the build, and the application can run with Java 17 or newer.
|
||||
|
||||
You first need to clone the project locally:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/spring-projects/spring-petclinic.git
|
||||
cd spring-petclinic
|
||||
```
|
||||
If you are using Maven, you can start the application on the command-line as follows:
|
||||
|
||||
```bash
|
||||
./mvnw spring-boot:run
|
||||
```
|
||||
With Gradle, the command is as follows:
|
||||
|
||||
```bash
|
||||
./gradlew bootRun
|
||||
```
|
||||
|
||||
You can then access the Petclinic at <http://localhost:8080/>.
|
||||
|
||||
<img width="1042" alt="petclinic-screenshot" src="https://cloud.githubusercontent.com/assets/838318/19727082/2aee6d6c-9b8e-11e6-81fe-e889a5ddfded.png">
|
||||
|
||||
You can, of course, run Petclinic in your favorite IDE.
|
||||
See below for more details.
|
||||
|
||||
## Building a Container
|
||||
|
||||
There is no `Dockerfile` in this project. You can build a container image (if you have a docker daemon) using the Spring Boot build plugin:
|
||||
|
||||
```bash
|
||||
./mvnw spring-boot:build-image
|
||||
```
|
||||
|
||||
## In case you find a bug/suggested improvement for Spring Petclinic
|
||||
|
||||
Our issue tracker is available [here](https://github.com/spring-projects/spring-petclinic/issues).
|
||||
|
||||
## Database configuration
|
||||
|
||||
In its default configuration, Petclinic uses an in-memory database (H2) which
|
||||
gets populated at startup with data. The h2 console is exposed at `http://localhost:8080/h2-console`,
|
||||
and it is possible to inspect the content of the database using the `jdbc:h2:mem:<uuid>` URL. The UUID is printed at startup to the console.
|
||||
|
||||
A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed. Note that whenever the database type changes, the app needs to run with a different profile: `spring.profiles.active=mysql` for MySQL or `spring.profiles.active=postgres` for PostgreSQL. See the [Spring Boot documentation](https://docs.spring.io/spring-boot/how-to/properties-and-configuration.html#howto.properties-and-configuration.set-active-spring-profiles) for more detail on how to set the active profile.
|
||||
|
||||
You can start MySQL or PostgreSQL locally with whatever installer works for your OS or use docker:
|
||||
|
||||
```bash
|
||||
docker run -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:9.5
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
docker run -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 postgres:18.1
|
||||
```
|
||||
|
||||
Further documentation is provided for [MySQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt)
|
||||
and [PostgreSQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt).
|
||||
|
||||
Instead of vanilla `docker` you can also use the provided `docker-compose.yml` file to start the database containers. Each one has a service named after the Spring profile:
|
||||
|
||||
```bash
|
||||
docker compose up mysql
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
docker compose up postgres
|
||||
```
|
||||
|
||||
## Test Applications
|
||||
|
||||
At development time we recommend you use the test applications set up as `main()` methods in `PetClinicIntegrationTests` (using the default H2 database and also adding Spring Boot Devtools), `MySqlTestApplication` and `PostgresIntegrationTests`. These are set up so that you can run the apps in your IDE to get fast feedback and also run the same classes as integration tests against the respective database. The MySql integration tests use Testcontainers to start the database in a Docker container, and the Postgres tests use Docker Compose to do the same thing.
|
||||
|
||||
## Compiling the CSS
|
||||
|
||||
There is a `petclinic.css` in `src/main/resources/static/resources/css`. It was generated from the `petclinic.scss` source, combined with the [Bootstrap](https://getbootstrap.com/) library. If you make changes to the `scss`, or upgrade Bootstrap, you will need to re-compile the CSS resources using the Maven profile "css", i.e. `./mvnw package -P css`. There is no build profile for Gradle to compile the CSS.
|
||||
|
||||
## Working with Petclinic in your IDE
|
||||
## How to Run the Application
|
||||
|
||||
### Prerequisites
|
||||
|
||||
The following items should be installed in your system:
|
||||
|
||||
- Java 17 or newer (full JDK, not a JRE)
|
||||
- [Git command line tool](https://help.github.com/articles/set-up-git)
|
||||
- Your preferred IDE
|
||||
- Eclipse with the m2e plugin. Note: when m2e is available, there is a m2 icon in `Help -> About` dialog. If m2e is
|
||||
not there, follow the installation process [here](https://www.eclipse.org/m2e/)
|
||||
- [Spring Tools Suite](https://spring.io/tools) (STS)
|
||||
- [IntelliJ IDEA](https://www.jetbrains.com/idea/)
|
||||
- [VS Code](https://code.visualstudio.com)
|
||||
- Java 17+
|
||||
- Maven 3.8+
|
||||
- Docker & Docker Compose
|
||||
|
||||
### Steps
|
||||
1. **Clone the repository**
|
||||
```bash
|
||||
git clone https://github.com/XTiNCT-7/spring-petclinic.git
|
||||
cd spring-petclinic
|
||||
```
|
||||
|
||||
1. On the command line run:
|
||||
2. **Start MySQL using Docker**
|
||||
```bash
|
||||
docker-compose up mysql
|
||||
```
|
||||
|
||||
```bash
|
||||
git clone https://github.com/spring-projects/spring-petclinic.git
|
||||
```
|
||||
3. **Build and run the application**
|
||||
```bash
|
||||
mvn clean spring-boot:run
|
||||
```
|
||||
|
||||
1. Inside Eclipse or STS:
|
||||
4. Access the application:
|
||||
- Application UI: `http://localhost:8080`
|
||||
- Feature Flag APIs: `http://localhost:8080/feature-flags`
|
||||
|
||||
Open the project via `File -> Import -> Maven -> Existing Maven project`, then select the root directory of the cloned repo.
|
||||
---
|
||||
|
||||
Then either build on the command line `./mvnw generate-resources` or use the Eclipse launcher (right-click on project and `Run As -> Maven install`) to generate the CSS. Run the application's main method by right-clicking on it and choosing `Run As -> Java Application`.
|
||||
## Assumptions & Design Decisions
|
||||
|
||||
1. Inside IntelliJ IDEA:
|
||||
- Feature flags are **stored in the database** and persist across application restarts.
|
||||
- No authentication is applied to feature flag management APIs (as per requirement).
|
||||
- Feature flag evaluation is **centralized in a helper service** so it can be reused across controllers, services, and views.
|
||||
- A **custom annotation (`@FeatureToggle`)** is used to guard controller endpoints.
|
||||
- Thymeleaf views are conditionally rendered using model attributes derived from feature flag checks.
|
||||
- Feature flag behavior supports more than boolean enable/disable:
|
||||
- Global enable/disable
|
||||
- Whitelist-based access
|
||||
- Blacklist-based restriction
|
||||
- Percentage rollout (future-safe design)
|
||||
|
||||
In the main menu, choose `File -> Open` and select the Petclinic [pom.xml](pom.xml). Click on the `Open` button.
|
||||
---
|
||||
|
||||
- CSS files are generated from the Maven build. You can build them on the command line `./mvnw generate-resources` or right-click on the `spring-petclinic` project then `Maven -> Generates sources and Update Folders`.
|
||||
## Feature Flags Implemented
|
||||
|
||||
- A run configuration named `PetClinicApplication` should have been created for you if you're using a recent Ultimate version. Otherwise, run the application by right-clicking on the `PetClinicApplication` main class and choosing `Run 'PetClinicApplication'`.
|
||||
| Feature Flag Key | Type | Controls | Implementation Location |
|
||||
|------------------|------|----------|--------------------------|
|
||||
| `ADD_NEW_PET` | SIMPLE | Enables adding a new pet to an owner | `PetController`, `ownerDetails.html` |
|
||||
| `ADD_VISIT` | SIMPLE | Enables adding a visit for a pet | `VisitController`, `ownerDetails.html` |
|
||||
| `OWNER_SEARCH` | SIMPLE | Enables owner search functionality | `OwnerController`, `findOwners.html` |
|
||||
|
||||
1. Navigate to the Petclinic
|
||||
### Example
|
||||
- If `ADD_NEW_PET` is disabled:
|
||||
- The "Add New Pet" button is hidden in the UI
|
||||
- Direct access to `/owners/{id}/pets/new` is blocked using `@FeatureToggle`
|
||||
|
||||
Visit [http://localhost:8080](http://localhost:8080) in your browser.
|
||||
---
|
||||
|
||||
## Looking for something in particular?
|
||||
## Feature Flag Management APIs
|
||||
|
||||
|Spring Boot Configuration | Class or Java property files |
|
||||
|--------------------------|---|
|
||||
|The Main Class | [PetClinicApplication](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java) |
|
||||
|Properties Files | [application.properties](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources) |
|
||||
|Caching | [CacheConfiguration](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java) |
|
||||
Base Path: `/feature-flags`
|
||||
|
||||
## Interesting Spring Petclinic branches and forks
|
||||
### Create Feature Flag
|
||||
```http
|
||||
POST /feature-flags
|
||||
```
|
||||
|
||||
The Spring Petclinic "main" branch in the [spring-projects](https://github.com/spring-projects/spring-petclinic)
|
||||
GitHub org is the "canonical" implementation based on Spring Boot and Thymeleaf. There are
|
||||
[quite a few forks](https://spring-petclinic.github.io/docs/forks.html) in the GitHub org
|
||||
[spring-petclinic](https://github.com/spring-petclinic). If you are interested in using a different technology stack to implement the Pet Clinic, please join the community there.
|
||||
**Request Body**
|
||||
```json
|
||||
{
|
||||
"flagKey": "ADD_NEW_PET",
|
||||
"description": "Controls whether users can add new pets",
|
||||
"enabled": true,
|
||||
"flagType": "SIMPLE"
|
||||
}
|
||||
```
|
||||
|
||||
## Interaction with other open-source projects
|
||||
---
|
||||
|
||||
One of the best parts about working on the Spring Petclinic application is that we have the opportunity to work in direct contact with many Open Source projects. We found bugs/suggested improvements on various topics such as Spring, Spring Data, Bean Validation and even Eclipse! In many cases, they've been fixed/implemented in just a few days.
|
||||
Here is a list of them:
|
||||
### Get All Feature Flags
|
||||
```http
|
||||
GET /feature-flags
|
||||
```
|
||||
|
||||
| Name | Issue |
|
||||
|------|-------|
|
||||
| Spring JDBC: simplify usage of NamedParameterJdbcTemplate | [SPR-10256](https://github.com/spring-projects/spring-framework/issues/14889) and [SPR-10257](https://github.com/spring-projects/spring-framework/issues/14890) |
|
||||
| Bean Validation / Hibernate Validator: simplify Maven dependencies and backward compatibility |[HV-790](https://hibernate.atlassian.net/browse/HV-790) and [HV-792](https://hibernate.atlassian.net/browse/HV-792) |
|
||||
| Spring Data: provide more flexibility when working with JPQL queries | [DATAJPA-292](https://github.com/spring-projects/spring-data-jpa/issues/704) |
|
||||
---
|
||||
|
||||
## Contributing
|
||||
### Get Feature Flag by Key
|
||||
```http
|
||||
GET /feature-flags/{flagKey}
|
||||
```
|
||||
|
||||
The [issue tracker](https://github.com/spring-projects/spring-petclinic/issues) is the preferred channel for bug reports, feature requests and submitting pull requests.
|
||||
---
|
||||
|
||||
For pull requests, editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at <https://editorconfig.org>. All commits must include a __Signed-off-by__ trailer at the end of each commit message to indicate that the contributor agrees to the Developer Certificate of Origin.
|
||||
For additional details, please refer to the blog post [Hello DCO, Goodbye CLA: Simplifying Contributions to Spring](https://spring.io/blog/2025/01/06/hello-dco-goodbye-cla-simplifying-contributions-to-spring).
|
||||
### Update Feature Flag
|
||||
```http
|
||||
PUT /feature-flags/{id}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Delete Feature Flag
|
||||
```http
|
||||
DELETE /feature-flags/{id}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Custom Annotation Usage
|
||||
|
||||
```java
|
||||
@FeatureToggle(
|
||||
key = "OWNER_SEARCH",
|
||||
disabledMessage = "Owner search is restricted",
|
||||
disabledRedirect = "/owners/find"
|
||||
)
|
||||
```
|
||||
|
||||
This annotation prevents access to the controller method when the feature is disabled and optionally redirects the user.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
- Spring PetClinic: https://github.com/spring-projects/spring-petclinic
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue