|
| 1 | +--- |
| 2 | +page_type: sample |
| 3 | +languages: |
| 4 | +- java |
| 5 | +products: |
| 6 | +- azure-cache-redis |
| 7 | +name: Caching Data to Azure Cache for Redis with PasswordLess in Spring Boot Application |
| 8 | +description: This sample demonstrates how to cache data to Azure Cache for Redis with PasswordLess in Spring Boot application. |
| 9 | +--- |
| 10 | + |
| 11 | +# Caching Data to Azure Cache for Redis with PasswordLess in Spring Boot Application |
| 12 | + |
| 13 | +This code sample demonstrates how to use [passwordless connections](https://learn.microsoft.com/azure/developer/intro/passwordless-overview) to cache data in [Azure Cache for Redis](https://azure.microsoft.com/products/cache/). |
| 14 | + |
| 15 | +## What You Will Build |
| 16 | +You will build an application using the Spring Cloud Azure Starter Redis to cache data to Azure Cache for Redis with passwordless. |
| 17 | + |
| 18 | +## What You Need |
| 19 | + |
| 20 | +- [An Azure subscription](https://azure.microsoft.com/free/) |
| 21 | +- [Terraform](https://www.terraform.io/) |
| 22 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) |
| 23 | +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later |
| 24 | +- [Maven](https://maven.apache.org/) |
| 25 | +- [cURL](https://curl.se/) or a similar HTTP utility to test functionality. |
| 26 | +- You can also import the code straight into your IDE: |
| 27 | + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) |
| 28 | + |
| 29 | +## Provision Azure Resources Required to Run This Sample |
| 30 | +This sample will create Azure resources using Terraform. |
| 31 | + |
| 32 | +### Authenticate Using the Azure CLI |
| 33 | +Terraform must authenticate to Azure to create infrastructure. |
| 34 | + |
| 35 | +In your terminal, use the Azure CLI tool to setup your account permissions locally. |
| 36 | + |
| 37 | +```shell |
| 38 | +az login |
| 39 | +``` |
| 40 | + |
| 41 | +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. |
| 42 | + |
| 43 | +```shell |
| 44 | +You have logged in. Now let us find all the subscriptions to which you have access... |
| 45 | + |
| 46 | +[ |
| 47 | + { |
| 48 | + "cloudName": "AzureCloud", |
| 49 | + "homeTenantId": "home-Tenant-Id", |
| 50 | + "id": "subscription-id", |
| 51 | + "isDefault": true, |
| 52 | + "managedByTenants": [], |
| 53 | + "name": "Subscription-Name", |
| 54 | + "state": "Enabled", |
| 55 | + "tenantId": "0envbwi39-TenantId", |
| 56 | + "user": { |
| 57 | + |
| 58 | + "type": "user" |
| 59 | + } |
| 60 | + } |
| 61 | +] |
| 62 | +``` |
| 63 | + |
| 64 | +If you have more than one subscription, specify the subscription-id you want to use with command below: |
| 65 | +```shell |
| 66 | +az account set --subscription <your-subscription-id> |
| 67 | +``` |
| 68 | + |
| 69 | +### Provision the Resources |
| 70 | + |
| 71 | +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. |
| 72 | + |
| 73 | +Now you can use the terraform script to create Azure Resources. |
| 74 | + |
| 75 | +#### Run with Bash |
| 76 | + |
| 77 | +```shell |
| 78 | +# In the root directory of the sample |
| 79 | +# Initialize your Terraform configuration |
| 80 | +terraform -chdir=./terraform init |
| 81 | + |
| 82 | +# Apply your Terraform Configuration |
| 83 | +terraform -chdir=./terraform apply -auto-approve |
| 84 | +``` |
| 85 | + |
| 86 | +#### Run with Powershell |
| 87 | + |
| 88 | +```shell |
| 89 | +# In the root directory of the sample |
| 90 | +# Initialize your Terraform configuration |
| 91 | +terraform -chdir=terraform init |
| 92 | + |
| 93 | +# Apply your Terraform Configuration |
| 94 | +terraform -chdir=terraform apply -auto-approve |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +It may take around 15 minutes to run the script. After successful running, you will see prompt information like below: |
| 99 | + |
| 100 | +```shell |
| 101 | + |
| 102 | +azurecaf_name.resource_group: Creating... |
| 103 | +azurecaf_name.azurecaf_name_redis: Creating... |
| 104 | +azurecaf_name.azurecaf_name_redis: Creation complete after 0s ... |
| 105 | +azurerm_redis_cache.redis: Still creating... |
| 106 | +azurerm_redis_cache.redis: Still creating... |
| 107 | +azurerm_redis_cache.redis: Creation complete after ... |
| 108 | + |
| 109 | +Apply complete! Resources: 4 added, 0 changed, 0 destroyed. |
| 110 | + |
| 111 | +Outputs: |
| 112 | +... |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. |
| 117 | + |
| 118 | +### Enable Microsoft Entra ID authentication on your cache |
| 119 | + |
| 120 | +Refer [Enable Microsoft Entra ID authentication on your cache](https://learn.microsoft.com/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication#enable-microsoft-entra-id-authentication-on-your-cache) and copy the value of username. |
| 121 | + |
| 122 | +### Export Output to Your Local Environment |
| 123 | +Running the command below to export environment values: |
| 124 | + |
| 125 | +#### Run with Bash |
| 126 | + |
| 127 | +```shell |
| 128 | +source ./terraform/setup_env.sh |
| 129 | +export AZURE_CACHE_REDIS_USERNAME='<copied-username>' |
| 130 | +``` |
| 131 | + |
| 132 | +#### Run with Powershell |
| 133 | + |
| 134 | +```shell |
| 135 | +terraform\setup_env.ps1 |
| 136 | +$env:AZURE_CACHE_REDIS_USERNAME='<copied-username>' |
| 137 | +``` |
| 138 | + |
| 139 | +If you want to run the sample in debug mode, you can save the output value. |
| 140 | + |
| 141 | +```shell |
| 142 | +AZURE_CACHE_REDIS_HOST=... |
| 143 | +AZURE_CACHE_REDIS_USERNAME=<copied-username> |
| 144 | +``` |
| 145 | + |
| 146 | +## Run Locally |
| 147 | + |
| 148 | +### Run the sample with Maven |
| 149 | + |
| 150 | +In your terminal, run `mvn clean spring-boot:run`. |
| 151 | + |
| 152 | +```shell |
| 153 | +mvn clean spring-boot:run |
| 154 | +``` |
| 155 | + |
| 156 | +### Run the sample in IDEs |
| 157 | + |
| 158 | +You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. |
| 159 | + |
| 160 | +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). |
| 161 | + |
| 162 | +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). |
| 163 | + |
| 164 | +## Verify This Sample |
| 165 | + |
| 166 | +1. Send a GET request to check, where `name` could be any string: |
| 167 | + |
| 168 | +```shell |
| 169 | +curl -XGET http://localhost:8080/{name} |
| 170 | +``` |
| 171 | + |
| 172 | +2. Confirm from Azure Redis Cache console in Azure Portal: |
| 173 | + |
| 174 | +```shell |
| 175 | +keys * |
| 176 | +``` |
| 177 | + |
| 178 | +## Clean Up Resources |
| 179 | +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. |
| 180 | + |
| 181 | +The terraform destroy command terminates resources managed by your Terraform project. |
| 182 | +To destroy the resources you created. |
| 183 | + |
| 184 | +#### Run with Bash |
| 185 | + |
| 186 | +```shell |
| 187 | +terraform -chdir=./terraform destroy -auto-approve |
| 188 | +``` |
| 189 | + |
| 190 | +#### Run with Powershell |
| 191 | + |
| 192 | +```shell |
| 193 | +terraform -chdir=terraform destroy -auto-approve |
| 194 | +``` |
| 195 | + |
| 196 | +## Deploy to Azure Spring Apps |
| 197 | + |
| 198 | +Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). |
0 commit comments