Git Product home page Git Product logo

sandraahlgrimm / apptemplates-microservices-spring-app-on-azurespringapps Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/apptemplates-microservices-spring-app-on-azurespringapps

0.0 1.0 0.0 12.88 MB

Microservices based spring boot app deployed on Azure Spring Apps

License: MIT License

Shell 6.82% JavaScript 5.13% Java 52.40% HTML 6.77% HCL 9.31% Less 4.67% Bicep 14.90%

apptemplates-microservices-spring-app-on-azurespringapps's Introduction

Spring Boot PetClinic Microservices Application Deployed to Azure Spring Apps

This example shows you how to deploy an existing Java Spring Boot/Cloud application to Azure Spring Apps. When you're finished, you can continue to manage the application with Azure services via the Azure CLI, Bicep templates or switch to using the Azure Portal.

Refer to the App Templates repo Readme for more samples that are compatible with azure dev cli.

Features

This project provides the following features:

  • Build existing Spring Boot/Cloud applications
  • Provision Azure Spring Apps service and Azure Services using Bicep template from commandline or/and portal. See what is Bicep
  • Deploy applications to Azure
  • Bind applications to Azure services
  • Open and test the applications
  • Monitor the applications

Architecture and Azure Services

Components and Elements

  • All app components communicate with KeyVaults to retrieve sensitive information stored in secrets
  • All app components bound to Azure Files as permanent storage
  • All app components with data access gets second level cache on Redis
  • All components bound to Log Analytics to send logs and metrics
  • Endpoints to admin-server and api-gateway for external access
  • config-server in Azure Spring Apps gets the app configs from repos on GitHub with authentication
  • CI/CD by GitHub Actions are added with appropriate authentication to access the resources on Azure

Getting Started

Prerequisites

Installation

Before you start, install/update the Azure Spring Apps extension for the Azure CLI using following commands

az extension add --name spring

Or

az extension update --name spring

Quickstart

  1. Create a new folder and clone the repositories to your environment
