Skip to content

Commit 5eb6135

Browse files
authored
Add redis samples (#745)
* update readme * update samples for redis cache * Update pom.xml
1 parent 57fa2f4 commit 5eb6135

File tree

42 files changed

+1124
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1124
-18
lines changed

README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
### Azure Cache
3333

34-
| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x |
35-
|-----------------------------------------------------------------------------------------------------|--------------------------------|--------------------------------|
36-
| [azure-spring-cloud-sample-cache](cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache) |||
34+
| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x |
35+
|--------------------------------------------|--------------------------------|--------------------------------|
36+
| [azure-spring-cloud-sample-cache](cache) |||
3737

3838
### Azure Cosmos DB
3939

@@ -78,15 +78,17 @@
7878

7979
### Azure Service Bus
8080

81-
| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x |
82-
|-------------------------------------------------------------------------------------------------------------------|--------------------------------|--------------------------------|
83-
| [servicebus-jms-queue](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) |||
84-
| [servicebus-jms-topic](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) |||
85-
| [single-namespace](servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) |||
86-
| [multiple-namespaces](servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) |||
87-
| [servicebus-queue-binder](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) |||
88-
| [servicebus-queue-multibinders](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) |||
89-
| [servicebus-topic-binder](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) |||
81+
| Sample Project | Support Spring Cloud Azure 4.x | Support Spring Cloud Azure 5.x |
82+
|-----------------------------------------------------------------------------------------------|--------------------------------|--------------------------------|
83+
| [servicebus-queue](servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue) |||
84+
| [servicebus-topic](servicebus/spring-cloud-azure-starter-servicebus/servicebus-topic) |||
85+
| [servicebus-jms-queue](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-queue) |||
86+
| [servicebus-jms-topic](servicebus/spring-cloud-azure-starter-servicebus-jms/servicebus-jms-topic) |||
87+
| [single-namespace](servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace) |||
88+
| [multiple-namespaces](servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces) |||
89+
| [servicebus-queue-binder](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder) |||
90+
| [servicebus-queue-multibinders](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-multibinders) |||
91+
| [servicebus-topic-binder](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-topic-binder) |||
9092
| [servicebus-queue-binder-arm](servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm) |||
9193

9294
### Azure Storage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
"name": "[email protected]",
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).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.azure.spring</groupId>
9+
<artifactId>azure-spring-boot-samples</artifactId>
10+
<version>1.0.0</version>
11+
<relativePath>../../../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>spring-cloud-azure-sample-cache-passwordless</artifactId>
15+
<version>1.0.0</version>
16+
<packaging>jar</packaging>
17+
18+
<name>Spring Cloud Azure Sample Cache with Passwordless</name>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.azure.spring</groupId>
27+
<artifactId>spring-cloud-azure-starter-redis</artifactId>
28+
</dependency>
29+
</dependencies>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.spring.sample.cache;
5+
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.cache.annotation.EnableCaching;
9+
10+
@SpringBootApplication
11+
@EnableCaching
12+
public class CachePasswordlessApplication {
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(CachePasswordlessApplication.class, args);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.spring.sample.cache;
5+
6+
import org.springframework.cache.annotation.Cacheable;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
public class WebController {
13+
14+
@GetMapping("/{name}")
15+
@Cacheable("azureCache")
16+
public String getValue(@PathVariable("name") String name) {
17+
return "Hello " + name;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring:
2+
redis:
3+
host: ${AZURE_CACHE_REDIS_HOST}
4+
port: 6380
5+
username: ${AZURE_CACHE_REDIS_USERNAME}
6+
ssl: true
7+
azure:
8+
passwordless-enabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "AZURE_CACHE_REDIS_HOST" {
2+
value = azurerm_redis_cache.redis.hostname
3+
description = "The host name of the Redis instance."
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$env:AZURE_CACHE_REDIS_HOST=$(terraform -chdir=terraform output -raw AZURE_CACHE_REDIS_HOST)
2+
3+
echo AZURE_CACHE_REDIS_HOST=$env:AZURE_CACHE_REDIS_HOST
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export AZURE_CACHE_REDIS_HOST=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_HOST)
2+
3+
echo AZURE_CACHE_REDIS_HOST=AZURE_CACHE_REDIS_HOST

cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md renamed to cache/spring2-sample/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ You will build an application using the Spring Boot Starter Redis, Spring Cloud
2222
- [Terraform](https://www.terraform.io/)
2323
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)
2424
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later
25-
- Maven
25+
- [Maven](https://maven.apache.org/)
26+
- [cURL](https://curl.se/) or a similar HTTP utility to test functionality.
2627
- You can also import the code straight into your IDE:
2728
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download)
2829

cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml renamed to cache/spring2-sample/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.azure.spring</groupId>
99
<artifactId>azure-spring-boot-samples</artifactId>
1010
<version>1.0.0</version>
11-
<relativePath>../../../pom.xml</relativePath>
11+
<relativePath>../../../../pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>spring-cloud-azure-sample-cache</artifactId>
@@ -34,5 +34,6 @@
3434
<groupId>org.springframework.boot</groupId>
3535
<artifactId>spring-boot-starter-data-redis</artifactId>
3636
</dependency>
37-
</dependencies>
37+
</dependencies>
38+
3839
</project>
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class WebController {
1616

1717
@GetMapping("/{name}")
1818
@Cacheable("azureCache")
19-
public String getValue(@PathVariable String name) {
19+
public String getValue(@PathVariable("name") String name) {
2020
return "Hello " + name;
2121
}
2222
}

0 commit comments

Comments
 (0)