|
| 1 | +# Reactive Implementation of 🐈 Spring PetClinic 🐕 |
| 2 | +*15 minutes, Intermediate* |
| 3 | + |
| 4 | +This sample is a fully reactive version of the Spring PetClinic application using Spring WebFlux. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +The steps to run the application are provided below. You can run the application with no installation using Gitpod and Cassandra as DBaas for free or locally. To get started with a free-forever, zero-install Cassandra database **[click here](https://astra.datastax.com/register?utm_source=github&utm_medium=referral&utm_campaign=spring-petclinic-reactive)** 🚀 |
| 9 | + |
| 10 | + |
| 11 | +*Architecture overview* |
| 12 | + |
| 13 | +## Table of contents |
| 14 | + |
| 15 | +- [Run the application](#run-the-application) |
| 16 | +- [Understand the architecture](#understand-the-architecture) |
| 17 | +- [Contributing](#contributing) |
| 18 | + |
| 19 | +## Run the application |
| 20 | + |
| 21 | +### 1. Start the database |
| 22 | + |
| 23 | +**✅ Create a free-forever Cassandra database with DataStax Astra**: [click here to get started](https://astra.datastax.com/register?utm_source=github&utm_medium=referral&utm_campaign=spring-petclinic-reactive) 🚀 |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +**✅ Use the form to create a new database** |
| 30 | + |
| 31 | +On the Astra home page locate the **Add Database** button |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Select the **free tier** plan, this is a true free tier, free forever and no payment method asked 🎉 🎉 |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +Select the proper region and click the `configure` button. The number of regions and cloud providers are limited in the free tier but please notice you can run the DB on any cloud with any VPC Peering. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +Fill in the `database name`, `keyspace name`, `username` and `password`. *Please remember your password as you will be asked to provide it when the application starts the first time.* |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +**✅ View your Database and connect** |
| 48 | + |
| 49 | +View your database. It may take 2-3 minutes for your database to spin up. You will receive an email at that point. |
| 50 | + |
| 51 | +**👁️ Expected output** |
| 52 | + |
| 53 | +*Initializing* |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +Once the database is ready, notice how the status changes from `Pending` to `Active` and Astra enables the **CONNECT** button. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### 2. Copy credentials to connect |
| 62 | + |
| 63 | +**✅ Navigate to your credentials** |
| 64 | + |
| 65 | +Locate the combo `Organization: <Your email>` on the top navigation. On the right side of your organization, click the ellipsis (...) then click your `<Your email>`. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +You should land on the following screen. Scroll down to the bottom of the page to locate the `Service Account` in `Security Settings` |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +**✅ Create Service Account** |
| 74 | + |
| 75 | +Create a service account by clicking the `Add Service Account` button above the section as shown below |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +When the panel opens on the right, click `Add` |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +**✅ Copy credentials to your clipboard** |
| 84 | + |
| 85 | +Click the ellipsis at end of Service Account row to open menu and select `Copy Credentials` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +The credentials you copied to the clipboard look like the following JSON, we will use this in gitpod to enable connectivity. |
| 90 | +```json |
| 91 | +{ |
| 92 | + "clientId":"149de2c7-9b07-41b3-91ad-9453dee4dc54", |
| 93 | + "clientName":"cedrick.lunven@datastax.com", |
| 94 | + "clientSecret":"aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### 3. Start in Gitpod |
| 99 | + |
| 100 | +**✅ Open Gitpod (with creds copied to clipboard)** |
| 101 | + |
| 102 | +[](https://gitpod.io/#https://github.com/spring-petclinic/spring-petclinic-reactive) |
| 103 | + |
| 104 | +When you first launch gitpod, it builds the image. |
| 105 | + |
| 106 | + |
| 107 | +Git pod pulls the image. |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +**✅ Paste credentials in Gitpod terminal** |
| 113 | + |
| 114 | +Once Gitpod loads the workspace, you'll be asked to paste your service account credentials in the Gitpod terminal at the bottom of the screen. The [setup.sh](setup.sh) script at the root of the repository is what asks this question. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +**✅ Open Swagger UI in browser** |
| 119 | + |
| 120 | +When gitpod finishes building the app, a new tab will open in your browser showing the following. |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +**🎉 Celebrate!** |
| 125 | + |
| 126 | +You've successfully built the Spring Petclinic Reactive backend application! |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +**✅ Start the Web UI** : |
| 131 | + |
| 132 | +You may have noticed another terminal named `spring-petclinic-angular`. This is where the UI should start. |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +After answering the question about analytics usage, you should be able to access the UI on a new tab. |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +**NOTE** If you want to run everything locally, reference the [LOCAL_README.md](doc/LOCAL_README.md) |
| 141 | + |
| 142 | +## Understand the architecture |
| 143 | + |
| 144 | +### Internal Architecture our of component |
| 145 | + |
| 146 | +Let's have a look inside the main component `spring-petclinic-reactive` to see which libraries and frameworks have been used. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +- `Spring-boot`: Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration. |
| 151 | + |
| 152 | +- `Spring-Security`: Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements. |
| 153 | + |
| 154 | +- `Spring-WebFlux`: Spring sub framework allowing to create Reactive Rest Endpoint. |
| 155 | + |
| 156 | +- `Spring-Actuator`: Expose Endpoints to expose metrics to third party system: health, infos, jmx,prometheus,... |
| 157 | + |
| 158 | +- `Spring-Test`: Enabled unit testing and mocking with Spring configuration and beans. |
| 159 | + |
| 160 | +- `Spring-Cloud`: Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry. |
| 161 | + |
| 162 | +- `SpringFox` *(Swagger)*: Annotation based rest documentation generation and test client generation (swagger-ui). |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +### Logical Architecture |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +Here, you can find a description of the logical architecture components: |
| 172 | + |
| 173 | +- `spring-parclinic-angular`: This is the existing project that provides a user interface implementation using Angular. It has been used as well for other backend projects like the |
| 174 | +**spring-petclinic-rest** |
| 175 | + |
| 176 | +- `prometheus`: Our component exposes some metrics through the actuator endpoint. A registry will push this information into the Prometheus database (docker-based). |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +- `Grafana`: Allows to create dashboards based on data stored in prometheus. |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +- `zipkin`: Our component includes the `spring-cloud-sleuth` dependency allowing Brave to push metrics usage of the API to the distributed tracing component Zipkin. To enable this tracing |
| 185 | +set the property `zipkin.enabled` to true in `application.yaml`. |
| 186 | +To start zipkin use `docker-compose up -d` |
| 187 | + |
| 188 | +``` |
| 189 | + zipkin: |
| 190 | + enabled: true |
| 191 | + baseUrl: http://localhost:9411 |
| 192 | + sender: |
| 193 | + type: web |
| 194 | +``` |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | +- `Apache Cassandra`: A NoSQL database |
| 199 | + |
| 200 | +- `DataStax Astra` : Apache Cassandra available in the Cloud for free as a managed service (DBaas) |
| 201 | + |
| 202 | + |
| 203 | +### Data Model diagram |
| 204 | + |
| 205 | +The underlying data model implemented in Apache Cassandra is different from the one you would have defined with a relational database. |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +To enable scalability, Apache Cassandra does not support joins or integrity constraints. Therefore we used some denormalization. |
| 210 | +We also created some `secondary indices` to queries columns that are not the PARTITION KEY. These secondary indices work well in this case because the cardinality is low (e.g, few pets for an owner). |
| 211 | + |
| 212 | +The application generates the objects related to the data model (e.g., tables, indices, udts) at startup. |
| 213 | + |
| 214 | +## C. Contributing |
| 215 | + |
| 216 | + The [issue tracker](https://github.com/spring-petclinic/spring-petclinic-reactive/issues)is the preferred channel for bug reports, features requests and submitting pull requests. |
| 217 | + |
| 218 | +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. |
| 219 | + |
| 220 | + |
| 221 | + |
0 commit comments