mkdir [your source code folder name]
cd [your source code folder name]
git clone [your source code repo URL, or https://github.com/Azure-Samples/apptemplates-microservices-spring-app-on-AzureSpringApps]
git clone [your config repo URL, or https://github.com/euchungmsft/spring-petclinic-microservices-config]
  1. Change directory and build the project.
cd apptemplates-microservices-spring-app-on-AzureSpringApps
mvn clean package -DskipTests -Denv=cloud
  1. Config your project
mv .env-sample .env

Open .env with your text editor and update PROJECT_NAME, SUBSCRIPTION, SP_NAME, MY_UPN at least and REGION optionally only if necessary. PROJECT_NAME is prefix for all resources to be created. SUBSCRIPTION is your Azure Subscription ID, SP_NAME is Service Principal name to be created, MY_UPN is your Azure login id in email address format

PROJECT_NAME=[your project name, to give names to all resoures]

RESOURCE_GROUP=${PROJECT_NAME}-rg
REGION=westus
..
..
  1. Login to Azure from CLI
az login --use-device-code
  1. Create project resources
bin/init.sh

Open .env with your text editor and update AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID and AZURE_OBJECT_ID as given by the output of the init.sh script.

  1. Load all required variables and set defaults in your environment
source ./.env
az account set --subscription ${SUBSCRIPTION}
  1. Provision infrastructure components and Azure Spring Apps
az deployment group create \
 -g $RESOURCE_GROUP \
 -f iac/bicep/spc-main.bicep \
 --parameters location=$REGION \
  projectName=$PROJECT_NAME
  1. Provision Azure services
az deployment group create \
 -g $RESOURCE_GROUP \
 -f iac/bicep/services-main.bicep \
 --parameters location=$REGION \
  projectName=$PROJECT_NAME \
  database_name=$MYSQL_DATABASE_NAME \
  mysql_adminUsername=$MYSQL_SERVER_ADMIN_NAME \
  mysql_adminPassword=$MYSQL_SERVER_ADMIN_PASSWORD \
  servicePrincipal="{ \"clientId\": \"$AZURE_CLIENT_ID\", 
  \"tenantId\": \"$AZURE_TENANT_ID\", 
  \"objectId\": \"$AZURE_OBJECT_ID\" }"
  1. Config Log Analytics
bin/loganalytics-diagnostics.sh

To add Application Insights in your environment, on your portal, select your spring apps instance, click on Application Insights blade on the left, check on 'Enable Application Insights', Click on 'Create New' link, give it a new name for your App Insights instance.

Application Insights creation

  1. Give permission to access KeyVault secrets
az keyvault set-policy --name $KEY_VAULT \
--upn $MY_UPN \
--secret-permissions all

az keyvault set-policy --name $KEY_VAULT \
--object-id $AZURE_OBJECT_ID \
--secret-permissions all
  1. Initialize MySQL
mysql -u ${MYSQL_SERVER_ADMIN_LOGIN_NAME} \
 -h ${MYSQL_SERVER_FULL_NAME} \
 -P 3306 "--password=$MYSQL_SERVER_ADMIN_PASSWORD" \
 -e "-- CREATE DATABASE $MYSQL_DATABASE_NAME;
CREATE USER 'root' IDENTIFIED BY '$MYSQL_DATABASE_NAME';
GRANT ALL PRIVILEGES ON petclinic.* TO 'root';
CALL mysql.az_load_timezone();
SELECT name FROM mysql.time_zone_name;
quit
"
  1. Adding Storage Account
KEY0=$(az storage account keys list -g $RESOURCE_GROUP --account-name $STORAGE_ACCOUNT_NAME | jq -r '.[0].value')
echo $KEY0
az spring storage add \
 --storage-type StorageAccount \
 --account-key $KEY0 \
 --account-name $STORAGE_ACCOUNT_NAME \
 --name $SHARE_NAME \
 --service $SPRING_CLOUD_SERVICE
  1. Config your PetClinic App

Open application.yml in spring-petclinic-microservices-config repo

Update all resource names with your project name and all values under spring.cloud.azure.eventhub

spring:
  config:
    activate:
      on-profile: mysql
...
  cloud:
    azure:
      eventhub:
        connection-string: <your event hub connection string>
        checkpoint-storage-account: <your storage account name for checkpoint>
        checkpoint-access-key: <your storage account access key for checkpoint>
        checkpoint-container: <your blob container name for checkpoint>
        namespace: <your event hub namespace>
    stream:
      function:
        definition: consume;supply
      bindings:
        consume-in-0:
          destination: <your event hub name>
          group: $Default
        supply-out-0:
          destination: <your event hub name>

Commit and push the changes

git remote add origin https://github.com/<your gh account name>/spring-petclinic-microservices-config
git push -u origin master
  1. Config your Azure Spring App

Back to spring-petclinic-microservices, make a copy application.yml.sample to application.yml

cp application.yml.sample application.yml

Customize application.yml

spring:
  cloud:
    config:
      server:
        git:
          uri: [your config repo url]
          username: [your login id at GitHub]
          password: [your developer token at GitHub]
          label: main
        native:
          search-locations: classpath:.
  profiles:
    active: native 

Your developer token is from your GitHub account settings. See this for further details

Add app configs to your Azure Spring Apps

az spring config-server set \
 --config-file application.yml \
 --name ${SPRING_CLOUD_SERVICE}
  1. Add cache config
KEY0=`az redis list-keys --name ${PROJECT_NAME}-redis --resource-group ${RESOURCE_GROUP} | jq -r .primaryKey`
echo "{\"singleServerConfig\":{\"address\": \"redis://${PROJECT_NAME}-redis.redis.cache.windows.net:6379\", \"password\": \"$KEY0\"}}" > redisson.json
cat redisson.json

cp redisson.json spring-petclinic-${CUSTOMERS_SERVICE}/src/main/resources/
cp redisson.json spring-petclinic-${VETS_SERVICE}/src/main/resources/
cp redisson.json spring-petclinic-${VISITS_SERVICE}/src/main/resources/ 
  1. Build your app
mvn clean package -DskipTests -Denv=cloud
  1. Create apps
bin/spring-apps.sh create api-gateway
bin/spring-apps.sh create admin-server
bin/spring-apps.sh create customers-service
bin/spring-apps.sh create vets-service
bin/spring-apps.sh create visits-service
bin/spring-apps.sh create consumer-service
  1. Append storage to the apps
bin/spring-apps.sh append-persistent-storage customers-service
bin/spring-apps.sh append-persistent-storage vets-service
bin/spring-apps.sh append-persistent-storage visits-service
bin/spring-apps.sh append-persistent-storage consumer-service
  1. Deploy apps
bin/spring-apps.sh deploy api-gateway
bin/spring-apps.sh deploy admin-server
bin/spring-apps.sh deploy customers-service
bin/spring-apps.sh deploy vets-service
bin/spring-apps.sh deploy visits-service
bin/spring-apps.sh deploy consumer-service
  1. Browse logs
bin/spring-apps.sh logs api-gateway
bin/spring-apps.sh logs admin-server
bin/spring-apps.sh logs customers-service
bin/spring-apps.sh logs vets-service
bin/spring-apps.sh logs visits-service
bin/spring-apps.sh logs consumer-service

Demo

To run the demo, follow these steps:

  1. Demo deployments

Spring Cloud Apps

  1. Demo PetClinic app

Open PetClinic from your Browser

https://[your project name]-springcloud-api-gateway.azuremicroservices.io

api-gateway overview

Navigate from menu on the top

Petclinic Main Petclinic Customers Petclinic Visits Petclinic Vets

  1. Demo Azure Monitor

Open your Application Insights instance

Application Insights Overview

Navigate to the Performance blade

Application Insights Performance, Operations

Click on Dependencies, you can see the performance number for dependencies to the services

Application Insights Performance, Dependencies

Click on Roles, you can see the performance number to compare across instances and roles

Application Insights Performance, Roles

Navigate to the Application Map blade

Application Insights Application Map

Navigate to the Failures blade

Application Insights Failures, Dependencies

Click on Exceptions, you can see a collection of exceptions

Application Insights Failures, Exceptions

Click on an exception to see the end-to-end transaction and stacktrace in context

Application Insights Failures, Exceptions Details

Navigate to the Metrics blade

Application Insights Metrics

Navigate to Live Metrics blade, you can see live metrics on screen with low latencies < 1 second Application Insights Live Metrics

  1. Demo API test from command line

For the app services

curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/owners
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/owners/4
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/owners/ 
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/petTypes
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/owners/3/pets/4
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/owners/6/pets/8/
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/vet/vets
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/visit/owners/6/pets/8/visits
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/visit/owners/6/pets/8/visits

For the Actuators

curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/actuator/
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/actuator/env
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/actuator/configprops
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/actuator
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/actuator/env
curl -X GET https://${SPRING_CLOUD_SERVICE}-${API_GATEWAY}.azuremicroservices.io/api/customer/actuator/configprops

Resources

  • document for instructions for CI/CD

  • document for instructions with all details

  • document for APIM integration with Swagger/OAS3

  • document for managed testing pm Azure Load Test

  • document for CI/CD on GitHub Actions in detail

apptemplates-microservices-spring-app-on-azurespringapps's People

Contributors

aarthiem avatar euchungmsft avatar microsoftopensource avatar sandraahlgrimm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.