Skip to content

Commit

Permalink
Refactor sample_spring-petclinic-microservices. (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
hui1110 authored Feb 21, 2022
1 parent 9295749 commit e9f13e0
Show file tree
Hide file tree
Showing 28 changed files with 551 additions and 573 deletions.
154 changes: 118 additions & 36 deletions spring-petclinic-microservices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@ You will:

## What you will need

In order to deploy a Java app to cloud, you need an Azure subscription. If you do not already have
an Azure subscription, you can activate your
[MSDN subscriber benefits](https://azure.microsoft.com/pricing/member-offers/msdn-benefits-details/)
or sign up for a
[free Azure account]((https://azure.microsoft.com/free/)).

In addition, you will need the following:

- [Azure CLI version 2.0.67 or higher](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest)
- [Java 8](https://www.azul.com/downloads/azure-only/zulu/?version=java-8-lts&architecture=x86-64-bit&package=jdk)
- [Maven](https://maven.apache.org/download.cgi)
- [An Azure subscription](https://azure.microsoft.com/free/)
- [Terraform](https://www.terraform.io/)
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later
- [MySQL CLI](https://dev.mysql.com/downloads/shell/)
- [Git](https://git-scm.com/)
- [Jq](https://stedolan.github.io/jq/)

- [Maven](https://maven.apache.org/download.cgi)
- You can also import the code straight into your IDE:
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download)

## Clone and build the repo

### Clone the sample app repository
Expand All @@ -50,44 +45,113 @@ In addition, you will need the following:

This will take a few minutes.

## Provision Azure resources using Azure CLI
## Provision Azure Resources Required to Run This Sample

### Login to Azure
### Authenticate Using the Azure CLI
Terraform must authenticate to Azure to create infrastructure.

Login to the Azure CLI and choose your active subscription. Be sure to choose the active
subscription that is whitelisted for Azure Spring Cloud
In your terminal, use the Azure CLI tool to setup your account permissions locally.

```bash
az login
az account list -o table
az account set --subscription ${SUBSCRIPTION}
```shell
az login
```

### Prepare your environment for deployments
Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use.

Create a bash script with environment variables by making a copy of the supplied template:
```shell
You have logged in. Now let us find all the subscriptions to which you have access...

[
{
"cloudName": "AzureCloud",
"homeTenantId": "home-Tenant-Id",
"id": "subscription-id",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-Name",
"state": "Enabled",
"tenantId": "0envbwi39-TenantId",
"user": {
"name": "[email protected]",
"type": "user"
}
}
]
```

```bash
cp .scripts/setup-env-variables-azure-template.sh .scripts/setup-env-variables-azure.sh
If you have more than one subscription, specify the subscription-id you want to use with command below:
```shell
az account set --subscription <your-subscription-id>
```

Open `.scripts/setup-env-variables-azure.sh` and enter the following information:
### Provision the Resources

```bash
After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.

#### Run with Bash

```shell
# In the root directory of the sample
# Initialize your Terraform configuration
terraform -chdir=./terraform init

# Apply your Terraform Configuration
terraform -chdir=./terraform apply -auto-approve

export RESOURCE_GROUP=resource-group-name # customize this
export LOCATION=SouthCentralUS #customize this
export COSMOSDB_NAME=mycosmosdbaccname # customize this
export REDIS_NAME=myredisname #customize this
export KEYVAULT_NAME=myend2endkv #customize this
export APP_NAME_FOR_KEYVAULT=myappforkeyvault #customize this

```

Then, set the environment:
#### Run with Powershell

```bash
source .scripts/setup-env-variables-azure.sh
```shell
# In the root directory of the sample
# Initialize your Terraform configuration
terraform -chdir=terraform init

# Apply your Terraform Configuration
terraform -chdir=terraform apply -auto-approve

```

It may take a few minutes to run the script. After successful running, you will see prompt information like below:

```shell
azurecaf_name.resource_group: Creating...
azurecaf_name.redis: Creating...
azurecaf_name.service-principal: Creating...
azurecaf_name.cosmos: Creating...
azurecaf_name.kv: Creating...
...
azurerm_resource_group.main: Creating...
...
azurerm_key_vault.kv_account: Creating...
azurerm_cosmosdb_account.application: Creating...
azurerm_key_vault.kv_account: Still creating... [10s elapsed]
...

Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:
...
```

You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created.

### Export Output to Your Local Environment

#### Run with Bash

Running the command below to export environment values:

```shell
source ./terraform/setup_env.sh
```

#### Run with Powershell

Running the command below to export environment values:

```shell
. terraform\setup_env.ps1
```

make sure keyvault.env file is created at the root of the repo.
Expand All @@ -113,6 +177,24 @@ Containers startup order is coordinated with [`dockerize` script](https://github
Cloud Gateway timeouts.
You can track services availability using Eureka dashboard available by default at `http://localhost:8761`.

## Clean Up Resources
After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing.

The terraform destroy command terminates resources managed by your Terraform project.
To destroy the resources you created.

### Run with Bash

```shell
az group delete --name $(terraform -chdir=./terraform output -raw resource_group_name)
```

### Run with Powershell

```shell
az group delete --name $(terraform -chdir=terraform output -raw resource_group_name)
```

## Understanding the Spring Petclinic application

[See the presentation of the Spring Petclinic Framework version](http://fr.slideshare.net/AntoineRey/spring-framework-petclinic-sample-application)
Expand Down
70 changes: 42 additions & 28 deletions spring-petclinic-microservices/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-samples</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-microservices</artifactId>
<version>2.3.6</version>
<version>1.0.0</version>

<name>Spring PetClinic Microservices</name>
<packaging>pom</packaging>
Expand All @@ -26,41 +27,54 @@
<module>spring-petclinic-api-gateway</module>
</modules>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<docker.image.prefix>springcommunity</docker.image.prefix>
<docker.image.exposed.port>9090</docker.image.exposed.port>
<docker.image.dockerfile.dir>${basedir}</docker.image.dockerfile.dir>
<docker.image.dockerize.version>v0.6.1</docker.image.dockerize.version>
<docker.plugin.version>1.2.2</docker.plugin.version>
<spring-boot-admin.version>2.5.5</spring-boot-admin.version>
<spring-cloud-azure.version>4.0.0-beta.3</spring-cloud-azure.version>
<azure-spring-data-cosmos.version>3.16.0</azure-spring-data-cosmos.version><!-- {x-version-update;com.azure:azure-spring-data-cosmos;dependency} -->
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angularjs</artifactId>
<version>${webjars-angular.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>${webjars-jquery.version}</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-spring-data-cosmos</artifactId>
<version>${azure-spring-data-cosmos.version}</version>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${webjars-bootstrap.version}</version>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId>
<version>${spring-cloud-azure.version}</version>
<groupId>org.webjars</groupId>
<artifactId>angular-ui-router</artifactId>
<version>${webjars-angular-ui-router.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<properties>
<docker.image.prefix>springcommunity</docker.image.prefix>
<docker.image.exposed.port>9090</docker.image.exposed.port>
<docker.image.dockerfile.dir>${basedir}</docker.image.dockerfile.dir>
<docker.image.dockerize.version>v0.6.1</docker.image.dockerize.version>
<docker.plugin.version>1.2.2</docker.plugin.version>
<spring-boot-admin.version>2.5.5</spring-boot-admin.version>
<webjars-bootstrap.version>3.3.7-1</webjars-bootstrap.version>
<webjars-jquery.version>3.1.1-1</webjars-jquery.version>
<webjars-angular.version>1.6.4</webjars-angular.version>
<webjars-angular-ui-router.version>1.0.3</webjars-angular-ui-router.version>
</properties>

<repositories>
<repository>
<id>spring-milestones</id>
Expand Down
Loading

0 comments on commit e9f13e0

Please sign in to comment.