From eb58e8c65f9e090ab702bec6ad27d84dd6ee135c Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Tue, 25 Jan 2022 11:29:08 +0800 Subject: [PATCH] Update redis kafka sample (#165) --- .../spring-cloud-azure-sample-cache/README.md | 160 ++++++++++++------ .../spring-cloud-azure-sample-cache/pom.xml | 2 +- .../src/main/resources/application.yaml | 10 +- .../terraform/main.tf | 58 +++++++ .../terraform/outputs.tf | 14 ++ .../terraform/setup_env.sh | 3 + .../terraform/variables.tf | 17 ++ .../README.md | 2 +- pom.xml | 1 + 9 files changed, 202 insertions(+), 65 deletions(-) create mode 100644 cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/main.tf create mode 100644 cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/outputs.tf create mode 100644 cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/setup_env.sh create mode 100644 cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/variables.tf diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md index 90edf29c7..fbc6a54ee 100644 --- a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md @@ -1,86 +1,134 @@ # Spring Cloud Azure Sample redis cache -## Key concepts - Redis support is based on spring-boot-starter-data-redis, which obtains and Automatic configuration of redis properties through Azure Redis Cache Management SDK. +## What You Will Build +You will build an application using the Spring Boot Starter Redis, Spring Cloud Azure Starter and Spring Cloud Azure Resource Manager to cache data to Azure Cache for Redis. -## Getting started +## What You Need -Running this sample will be charged by Azure. You can check the usage and bill at [this link][azure-account]. +- [An Azure subscription](https://azure.microsoft.com/free/) +- [Terraform](https://www.terraform.io/) +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) +## Provision Azure Resources Required to Run This Sample +This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: +> [!IMPORTANT] +> If you choose to use a security principal to authenticate and authorize with Azure Active Directory for accessing an Azure resource +> please refer to [Authorize access with Azure AD](https://microsoft.github.io/spring-cloud-azure/docs/current/reference/html/index.html#authorize-access-with-azure-active-directory) to make sure the security principal has been granted the sufficient permission to access the Azure resource. +### Authenticate Using the Azure CLI +Terraform must authenticate to Azure to create infrastructure. -### Coordinates +In your terminal, use the Azure CLI tool to setup your account permissions locally. -Maven coordinates: +```shell +az login +``` -```xml - - com.azure.spring - spring-cloud-azure-starter - - - com.azure.spring - spring-cloud-azure-resourcemanager - - - org.springframework.boot - spring-boot-starter-data-redis - +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. + +```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": "your-username@domain.com", + "type": "user" + } + } +] ``` -### Create an Azure Cache for Redis instance +If you have more than one subscription, specify the subscription-id you want to use with command below: +```shell +az account set --subscription +``` + +### Provision the Resources + +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. -1. Create a service principal for use in by your app. Please follow [create service principal from Azure CLI][create-sp-using-azure-cli]. +Now you can use the terraform script to create Azure Resources. -1. Create an Azure Cache for Redis instance. Please follow [create-azure-cache-for-redis]. +```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 +``` -## Examples +It may take around 15 minutes to run the script. After successful running, you will see prompt information like below: -1. Update `src/main/resources/application.yaml` to specify - resource group, service principal, and cache instance name: +```shell - ```yaml - spring: - cloud: - azure: - credential: - client-id: [service-principal-id] - client-secret: [service-principal-secret] - profile: - tenant-id: [tenant-id] - subscription-id: [subscription-id] - redis: - name: [azure-cache-for-redis-instance-name] - resource: - resource-group: [resource-group] - ``` - > :notes: Please note that currently we do not support the automatic creation of Azure Cache resources. - -1. Run the application using the `$ mvn spring-boot:run` command. +azurecaf_name.resource_group: Creating... +azurecaf_name.azurecaf_name_redis: Creating... +azurecaf_name.azurecaf_name_redis: Creation complete after 0s ... +azurerm_redis_cache.redis: Still creating... +azurerm_redis_cache.redis: Still creating... +azurerm_redis_cache.redis: Creation complete after ... -1. Send a GET request to check, where `name` could be any string: +Apply complete! Resources: 4 added, 0 changed, 0 destroyed. - $ curl -XGET http://localhost:8080/{name} +Outputs: +... -1. Confirm from Azure Redis Cache console in Azure Portal +``` - $ keys * +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. -1. Delete the resources on [Azure Portal][azure-portal] to avoid unexpected charges. +### Export Output to Your Local Environment +Running the command below to export environment values: +```shell +source ./terraform/setup_env.sh +``` -## Troubleshooting +## Run Locally -## Next steps +In your terminal, run `mvn clean spring-boot:run`. + +```shell +mvn clean spring-boot:run +``` -## Contributing +## Verify This Sample - -[azure-account]: https://azure.microsoft.com/account/ -[azure-portal]: https://ms.portal.azure.com/ -[create-azure-cache-for-redis]: https://docs.microsoft.com/azure/azure-cache-for-redis/quickstart-create-redis -[create-sp-using-azure-cli]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/main/create-sp-using-azure-cli.md +1. Send a GET request to check, where `name` could be any string: + +```shell +$ curl -XGET http://localhost:8080/{name} +``` + +2. Confirm from Azure Redis Cache console in Azure Portal: + +```shell +$ keys * +``` + +## 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. + +```shell +terraform -chdir=./terraform destroy -auto-approve +``` diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml index b08a78cdf..363d155df 100644 --- a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml @@ -11,7 +11,7 @@ ../../../pom.xml - spring-cloud-azure-cache-sample + spring-cloud-azure-sample-cache 1.0.0 jar diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/src/main/resources/application.yaml b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/src/main/resources/application.yaml index 9e20abacd..f67c6eec9 100644 --- a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/src/main/resources/application.yaml +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/src/main/resources/application.yaml @@ -1,13 +1,9 @@ spring: cloud: azure: - credential: - client-id: [service-principal-id] - client-secret: [service-principal-secret] profile: - tenant-id: [tenant-id] - subscription-id: [subscription-id] + subscription-id: ${AZURE_SUBSCRIPTION_ID} redis: - name: [azure-cache-for-redis-instance-name] + name: ${AZURE_CACHE_REDIS_NAME} resource: - resource-group: [resource-group] + resource-group: ${AZURE_RESOURCE_GROUP} diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/main.tf b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/main.tf new file mode 100644 index 000000000..cd02418cb --- /dev/null +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/main.tf @@ -0,0 +1,58 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">= 2.75" + } + azurecaf = { + source = "aztfmod/azurecaf" + version = "1.2.10" + } + } +} + +provider "azurerm" { + features {} +} + +resource "azurecaf_name" "resource_group" { + name = var.application_name + resource_type = "azurerm_resource_group" + random_length = 5 + clean_input = true +} + +resource "azurerm_resource_group" "main" { + name = azurecaf_name.resource_group.result + location = var.location + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +# =================== redis ================ +data "azurerm_subscription" "current" { } + +resource "azurecaf_name" "azurecaf_name_redis" { + name = var.application_name + resource_type = "azurerm_redis_cache" + random_length = 5 + clean_input = true +} + +resource "azurerm_redis_cache" "redis" { + name = azurecaf_name.azurecaf_name_redis.result + location = var.location + resource_group_name = azurerm_resource_group.main.name + capacity = 0 + family = "C" + sku_name = "Basic" + enable_non_ssl_port = false + minimum_tls_version = "1.2" + + redis_configuration { + } +} diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/outputs.tf b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/outputs.tf new file mode 100644 index 000000000..3bf452c06 --- /dev/null +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/outputs.tf @@ -0,0 +1,14 @@ +output "AZURE_SUBSCRIPTION_ID" { + value = data.azurerm_subscription.current.subscription_id + description = "The subscription ID of the resource." +} + +output "AZURE_RESOURCE_GROUP" { + value = azurerm_redis_cache.redis.resource_group_name + description = "The resource group name of the resource." +} + +output "AZURE_CACHE_REDIS_NAME" { + value = azurerm_redis_cache.redis.name + description = "The DNS name of the Redis instance." +} diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/setup_env.sh b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/setup_env.sh new file mode 100644 index 000000000..e27af3444 --- /dev/null +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/setup_env.sh @@ -0,0 +1,3 @@ +export AZURE_SUBSCRIPTION_ID=$(terraform -chdir=./terraform output -raw AZURE_SUBSCRIPTION_ID) +export AZURE_RESOURCE_GROUP=$(terraform -chdir=./terraform output -raw AZURE_RESOURCE_GROUP) +export AZURE_CACHE_REDIS_NAME=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_NAME) diff --git a/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/variables.tf b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/variables.tf new file mode 100644 index 000000000..d58fa546b --- /dev/null +++ b/cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/variables.tf @@ -0,0 +1,17 @@ +variable "application_name" { + type = string + description = "The name of your application." + default = "redis-sample" +} + +variable "location" { + type = string + description = "The Azure region where all resources in this example should be created." + default = "eastus" +} + +variable "sample_tag_value" { + type = string + description = "The value of spring-cloud-azure-sample tag." + default = "true" +} diff --git a/eventhubs/spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka/README.md b/eventhubs/spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka/README.md index efc8e0c17..0a54a5cac 100644 --- a/eventhubs/spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka/README.md +++ b/eventhubs/spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka/README.md @@ -1,6 +1,6 @@ # Spring Cloud Azure Sample Stream Event Hubs Kafka -This sample demonstrates how to use the Spring Cloud Azure Starter and Spring Cloud Starter Stream Kafka for Azure Event Hubs. The sample app exposes a RESTful API to receive +This sample demonstrates how to use the Spring Cloud Azure Starter and Spring Cloud Starter Stream Kafka for Azure Event Hubs ([Basic pricing tier is not supported](https://azure.microsoft.com/pricing/details/event-hubs/#explore-pricing-options)). The sample app exposes a RESTful API to receive string message. Then message is sent through Azure Event Hubs to a bean `consumer` which simply logs the message. diff --git a/pom.xml b/pom.xml index fba1d51f0..60bc10fa0 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ appconfiguration/azure-appconfiguration-sample appconfiguration/feature-management-sample appconfiguration/feature-management-web-sample + cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache cloudfoundry/azure-cloud-foundry-service-sample cosmos/spring-cloud-azure-starter-data-cosmos/spring-cloud-azure-data-cosmos-sample cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample